Dan2000
Posts: 6
Joined: Sat Oct 26, 2013 10:52 am

Text/annotation on a picture at arbitrary position...?

Tue Apr 21, 2015 5:10 pm

Hello guys,

I am not sure if this is the proper part of the forum to ask this question, but anyway here is my question:
How can I place an annotation (a short text) on the lower side of the captured image using python (picamera release 1.10)?
I use the example code below so as it is given in picamera release 1.10 documentation. This code prints the annotation only at the top center of the picture.

Code: Select all

import picamera
import time
 
with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 24
    camera.start_preview()
    camera.annotate_text = 'my Anottation!'
    time.sleep(2)
    camera.capture('myPic.jpg')
Can anyone help? Thank you very much for your suggestions. Please tell me also if I have to submit this question at Python's forum
Best regards

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9069
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Text/annotation on a picture at arbitrary position...?

Tue Apr 21, 2015 5:29 pm

You can't is the simple answer.

The annotation feature is exposing what was originally a debug feature that was attempting to interrupt the image processing pipe as little as possible. It therefore always works from the top of the image to get the stuff it wants to do done as soon as possible (the whole pipe works on data as soon as it is available, not waiting until the end of the frame).

I did do a load of tweaks under viewtopic.php?f=43&t=97929, but they haven't all rippled through to picamera yet.
The only thing I can think of that could be added relatively easily is to move the text to be left or right justified instead of centred. Moving up or down probably doesn't warrant the effort involved.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Text/annotation on a picture at arbitrary position...?

Tue Apr 21, 2015 5:39 pm

You could use pygame but the fonts seem to be limited to 1

Dan2000
Posts: 6
Joined: Sat Oct 26, 2013 10:52 am

Re: Text/annotation on a picture at arbitrary position...?

Wed Apr 22, 2015 7:32 am

6by9 wrote:You can't is the simple answer.

The annotation feature is exposing what was originally a debug feature that was attempting to interrupt the image processing pipe as little as possible. It therefore always works from the top of the image to get the stuff it wants to do done as soon as possible (the whole pipe works on data as soon as it is available, not waiting until the end of the frame).

I did do a load of tweaks under viewtopic.php?f=43&t=97929, but they haven't all rippled through to picamera yet.
The only thing I can think of that could be added relatively easily is to move the text to be left or right justified instead of centred. Moving up or down probably doesn't warrant the effort involved.
Thank you very much for the resolute answer. Then I have to just live with it or leave it, I think.
It would be very nice if it is possibility in the future version to put the annotation at any location of the picture with different font attributes.
Best regards

Dan2000
Posts: 6
Joined: Sat Oct 26, 2013 10:52 am

Re: Text/annotation on a picture at arbitrary position...?

Wed Apr 22, 2015 11:22 am

gordon77 wrote:You could use pygame but the fonts seem to be limited to 1
I am not familiar with this one. Is there any tutorial how I can use pygame to capture an image with the camera and put annotation on it? Thanks in advance

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Text/annotation on a picture at arbitrary position...?

Wed Apr 22, 2015 5:58 pm

Dan2000 wrote:
gordon77 wrote:You could use pygame but the fonts seem to be limited to 1
I am not familiar with this one. Is there any tutorial how I can use pygame to capture an image with the camera and put annotation on it? Thanks in advance
Here's an example l wrote in a similar thread.

viewtopic.php?f=32&t=93300&p=650971#p650971

You could use pygame to capture an image or just save the image with picamera and reload it.

User avatar
jbeale
Posts: 3689
Joined: Tue Nov 22, 2011 11:51 pm
Contact: Website

Re: Text/annotation on a picture at arbitrary position...?

Thu Apr 23, 2015 3:03 pm

If you don't mind the cpu/memory/time overhead of decompressing and recompressing the JPEG on the ARM cpu, then you could also use any of various image processing programs, that gives you complete control of font type, size, position, color etc. I have used imagemagick, which can do all that and much more.

See for example: http://www.imagemagick.org/Usage/annotating/

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Text/annotation on a picture at arbitrary position...?

Sat Apr 25, 2015 2:21 pm

Here's your original Picamera code + mine to add the text, in this case the date and time, to a still.

Code: Select all

#!/usr/bin/python
import os
import pygame, sys
from pygame.locals import *
import picamera
import time
import datetime

width = 1280
height = 720
 
with picamera.PiCamera() as camera:
    camera.resolution = (width, height)
    camera.framerate = 24
    camera.start_preview()
    camera.annotate_text = 'my Anottation!'
    time.sleep(2)
    camera.capture('myPic.jpg')

pygame.init()

dgryColor = pygame.Color(64,64,64)
greenColor = pygame.Color(0,255,0)
yellowColor = pygame.Color(255,255,0)
redColor = pygame.Color(255,0,0)
blueColor = pygame.Color(0,0,255)
whiteColor = pygame.Color(255,255,255)
greyColor = pygame.Color(128,128,128)
blackColor = pygame.Color(0,0,0)
purpleColor = pygame.Color(255,0,255)
lgryColor = pygame.Color(192,192,192)

windowSurfaceObj = pygame.display.set_mode((width,height),1,16)
pygame.display.set_caption(' Text on Image ')

# load image
imagefile = ('myPic.jpg')
image = pygame.image.load(imagefile)
windowSurfaceObj.blit(image,(0,0))
pygame.display.flip()

# define time format and colors
now = datetime.datetime.now()
msg = now.strftime("%Y/%m/%d %H:%M:%S")
fsize = 16 #font size
textcolor = 5 # 0 to 9
backcolor = 0 # 0 to 9, -1 for no background
fx = 1  # x position of text
fy = height - fsize # y postion of text

# put text on image and save
colors = [dgryColor,yellowColor,redColor,greenColor,blueColor,whiteColor,greyColor,blackColor,purpleColor,lgryColor]
tcolor = colors[textcolor]
lt = (len(msg) * (fsize/2))
if backcolor > -1:
   bcolor = colors[backcolor]
   pygame.draw.rect(windowSurfaceObj,bcolor,Rect(fx,fy, lt, fsize))
fontObj = pygame.font.Font('freesansbold.ttf',fsize)
msgSurfaceObj = fontObj.render(msg, False,tcolor)
msgRectobj = msgSurfaceObj.get_rect()
msgRectobj.topleft =(fx,fy)
windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
pygame.display.update(pygame.Rect(fx,fy,lt,fsize))
pygame.image.save(windowSurfaceObj,'myPic.jpg')
pygame.display.quit()

Return to “Camera board”