Thanks
Lukasz
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)
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:
If you need JPEG output, the classic libjpeg is pretty straightforward.
-Beej
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.
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=
On Windows, simply use Win32 api (6 lines of code) do draw text over Jpeg