Page 1 of 1

Optimization of Code

Posted: 2008-06-10T04:16:37-07:00
by seans
I have got a large number of images which I need to resize and put a text box under the new resized image containing some text.

My code works fine but I can't help thinking that it is quite inefficient. I have only just started using Imagemagick and it seems to be a highly efficient tool if used correctly. 5 lines of code seems quite a lot for what seems to be a relatively easy task.

It would be great to know if there is a better way of doing this.

Thanks a lot,
Sean

REM ** CREATE IMAGE **
rem resize image so that 150px is longest dimension
convert -resize 150x150 c01t01v0010.bmp output1.jpg
rem place on 150x150 white background (imagetemplate.jpg)
composite -gravity center output1.jpg template\imagetemplate.jpg output2.jpg


REM ** CREATE TEXT **
rem Put text onto white background 150px wide, height variable
convert -gravity center -font Arial -pointsize 12 -size 150x caption:"This is my text" output3.jpg
rem Place image on 150x20 image (texttemplate.jpg)
composite -gravity center output3.jpg template\texttemplate.jpg output4.jpg

REM ** ADD TOGETHER **
convert output2.jpg output4.jpg -append -trim final.jpg

Re: Optimization of Code

Posted: 2008-06-10T05:17:25-07:00
by Bonzo
Not sure exactly what you want or what method you are using - I tend to use php.

Code: Select all

Two lines and one tempory image:
<?php
exec("convert \( screen_capture.jpg -resize 150x150 \) -size 150x200 xc:white -gravity center +swap -composite output.jpg"); 

exec("convert output.jpg -fill black -pointsize 12 -gravity south -annotate +0+0 \"This is my text\" output3.jpg");
?>

One line:
<?php
exec("convert \( screen_capture.jpg -resize 150x150 \) -size 150x200 xc:white -gravity center +swap -composite -fill black -pointsize 12 -gravity south -annotate +0+0 \"This is my text\" output4.jpg");
?>


Re: Optimization of Code

Posted: 2008-06-10T16:22:53-07:00
by anthony
See IM Examples, Annotating Images.

The number of operations depends on what you are trying to do.
You need at least 5 operations minimum...
  • read
  • resize
  • create_label
  • append
  • write
anything else would depend on other aspects like,
  • making space for the label
  • centering,
  • making the label the right size,
  • drawing text,
  • etc.
Looking at the code, you are doing a composition, which is not the method I would have chosen, also the composite operator is available now in the "convert" command meaning you can merge your operations into a single command, avoiding multiple reads, writes and intermediate temporary files.

Actually try "montage" which most people overlook, and yet provides the fastest and simplest way to BOTH resize and label images. It has lots of controls for coloring and framing too!!!