Page 1 of 1

Please help me optimize my convert command string

Posted: 2013-09-25T02:47:36-07:00
by laurens_at_hol
Hi,

I'm working on a convert command that takes a random photo, resizes it to a fixed size, creates rounded corners and puts a black background behind it.
The rounded corners part is from the examples from the ImageMagick website. I'm not sure this is the best way to do it, but at least it is all done in one call.
I want the output to be 150 dpi so I put in the resample command. I added the profile 8bim, because I read that this ensures PhotoShop also recognizes the new 150 dpi setting.

I have a working convert string that does what I want, but I was hoping to optimize it for speed and cpu/memory usage.

The convert string I have sofar is:

Code: Select all

convert original.jpg -resample 150 +profile 8bim -thumbnail 827x827 -format 'roundrectangle 1,1 %[fx:w+4],%[fx:h+4] 20,20' -write info:tmp.mvg -alpha set -bordercolor none -border 3 \( +clone -alpha transparent -background none -fill white -stroke none -strokewidth 0 -draw @tmp.mvg \) -compose DstIn -composite \( +clone -alpha transparent -background none -fill none -stroke black -strokewidth 3 -draw @tmp.mvg \) -compose Over -composite -gravity center -background black -extent 1063x1063 output.jpg
I tried to write to a mpr instead of a file on disk, because I thought that might speed things up but I couldn't get it to work.

Could someone take a look and give me some advice to optimize the convert command? Thanks!

Re: Please help me optimize my convert command string

Posted: 2013-09-25T05:41:49-07:00
by snibgo
The mpr format is for images. I don't think it can be used for text.

The border and second clone don't seem to be useful. Here's a simplified command. Windows format, so you'll need to convert it back to Unix (change "^" to "\" and "%%" to "%").

Code: Select all

convert ^
  original.jpg ^
  -resample 150 +profile 8bim -thumbnail 827x827 ^
  -format "roundrectangle 0,0 %%[fx:w-1],%%[fx:h-1] 20,20" -write info:tmp.mvg ^
  ( +clone -alpha transparent ^
     -background none -fill white -stroke none -strokewidth 3 -draw @tmp.mvg ^
  ) ^
  -compose DstIn -composite ^
  -gravity center -background black -compose Over -extent 1063x1063 ^
  output3.jpg
The result isn't quite identical to your version.

Re: Please help me optimize my convert command string

Posted: 2013-09-25T07:06:08-07:00
by laurens_at_hol
Hi snibgo, thanks for your reply.

I tried your modified command and it gives me about 11% speed improvement. I also changed the colornames to hashcodes. That pushed it to 13% speed improvement.
I think 13% is quite an improvement, so that's great.

I noticed you changed the settings for the rounded corners a bit. I like it better your way :)

In this topic I also read something about Read Modifiers to reduce memory requirements.
http://www.imagemagick.org/Usage/api/#speed
Can I use [#x#] Read Resize? How would that go in combination with the -resample and -thumbnail options I already use?


Thanks again.

Re: Please help me optimize my convert command string

Posted: 2013-09-25T08:04:50-07:00
by snibgo
"-resample" and "-thumbnail" will both change the number of pixels in the image. Doing both will take time and lower the output quality. I don't know the requirements for Photoshop, but I suggest you do only one resample or thumbnail or resize but not two.

I doubt if read modifiers will make a significant difference (compared to a single "-resize"), but you can try. It will depend on the input image. See http://www.imagemagick.org/Usage/files/#read_mods

Re: Please help me optimize my convert command string

Posted: 2013-09-25T09:45:17-07:00
by Bonzo
You could try adding -define jpeg:size=827x827
http://www.imagemagick.org/script/comma ... php#define

Code: Select all

convert -define jpeg:size=827x827 ^
  original.jpg ^
  -resample 150 +profile 8bim -thumbnail 827x827 ^
  -format "roundrectangle 0,0 %%[fx:w-1],%%[fx:h-1] 20,20" -write info:tmp.mvg ^
  ( +clone -alpha transparent ^
     -background none -fill white -stroke none -strokewidth 3 -draw @tmp.mvg ^
  ) ^
  -compose DstIn -composite ^
  -gravity center -background black -compose Over -extent 1063x1063 ^
  output3.jpg

Re: Please help me optimize my convert command string

Posted: 2013-09-26T06:15:29-07:00
by laurens_at_hol
@snibgo
I do the resample and thumbnail because I want the photo in the middle to be exactly 14x14cm with resolution of 150 dpi.
Is there another (better) way to achieve this? Can it be done by using only one command?
I did some tests with read modifiers, but didn't get good results. I think I won't use that.

@Bonzo
I will try that, thanks.

Re: Please help me optimize my convert command string

Posted: 2013-09-26T06:50:53-07:00
by snibgo
Using "-density 150" instead of "-resample 150" should do the job faster, but you'll need to check that Photoshop is happy with this.

Re: Please help me optimize my convert command string

Posted: 2013-09-26T07:56:50-07:00
by laurens_at_hol
Using -density 150 instead of -resample 150 is about 10% faster. I still need the -thumbnail 827x827 to resize the image.
PhotoShop still recognizes the 150dpi without the use of -resample.

I also tested -define jpeg:size=827x827, that is less than 1% faster. I'm not sure if I will be using that.