Page 1 of 1
Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T04:31:35-07:00
by robocop
Hi Magick Folks
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!
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T11:03:37-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T17:23:50-07:00
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.
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T18:09:11-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T18:13:20-07:00
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!
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T21:18:37-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-05T23:29:23-07:00
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
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-06T06:47:04-07:00
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.
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-08T16:45:38-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2015-12-08T19:36:59-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2018-10-10T02:56:49-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2018-10-10T04:25:14-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2018-10-13T16:23:30-07:00
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
Re: Optimizing Imagemagick convert & composite code
Posted: 2018-10-14T00:47:16-07:00
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.
Re: Optimizing Imagemagick convert & composite code
Posted: 2018-10-17T22:57:59-07:00
by robocop
Thank you all for the suggestions. It was very helpful. Cheers