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
Optimization of Code
Re: Optimization of Code
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");
?>
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Optimization of Code
See IM Examples, Annotating Images.
The number of operations depends on what you are trying to do.
You need at least 5 operations minimum...
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!!!
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
- making space for the label
- centering,
- making the label the right size,
- drawing text,
- etc.
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/
https://imagemagick.org/Usage/