Optimizing Imagemagick convert & composite 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
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Optimizing Imagemagick convert & composite code

Post by robocop »

Hi Magick Folks :D

I was wondering if it's possible to optimize the following convert and composite code ?

I've added 3 sample lines below of what i have. I have a huge file of words to convert and composite to a custom background canvas.

Everything is the same except the Labels.

( I copied this code from somewhere on this forum and modified it slightly to meet my needs. )

Code: Select all

convert -size 100x100 -background white -font arial.ttf -fill black label:"Australia" -trim +repage -depth 8 australia.jpg && composite -gravity center  australia.jpg  canvas.png  australia.jpg
convert -size 100x100 -background white -font 1/arial.ttf -fill black label:"Fiji" -trim +repage -depth 8 fiji.jpg && composite -gravity center  fiji.jpg  canvas.png  fiji.jpg
convert -size 100x100 -background white -font 1/arial.ttf -fill black label:"Tonga" -trim +repage -depth 8 tonga.jpg && composite -gravity center  tonga.jpg  canvas.png  tonga.jpg
I'm a total noob with this so any recommendations would be greatly appreciated. Thank you for your time!
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Imagemagick convert & composite code

Post by fmw42 »

Use one convert command with multiple parenthesis processing to crate the various label: images and multiple composites.

See
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/layers/#convert

Please always provide your IM version and platform, since syntax differs. See also viewtopic.php?f=1&t=9620
PandoraBox
Posts: 23
Joined: 2011-04-10T14:08:11-07:00
Authentication code: 8675308

Re: Optimizing Imagemagick convert & composite code

Post by PandoraBox »

If your under windows something like this in a batch file works well also.


for /F "tokens=*" %%A in (wordlist.txt) do call :process %%A
goto thenextstep
:process
set VAR1=%1
convert -size 100x100 -background white -font arial.ttf -fill black label:"%VAR1%" -trim +repage -depth 8 %VAR1%.jpg
composite -gravity center %VAR1%.jpg canvas.png %VAR1%.jpg
pause
goto :EOF

---------------
The wordlist.txt is where you can store your list of words I usually use this trick when I want to test a font on it's kerning.
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Re: Optimizing Imagemagick convert & composite code

Post by robocop »

fmw42 wrote: Please always provide your IM version and platform, since syntax differs. See also viewtopic.php?f=1&t=9620
Sorry i totally forgot about noting it.

Version: ImageMagick 6.7.7-10 2014-03-06 Q16 on Linux Ubuntu

I check those links out. Cheers
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Re: Optimizing Imagemagick convert & composite code

Post by robocop »

PandoraBox wrote:If your under windows something like this in a batch file works well also.


for /F "tokens=*" %%A in (wordlist.txt) do call :process %%A
goto thenextstep
:process
set VAR1=%1
convert -size 100x100 -background white -font arial.ttf -fill black label:"%VAR1%" -trim +repage -depth 8 %VAR1%.jpg
composite -gravity center %VAR1%.jpg canvas.png %VAR1%.jpg
pause
goto :EOF

---------------
The wordlist.txt is where you can store your list of words I usually use this trick when I want to test a font on it's kerning.
I'm actually using ImageMagick 6.7.7-10 on Linux Ubuntu and will give this a shot! ( Sorry i forgot to mention my version earlier ).

Thank you!
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Imagemagick convert & composite code

Post by fmw42 »

That code is for Windows and won't work on Unix.

In Unix syntax, try

Code: Select all

