Reduce the image generation time and optimize the imagemagick commands

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
atf4_solace
Posts: 20
Joined: 2017-09-15T07:57:03-07:00
Authentication code: 1151

Reduce the image generation time and optimize the imagemagick commands

Post by atf4_solace » 2019-01-30T03:23:38-07:00

Hello Team,

We are using the Version: ImageMagick 6.9.3-8 Q16 x86_64 2018-06-20 and Platform is LINUX and API is PHP using exec function.

We have created a frame image using the ImageMagick commands shown in the screenshot below

Please see screenshot for this:
http://prntscr.com/me3n6h
Currently, this image is taking 1.5 seconds to generate through all commands.

Below are the all commands used to generate an image as shown in the screenshot above

Below the fourth command is using the child image so please find it at this URL:
http://socialenginespecialist.com/imagi ... _child.jpg

Code: Select all

convert -size 420x580 xc:#4c6689 matframe_temp1.miff
convert -size 320x480 xc:#7d845c matframe_temp2.miff
composite -gravity center matframe_temp2.miff matframe_temp1.miff matframe_temp2.miff
convert info_back_image_vertical.jpg matframe_temp_child.miff
convert matframe_temp_child.miff -resize x480 matframe_temp3.miff
convert matframe_temp3.miff +repage -gravity center -crop 300x460+0+0 +repage matframe_temp3.miff
convert -background "rgba(0,0,0,0)" -size 228.57142857143x240 -fill "black" -gravity west label:"1.25 INCH SATIN BLACK FRAME\n\n1 - 16x24 opening\n\nAll products are\n Eco Friendly" matframe_temp4.miff
composite -gravity southwest -geometry +10+0 matframe_temp4.miff matframe_temp3.miff matframe_temp3.miff
convert -gravity center matframe_temp2.miff -fill none -strokewidth 2 -stroke '#FEFEFA' -draw 'rectangle 50,50 370,530' matframe_temp2.miff
composite -gravity center matframe_temp3.miff matframe_temp2.miff matframe_temp2.miff
convert matframe_temp2.miff matframe_final_image.jpg
So our questions are,
1. How can we reduce the processing time?
2. How can we group these multiple commands into one command so that our output will be faster?
3. If you have more suggestions for generating images fast please let us know.

Your help is appreciated.
Thanks.

snibgo
Posts: 12315
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Reduce the image generation time and optimize the imagemagick commands

Post by snibgo » 2019-01-30T06:30:41-07:00

First, change every "composite" to "convert". The order of the two input images must be reversed, and insert "-composite" after the two images. For example this...

Code: Select all

composite -gravity center matframe_temp2.miff matframe_temp1.miff matframe_temp2.miff
... will change to this:

Code: Select all

convert -gravity center matframe_temp1.miff matframe_temp2.miff -composite matframe_temp2.miff
Then test the script still works.

Then substitute miff filenames for whatever IM process made them. For example, "matframe_temp1.miff" is made from "-size 420x580 xc:#4c6689".

So the third command will become...

Code: Select all

convert -gravity center -size 420x580 xc:#4c6689 matframe_temp2.miff -composite matframe_temp2.miff
... and you no longer need to create matframe_temp1.miff.

Keep going until there is only one "convert" command.
snibgo's IM pages: im.snibgo.com

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

Re: Reduce the image generation time and optimize the imagemagick commands

Post by fmw42 » 2019-01-30T10:47:29-07:00

This link may help give an example.

https://imagemagick.org/Usage/layers/#convert

atf4_solace
Posts: 20
Joined: 2017-09-15T07:57:03-07:00
Authentication code: 1151

Re: Reduce the image generation time and optimize the imagemagick commands

Post by atf4_solace » 2019-01-31T02:50:16-07:00

Hello Team,

Thanks both of you for the answer

We have followed the suggestion and we have converted all these 10 commands to the below 1 command and its worked fine
and now for the same image generation, time is 0.5 and it was 2s before.

Code: Select all

convert -gravity center -size 420x580 xc:#4c6689 -size 320x480 xc:#7d845c -gravity center -composite \( \( info_back_image_vertical.jpg -resize x480 +repage -gravity center -crop 300x460+0+0 +repage \) -strokewidth 1 -fill 'black' -stroke none -gravity west \( -background 'rgba(0,0,0,0)' -size 228.57142857143x240 label:'1.25 INCH SATIN BLACK FRAME 1 - 16x24 opening All products are Eco Friendly' \) -gravity southwest -composite \) -gravity center -composite -fill none -strokewidth 2 -stroke '#FEFEFA' -draw 'rectangle 50,50 370,530' matframe_temp2.jpg
If we have any flaws in the command then please suggest there.

Thank you.

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

Re: Reduce the image generation time and optimize the imagemagick commands

Post by anthony » 2019-01-31T17:27:00-07:00

The main thing to keep in mind when in merging commands is that sometimes after setting some setting, you will need to unset or reset them, in case that setting effects a later step.

For example you start with a "-gravity center" later you do this again, when it is not needed as that setting would still be set.
Actually the first -gravity center is not needed as it is not used by the xc: image generator.

You can also simplify that first composite, by creating the smaller canvas and then using -border or -extent to add the 'frame' around it. You may also like to move that image creation to where it is actually needed, rather than having to do two 'stack save' (open parenthesis) operations.

Also why -size 228.57142857143x240 ? It doesn't make a lot of sense... why no just -size 228x240 which is much easier to parse

Basically slowly simplify steps. I do that all the time as I merge operations.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/

atf4_solace
Posts: 20
Joined: 2017-09-15T07:57:03-07:00
Authentication code: 1151

Re: Reduce the image generation time and optimize the imagemagick commands

Post by atf4_solace » 2019-02-01T06:32:56-07:00

Thank you for the answer, Anthony

We have followed your suggestions and implemented these
Can we globally reset all settings at once?

Thank You

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

Re: Reduce the image generation time and optimize the imagemagick commands

Post by fmw42 » 2019-02-01T10:17:32-07:00

Each setting must be reset separately. There is no command to globally reset everything that I know

Post Reply