Optimization of Code

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
seans

Optimization of Code

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Optimization of Code

Post 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");
?>

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimization of Code

Post 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!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply