Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Embedding text in images

2 views
Skip to first unread message

lukaszmail

unread,
Nov 9, 2009, 2:56:25 PM11/9/09
to
Does anyone know of a way to programatically embed text in an image
like jpeg? Perhaps some template with text placeholders that could be
updated at runtime and re-saved as jpeg? The idea is to create coupon
graphic that can be printed as jpeg, but it must be updated with
proper amount at runtime.

Thanks

Lukasz

Daniel Pitts

unread,
Nov 9, 2009, 5:55:07 PM11/9/09
to
I would suggest two things:

1. Keep the original template as a lossless file (PNG, with the right
settings is nice for this)

2. Keep the "templated" text out of the graphics file altogether, and
have a process that will take the input image, and text specifications,
and create an output image (jpg is fine for this, but PNG will usually
have higher quality)

Beej Jorgensen

unread,
Nov 9, 2009, 6:30:35 PM11/9/09
to

Seconded, what Daniel said about keeping the text information separate
from the original image and keeping the original as a PNG.

The Cairo lib (there are many, but it's one that I know of) has support
for reading and writing PNG files and for drawing text on surfaces:

http://www.cairographics.org/

If you need JPEG output, the classic libjpeg is pretty straightforward.

-Beej

Daniel Pitts

unread,
Nov 9, 2009, 6:37:17 PM11/9/09
to
ImageMagic is also a good command-line based tool which I believe can
read/write PNGs and JPGs, and compose images with other images and text.

I haven't used it for that purpose specifically, but I seem to recall
its support being good enough for the purpose the OP described.

Gene

unread,
Nov 9, 2009, 9:22:54 PM11/9/09
to
On Nov 9, 6:37 pm, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> Beej Jorgensen wrote:
> its support being good enough for the purpose the OP described.- Hide quoted text -

Right. I had a need to attach a caption as graphic characters on some
standard-sized (410x307) JPEGs. Here's a Windows command script that
does a reasonable job:

@echo off
rem
rem Add a caption to a slideshow image of standard size.
rem

echo -------------------------------------------------------------
echo You need to install ImageMagik before this script will work.
echo A Windows Setup version is available at www.imagemagick.org .
echo Press Ctrl-C to quit if you need to install first. Otherwise,
echo -------------------------------------------------------------
pause

set savedpath=%path%
path c:\ImageMagick-6.3.7\bin;%path%

rem Deal with bad args.
if a%2==a goto usage
if not exist %1 goto usage

rem parse image width into %%w and height into %%x
for /f "tokens=3" %%a in ('identify %1') do (
for /f "tokens=1,2 delims=x" %%w in ("%%a") do (
if not %%w==410 (
echo
-------------------------------------------------------------
echo Image file %1 is %%w pixels wide and needs to be 410.
goto usage
)
if %%x lss 307 (
echo
-------------------------------------------------------------
echo Image file %1 is %%x pixels high and needs to be at least
307.
goto usage
)
)
)
convert -background white -fill black -font Arial-Bold-Italic -
pointsize 14 -size 410x -gravity center caption:%2 caption.gif
if errorlevel 1 goto convert_error
convert %1 -crop 410x307-0-0 cropped.jpg
if errorlevel 1 goto crop_error
montage -tile 1x2 -geometry +0+0 cropped.jpg caption.gif complete.jpg
if errorlevel 1 goto montage_error
copy complete.jpg %1
if errorlevel 1 goto copy_error
del caption.gif
del cropped.jpg
del complete.jpg
start %1
goto done

:usage
echo -------------------------------------------------------------
echo usage:
echo caption GraphicFileName "Caption text."
echo note:
echo Graphic must be 410 pixels wide and at least 307 high.
echo -------------------------------------------------------------
)
goto done

:convert_error
echo -------------------------------------------------------------
echo text conversion error
echo -------------------------------------------------------------
goto done

:crop_error
echo -------------------------------------------------------------
echo error cropping image
echo -------------------------------------------------------------
goto done

:montage_error
echo -------------------------------------------------------------
echo error joining caption to image
echo -------------------------------------------------------------
goto done

:copy_error
echo -------------------------------------------------------------
echo can't write final image %1 (see complete.jpg for result)
echo -------------------------------------------------------------
goto done

:done
path %savedpath%
set savedpath=

georges

unread,
Nov 10, 2009, 2:10:12 AM11/10/09
to

On Windows, simply use Win32 api (6 lines of code) do draw text over Jpeg

0 new messages