Page 1 of 1

Image Magick commands are taking time on server

Posted: 2017-09-14T08:58:54-07:00
by atf_solace
Hi Geeks,

We are using sequence of commands to generate one output through imagemagic command.

Image used in first command is here - http://solaceinfotech.com/projects/tmp/ ... 50373.jpeg
Just for quick reference, here is what each command does.

Here are the commands.

Code: Select all

"/usr/bin/convert" 15049951621774150373.jpeg -scale 3600x2400! solace_tmp_0.png
cp solace_tmp_0.png solace_dummy.png
mogrify -extent 3600x2400 -background "#FFFFFF" -gravity Center solace_dummy.png
"/usr/bin/convert" solace_dummy.png -fill black -draw "color 0,0 reset" solace_dummy.png
"/usr/bin/convert" solace_dummy.png -draw "image over 0,0+3600+2400 '15049951621774150373.jpeg'" solace_merge_0.png
cp solace_merge_0.png solace_first.png
mogrify -extent 4800x3600 -background "#FFFFFF" -gravity Center solace_first.png
"/usr/bin/convert" solace_first.png -fill white -draw "color 0,0 reset" solace_first.png
"/usr/bin/convert" solace_first.png -draw "image over 600,600+3600+2400 'solace_merge_0.png'" solace_main_final.png
cp solace_main_final.png solace_first.png
"/usr/bin/convert" solace_main_final.png -font /fonts/arial/arial.ttf -pointsize 19.935 -fill red -annotate +0+3596 " Text 1 " solace_main_final.png
"/usr/bin/convert" solace_main_final.png -font /fonts/arial/arial.ttf -pointsize 19.935 -fill black -annotate +0+3596 " Text 2 " solace_main_final.png
"/usr/bin/convert" solace_main_final.png -draw "image over 1453,3521+75+75 'solace_qr_image.png'" solace_main_final.png


1. Resizing the original image and generate the TEMP.>> 9 seconds
2. Copy the image to dummy image.
2. Mogrify it to add extra space as dummy image.>> 8 seconds
3. Apply black color to the image dummy image.>> 2 seconds
4. Place original image in dummy image and say merge image.>> 11 seconds
5. Copy Merge image to first image
6. Mogrify the first image to original width and height.>> 8 seconds
7. Apply color white to first image.>> 1 seconds
8. Assinged the first image to master variable
8. Create a main final image by placing the merge image onto the master image.>> 11 seconds
9. Main final image copied to master image.
10.Draw text 1 on image>> 9 seconds
11.Draw text 2 on image>> 10 seconds
12 Draw QR image on image.>> 10 seconds


My Imagemagic installation details -
Version: ImageMagick 6.9.3-8 Q16 x86_64 2017-06-12
Features: Cipher DPC OpenMP

Execution of above commands take total 82 seconds at my end. Individual timings are mentioned as well above.

Can you please try running these commands at your server with same image and mention me how much total time its taking on your server?
If its taking less time, please mention configuration of your sever and details of Image magic installation.?

Re: Image Magick commands are taking time on server

Posted: 2017-09-14T09:38:23-07:00
by snibgo
I assume you want to reduce the time taken.

For your entire process, you seem to have one input and one output. But you have about eleven intermediate files, with associated file write/read, and image decompression/compression. I suggest you re-write it to use no intermediate files.

EDIT: You also have an input file solace_qr_image.png, which you haven't provided.

Re: Image Magick commands are taking time on server

