combining multiple convert

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
marcovd
Posts: 2
Joined: 2015-01-18T23:40:01-07:00
Authentication code: 6789

combining multiple convert

Post by marcovd »

Hi all, i'm brand new to imagemagick and immensely appreciate any help offered. I worked up this very inefficient shell script to crop and append right-eye.png in 4 different files.

It works fine for offline use, but i want to implement this script now within PHP and i have a hard time figuring out how to combine all these commands in to one.

Any help would be greatly appreciated.

Code: Select all

#!/bin/bash
# right-eye.png 1280x2879 Width by Height pixels

convert right-eye.png -crop 1197x153+41+72 sidesA-green1.png
convert right-eye.png -crop 1113x153+124+291 sidesA-green2.png
convert right-eye.png -crop 1035x153+202+510 sidesA-green3.png
convert right-eye.png -crop 958x153+279+729 sidesA-green4.png

convert right-eye.png -crop 1197x153+41+1030 sidesA-red1.png
convert right-eye.png -crop 1113x153+124+1249 sidesA-red2.png
convert right-eye.png -crop 1035x153+202+1468 sidesA-red3.png
convert right-eye.png -crop 958x153+279+1687 sidesA-red4.png

convert right-eye.png -crop 1197x153+41+1990 sidesA-black1.png
convert right-eye.png -crop 1113x153+124+2209 sidesA-black2.png
convert right-eye.png -crop 1035x153+202+2428 sidesA-black3.png
convert right-eye.png -crop 958x153+279+2647 sidesA-black4.png

convert sidesA-red1.png sidesA-green1.png sidesA-black1.png -append sidesA-greenredblack1.png
convert sidesA-green2.png sidesA-red2.png sidesA-black2.png -append sidesA-greenredblack2.png
convert sidesA-red3.png sidesA-green3.png sidesA-black3.png -append sidesA-greenredblack3.png
convert sidesA-green4.png sidesA-red4.png sidesA-black4.png -append sidesA-greenredblack4.png

echo ***DONE***

User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: combining multiple convert

Post by fmw42 »

There are two common ways to do this more efficiently. Each does so by only reading the input image once and puts all commands into one command line. One method uses clones and parenthesis processing. The other uses in memory mpr: format and parenthesis processing.

Assume your script is called yourscript.sh

It would be called as

bash yourscript.sh yourinputimage

Here are the two scripts.

Code: Select all

#!/bin/bash
convert "$1" \
\
\( -clone 0 -crop 1197x153+41+72 +repage \) \
\( -clone 0 -crop 1113x153+124+291 +repage \) \
\( -clone 0 -crop 1035x153+202+510 +repage \) \
\( -clone 0 -crop 958x153+279+729 +repage \) \
\
\( -clone 0 -crop 1197x153+41+1030 +repage \) \
\( -clone 0 -crop 1113x153+124+1249 +repage \) \
\( -clone 0 -crop 1035x153+202+1468 +repage \) \
\( -clone 0 -crop 958x153+279+1687 +repage \) \
\
\( -clone 0 -crop 1197x153+41+1990 +repage \) \
\( -clone 0 -crop 1113x153+124+2209 +repage \) \
\( -clone 0 -crop 1035x153+202+2428 +repage \) \
\( -clone 0 -crop 958x153+279+2647 +repage \) \
\
\( -clone 1,5,9 -append -write sidesA-greenredblack1.png \) \
\( -clone 2,6,10 -append -write sidesA-greenredblack2.png \) \
\( -clone 3,7,11 -append -write sidesA-greenredblack3.png \) \
\( -clone 4,6,12 -append -write sidesA-greenredblack4.png \) \
null:
exit 0

Code: Select all

#!/bin/bash
convert "$1" -write mpr:img +delete \
\
\( mpr:img -crop 1197x153+41+72 +repage -write mpr:green1 \) \
\( mpr:img -crop 1113x153+124+291 +repage -write mpr:green2 \) \
\( mpr:img -crop 1035x153+202+510 +repage -write mpr:green3 \) \
\( mpr:img -crop 958x153+279+729 +repage -write mpr:green4 \) \
\
\( mpr:img -crop 1197x153+41+1030 +repage -write mpr:red1 \) \
\( mpr:img -crop 1113x153+124+1249 +repage -write mpr:red2 \) \
\( mpr:img -crop 1035x153+202+1468 +repage -write mpr:red3 \) \
\( mpr:img -crop 958x153+279+1687 +repage -write mpr:red4 \) \
\
\( mpr:img -crop 1197x153+41+1990 +repage -write mpr:black1 \) \
\( mpr:img -crop 1113x153+124+2209 +repage -write mpr:black2 \) \
\( mpr:img -crop 1035x153+202+2428 +repage -write mpr:black3 \) \
\( mpr:img -crop 958x153+279+2647 +repage -write mpr:black4 \) \
\
\( mpr:red1 mpr:green1 mpr:black1 -append -write sidesA-greenredblack1.png \) \
\( mpr:red2 mpr:green2 mpr:black2 -append -write sidesA-greenredblack2.png \) \
\( mpr:red3 mpr:green3 mpr:black3 -append -write sidesA-greenredblack3.png \) \
\( mpr:red4 mpr:green4 mpr:black4 -append -write sidesA-greenredblack4.png \) \
null:
exit 0

the final null: just means throw away any temporary images used in the command line, since all the output image are done by using -write in the parenthesis.

See
http://www.imagemagick.org/Usage/files/#write
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/Usage/files/#mpr
http://www.imagemagick.org/Usage/files/#null
marcovd
Posts: 2
Joined: 2015-01-18T23:40:01-07:00
Authentication code: 6789

Re: combining multiple convert

Post by marcovd »

Wow thanks a bunch fmw42! im gonna try it out :)

By the way are there any things i should consider when converting this code to the php imagick api library?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: combining multiple convert

Post by fmw42 »

The script will not run under Imagick. Imagick needs separate calls for each step and does not support mpr: or clones. The Imagick syntax is quite different from the command line syntax. It can be done in Imagick, but you can run these bash scripts directly with PHP exec()
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: combining multiple convert

Post by Bonzo »

I do not use Imagick for the reason fmw42 said above and when php was upgraded to 5.4 something Imagick stopped working. Although I believe it is fixed now and if it is only you using the code it is not so much of a problem. But on a live website it would be a pain.

How do you get your data? If in an array I do a loop and create the command something like:

Code: Select all

$cmd  = ' $input ';
foreach( $array as $value ){
$cmd .= ' ( -clone 0 -crop '.$value[1].'x'.$value[2].'+'.$value[3].'+'.$value[4].' +repage -write '.$value[0].'.png +delete )  ';
}

exec("convert $cmd NULL:);
I am currently working on some code and it took something like 3.7seconds to run as individual commands and went down to 1.5seconds using the example above. This was with 14 crops on a .png 800px x 250px approx.; I forget the actual size now.
Post Reply