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 » 2015-12-05T04:31:35-07:00

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: 24134
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Imagemagick convert & composite code

Post by fmw42 » 2015-12-05T11:03:37-07:00

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 » 2015-12-05T17:23:50-07:00

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 » 2015-12-05T18:09:11-07:00

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 » 2015-12-05T18:13:20-07:00

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: 24134
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Optimizing Imagemagick convert & composite code

Post by fmw42 » 2015-12-05T21:18:37-07:00

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 » 2015-12-05T23:29:23-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
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 » 2015-12-06T06:47:04-07:00

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: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing Imagemagick convert & composite code

Post by anthony » 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
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 » 2015-12-08T19:36:59-07:00

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 » 2018-10-10T02:56:49-07:00

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: 10958
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Optimizing Imagemagick convert & composite code

Post by snibgo » 2018-10-10T04:25:14-07:00

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: 8871
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Optimizing Imagemagick convert & composite code

Post by anthony » 2018-10-13T16:23:30-07:00

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: 8
Joined: 2018-09-16T00:20:21-07:00
Authentication code: 1152

Re: Optimizing Imagemagick convert & composite code

Post by bratpit » 2018-10-14T00:47:16-07:00

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 » 2018-10-17T22:57:59-07:00

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