cd directory_containing_wordfile.txt
wordlist=`cat wordfile.txt`
for word in $wordlist; do
convert  canvas.jpg \
\( -size 100x100 -background white -font arial.ttf \
-fill black label:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Re: Optimizing Imagemagick convert & composite code

Post by robocop »

fmw42 wrote:That code is for Windows and won't work on Unix.

In Unix syntax, try

Code: Select all

cd directory_containing_wordfile.txt
wordlist=`cat wordfile.txt`
for word in $wordlist; do
convert  canvas.jpg \
\( -size 100x100 -background white -font arial.ttf \
-fill black label:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done
Yay! It works perfect :D I was tinkering with the windows code PandoraBox gave, and i was pretty hopeless with my attempts to make it work.

Thank you Fred. Couldn't get my stuff done without the immense help from you folks over here. Appreciated very much always! Please have a great weekend and Cheers
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
PandoraBox
Posts: 23
Joined: 2011-04-10T14:08:11-07:00
Authentication code: 8675308

Re: Optimizing Imagemagick convert & composite code

Post by PandoraBox »

Thanks fred, but like you say always if people post the version and operating system saves a huge amount of work however I can't complain when I see a comparison code for different platforms..

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

Re: Optimizing Imagemagick convert & composite code

Post by anthony »

fmw42 wrote:That code is for Windows and won't work on Unix.

In Unix syntax, try

Code: Select all

cd directory_containing_wordfile.txt
wordlist=`cat wordfile.txt`
for word in $wordlist; do
convert  canvas.jpg \
\( -size 100x100 -background white -font arial.ttf \
-fill black label:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done
Slight alternative for the shell script loop reading full lines from a file (with spaces)
One line per processing loop.

Code: Select all

while read line; do
   ...
done < file.txt
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Re: Optimizing Imagemagick convert & composite code

Post by robocop »

anthony wrote:
fmw42 wrote: Slight alternative for the shell script loop reading full lines from a file (with spaces)
One line per processing loop.

Code: Select all

while read line; do
   ...
done < file.txt
Thanks a lot Anthony. This is perfect for processing full lines, something i plan on doing later on.

Also to Fred and PandoraBox. This was all very helpful and i appreciate everyone's time.

Thanks very much :)
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Re: Optimizing Imagemagick convert & composite code

Post by robocop »

anthony wrote: 2015-12-08T16:45:38-07:00

Slight alternative for the shell script loop reading full lines from a file (with spaces)
One line per processing loop.

Code: Select all

while read line; do
   ...
done < file.txt
Hello there!

I was hoping someone could please help me. For the life of me, i am unable to get this shell script loop reading full lines to work on my end.

The following code is working perfectly for single words.

Code: Select all

cd directory_containing_wordfile.txt
wordlist=`cat wordfile.txt`
for word in $wordlist; do
convert  canvas.jpg \
\( -size 100x100 -background white -font arial.ttf \
-fill black label:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done
But i am unable to generate images with full lines and spaces. This is what i have tried below. Could anyone please let me know what i am doing wrong? Thank you

Code: Select all

cd directory_containing_wordfile.txt
wordlist=`cat wordfile.txt`
while read line; do
convert  canvas.jpg \
\( -size 100x100 -background white -font arial.ttf \
-fill black label:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done < wordfile.txt
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Optimizing Imagemagick convert & composite code

Post by snibgo »

The question is about bash scripting, not IM. The same problem would occur if you had "echo" instead of "convert".

This works:

Code: Select all

cat wordfile.txt | \
while read word; do
  echo $word
done
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing Imagemagick convert & composite code

Post by anthony »

Getting full lines and spaces requires careful quoting. You need to read, not concatenate into a work list. Note the use of "$line" in the loop..

Code: Select all

while read line; do
    convert  canvas.jpg \
               \( -size 100x100 -background white -font arial.ttf \
                  -fill black label:"$line" -trim +repage -depth 8 \) \
              -gravity center -composite "$line".jpg
done < wordfile.txt
Your mistake was you used "$word" when you were reading "$line".

NOTE: this will also create filenames with spaces, and punctuation. And while computers can handle such filenames, they make it harder to deal with later. I suggest the following...

Code: Select all

while read filename line; do
    convert  canvas.jpg \
               \( -size 100x100 -background white -font arial.ttf \
                  -fill black label:"$line" -trim +repage -depth 8 \) \
              -gravity center -composite "$filename".jpg
done < wordfile.txt
With a "wordfile.txt" containg the simple filename to use, then the line that file will contain..

Code: Select all

return  Return to Sender
invoiced  Bill has been Invoiced
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bratpit
Posts: 17
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Optimizing Imagemagick convert & composite code

Post by bratpit »

anthony wrote: 2015-12-08T16:45:38-07:00
fmw42 wrote:That code is for Windows and won't work on Unix.

In Unix syntax, try

Code: Select all

cd directory_containing_wordfile.txt
wordlist=`cat wordfile.txt`
for word in $wordlist; do
convert  canvas.jpg \
\( -size 100x100 -background white -font arial.ttf \
-fill black label:"$word" -trim +repage -depth 8 \) \
-gravity center -composite $word.jpg
done
Slight alternative for the shell script loop reading full lines from a file (with spaces)
One line per processing loop.

Code: Select all

while read line; do
   ...
done < file.txt
For is bad option for read lines from file.
While is better
but
This is safer option
while IFS= read -r line || [ -n "$line" ]; do
echo "$line"
done <file
The -r option to read prevents backslash interpretation (usually used as a backslash newline pair, to continue over multiple lines or to escape the delimiters). Without this option, any unescaped backslashes in the input will be discarded. You should almost always use the -r option with read.

And || [ -n "$line" ] to make it work with files without a newline at end.If there is such file shell does not read last line from the file.

To preserve white space at the beginning or the end of a line, specify IFS= (with no value) immediately before the read command. After reading is completed, the IFS returns to its previous value in shell variable.
robocop
Posts: 32
Joined: 2013-03-22T23:15:09-07:00
Authentication code: 6789

Re: Optimizing Imagemagick convert & composite code

Post by robocop »

Thank you all for the suggestions. It was very helpful. Cheers
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 | Ubuntu 18.04.1 LTS
Post Reply