Posted: 2017-09-14T10:39:53-07:00
by snibgo
It took 62 seconds on my Windows 8.1, IM v6.9.5-3. (The final command failed, as I don't have solace_qr_image.png.)

By changing the .png extensions from .png to .miff, the time reduced to 5.5 seconds.

Thus, almost all the time required is in reading/writing PNG files.

Re: Image Magick commands are taking time on server

Posted: 2017-09-16T22:36:57-07:00
by aaroncalvo
.miff

Wow that's a 90%+ reduction.

Can I ask you please to share your updated logic. It would be very helpful.

Thanks in advance

Edit, just to confirm, all you did was change the extension or something else as well?

Re: Image Magick commands are taking time on server

Posted: 2017-09-16T23:10:02-07:00
by snibgo
I didn't change the logic. I changed the syntax for Windows BAT, then changed the PNG extension to MIFF.

When a file is merely an intermediate result, we don't usually need to save disk space. Compressing and uncompressing takes time. An uncompressed format (miff or tiff) is much faster than PNG.

Changing the logic, so no intermediate files are written, and the input JPEG is read only once, would save even more time.

Code: Select all

set TMPEXT=miff

%IM%convert ^
  15049951621774150373.jpeg ^
  -scale 3600x2400! ^
  solace_tmp_0.%TMPEXT%

cp solace_tmp_0.%TMPEXT% solace_dummy.%TMPEXT%

%IM%mogrify -extent 3600x2400 -background "#FFFFFF" -gravity Center solace_dummy.%TMPEXT%
%IM%convert solace_dummy.%TMPEXT% -fill black -draw "color 0,0 reset" solace_dummy.%TMPEXT%
%IM%convert solace_dummy.%TMPEXT% -draw "image over 0,0 3600,2400 '15049951621774150373.jpeg'" solace_merge_0.%TMPEXT%

cp solace_merge_0.%TMPEXT% solace_first.%TMPEXT%

%IM%mogrify -extent 4800x3600 -background "#FFFFFF" -gravity Center solace_first.%TMPEXT%
%IM%convert solace_first.%TMPEXT% -fill white -draw "color 0,0 reset" solace_first.%TMPEXT%
%IM%convert solace_first.%TMPEXT% -draw "image over 600,600 3600,2400 solace_merge_0.%TMPEXT%" solace_main_final.%TMPEXT%

cp solace_main_final.%TMPEXT% solace_first.%TMPEXT%

%IM%convert solace_main_final.%TMPEXT% -font arial -pointsize 19.935 -fill red -annotate +0+3596 " Text 1 " solace_main_final.%TMPEXT%
%IM%convert solace_main_final.%TMPEXT% -font arial -pointsize 19.935 -fill black -annotate +0+3596 " Text 2 " solace_main_final.%TMPEXT%
%IM%convert solace_main_final.%TMPEXT% -draw "image over 1453,3521 75,75 solace_qr_image.png" solace_main_final.%TMPEXT%

Re: Image Magick commands are taking time on server

Posted: 2017-09-18T07:45:43-07:00
by atf_solace
Thanks a lot snibgo for your reply. Its working good now.
Awesome guidance on the topic, heartly thanks for it.

I want to ask one more question.
Question : Can you please provide some guidance on combining the image magic commands?
I am new to the image magic, if anyone can please help me with combining image magic command for above ones, it would be really appreciated.

Thanks.

Re: Image Magick commands are taking time on server

Posted: 2017-09-18T09:37:46-07:00
by snibgo
Step 1: Each "mogrify" command processes just one image, so change them into "convert" commands. The order should be corrected: "-background" and "-gravity" are options to "-extent", so need to come first.

Step 2: Consider these three converts, as a group. I've broken them into lines to make them easier to read. (The Windows line-continuation is "^". For bash, use "\".)

Code: Select all

%IM%convert ^
  solace_dummy.%TMPEXT% ^
  -background "#FFFFFF" -gravity Center -extent 3600x2400 ^
  solace_dummy.%TMPEXT%

%IM%convert ^
  solace_dummy.%TMPEXT% ^
  -fill black -draw "color 0,0 reset" ^
  solace_dummy.%TMPEXT%

%IM%convert ^
  solace_dummy.%TMPEXT% ^
  -draw "image over 0,0 3600,2400 '15049951621774150373.jpeg'" ^
  solace_merge_0.%TMPEXT%
The output of each is the input to the next. So simply make them a single command:

Code: Select all

%IM%convert ^
  solace_dummy.%TMPEXT% ^
  -background "#FFFFFF" -gravity Center -extent 3600x2400 ^
  -fill black -draw "color 0,0 reset" ^
  -draw "image over 0,0 3600,2400 '15049951621774150373.jpeg'" ^
  solace_merge_0.%TMPEXT%
To be honest, I don't know what this is trying to do. The input is already 3600x2400, so "-extent" does nothing. Then you turn the image entirely black, which is fair enough, but then you draw the original input over it. So what is the point of this? Can we remove it entirely?

The next group of three is more promising:

Code: Select all

%IM%convert ^
  solace_first.%TMPEXT% ^
  -extent 4800x3600 -background "#FFFFFF" -gravity Center ^
  solace_first.%TMPEXT%

%IM%convert ^
  solace_first.%TMPEXT% ^
  -fill white -draw "color 0,0 reset" ^
  solace_first.%TMPEXT%

%IM%convert ^
  solace_first.%TMPEXT% ^
  -draw "image over 600,600 3600,2400 solace_merge_0.%TMPEXT%" ^
  solace_main_final.%TMPEXT%
The "-extent" makes the image larger. But then you make it entirely white, and then you draw an image over that. We can replace the first two converts with:

Code: Select all

%IM%convert ^
  -size 4800x3600 xc:White ^
  solace_first.%TMPEXT%
Your "-draw" takes the input image, at the same size (3600x2400) and places it in the centre. So the overall effect of these commands is simply to add a 600 pixel white border, like this:

Code: Select all

%IM%convert ^
  15049951621774150373.jpeg ^
  -bordercolor white -border 600 ^
  solace_main_final.%TMPEXT%
And then you add text, which is fair enough. When combining commands, we need to watch out for settings that will "carry forwards" to later operations, such a "-gravity". Your script seems to boil down to:

Code: Select all

%IM%convert ^
  15049951621774150373.jpeg ^
  -resize 3600x2400! ^
  -gravity Center -bordercolor white -border 600 ^
  -gravity SouthWest ^
  -font arial -pointsize 19.935 ^
  -fill red -annotate +0+0 " Text 1 " ^
  -fill black -annotate +0+0 " Text 2 " ^
  solace_main_final.%TMPEXT%
(I used "-resize" rather than "-scale" because the quality is generally better, but it is slower. I base the text on the south-west corner.)

This takes 0.84 seconds when writing to miff, 0.97 seconds to tiff, or 8.69 seconds writing to png.