Please help me optimize my convert command string

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
laurens_at_hol
Posts: 20
Joined: 2011-11-16T08:53:16-07:00
Authentication code: 8675308

Please help me optimize my convert command string

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Please help me optimize my convert command string

Post 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.
snibgo's IM pages: im.snibgo.com
laurens_at_hol
Posts: 20
Joined: 2011-11-16T08:53:16-07:00
Authentication code: 8675308

Re: Please help me optimize my convert command string

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Please help me optimize my convert command string

Post 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
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Please help me optimize my convert command string

Post 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
laurens_at_hol
Posts: 20
Joined: 2011-11-16T08:53:16-07:00
Authentication code: 8675308

Re: Please help me optimize my convert command string

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Please help me optimize my convert command string

Post 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.
snibgo's IM pages: im.snibgo.com
laurens_at_hol
Posts: 20
Joined: 2011-11-16T08:53:16-07:00
Authentication code: 8675308

Re: Please help me optimize my convert command string

Post 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.
Post Reply