Is it possible to produce 2 different sized images in one go

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
NeilF

Is it possible to produce 2 different sized images in one go

Post by NeilF »

Currently I'm producing two images via two commands. One for optimised TV viewing, and another as a small thumbnail.

Is it possible to do this in one command, hopefully speeding the process up?


Here's examples of my two commands:-
tiny thumbnail: convert <filename> -auto-orient -resize 200x150 -background black -gravity center -extent 200x150 -quality 60 -strip <thumb_filename>
resized photo: convert <fileName> -auto-orient -resize 1920x1080> -quality 60 <thumb_filename>

Notice on the thumbnail making the image 200x150, complete with black background, and stripping out the metatags to reduce filesize.

Now the source file could be X meg big so I suspect if these two conversions could be done by one command instead of two, there might be some speed savings to be had!?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Is it possible to produce 2 different sized images in on

Post by Bonzo »

Try:

Code: Select all

convert <filename> -auto-orient ( +clone -resize 200x150 -background black -gravity center -extent 200x150 -quality 60 -strip -write <thumb_filename> ) -resize 1920x1080> -quality 60 <thumb_filename>
You may need to use a \ in front of the ( and )
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Is it possible to produce 2 different sized images in on

Post by anthony »

See IM examples, Image File handling, Writing an Image Multiple Times
http://www.imagemagick.org/Usage/files/#write
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
NeilF

Re: Is it possible to produce 2 different sized images in on

Post by NeilF »

Bonzo wrote:Try:

Code: Select all

convert <filename> -auto-orient ( +clone -resize 200x150 -background black -gravity center -extent 200x150 -quality 60 -strip -write <thumb_filename> ) -resize 1920x1080> -quality 60 <thumb_filename>
You may need to use a \ in front of the ( and )
Thanks... Combining them like below almost halved the time for large photos to generate two images... Guess because when the 200x150 is generated it's already working with a far smaller image!

Just seemed to need a -write statement in there, and also a +delete else you'd get some oddly name files popping out too? Huh?

eg:-
convert 001_2.JPeG -resize "1920x1080>" -auto-orient ( +clone -strip -resize 200x150 -background black -gravity center -extent 200x150 -quality 60 -write 001_2_THUMB3.jpg +delete ) -quality 60 001_2_RESIZE3.jpg
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Is it possible to produce 2 different sized images in on

Post by Bonzo »

To save time on jpg images you can add a "hint" but it may not work very well in this case.

Code: Select all

convert -define jpeg:size=1920x1080 001_2.JPeG -resize "1920x1080>" -auto-orient ( +clone -strip -resize 200x150 -background black -gravity center -extent 200x150 -quality 60 -write 001_2_THUMB3.jpg +delete ) -quality 60 001_2_RESIZE3.jpg 
I belive the speed increase is that the image is only read in once.
NeilF

Re: Is it possible to produce 2 different sized images in on

Post by NeilF »

Bonzo wrote:To save time on jpg images you can add a "hint" but it may not work very well in this case.

Code: Select all

convert -define jpeg:size=1920x1080 001_2.JPeG -resize "1920x1080>" -auto-orient ( +clone -strip -resize 200x150 -background black -gravity center -extent 200x150 -quality 60 -write 001_2_THUMB3.jpg +delete ) -quality 60 001_2_RESIZE3.jpg 
I belive the speed increase is that the image is only read in once.
The big speed increase only manifests itself when I move the 1920x1080> upfront. With it done after the clone, its only a little faster than the two individual commands. Issuing the resize before the thumb creation though makes a big difference :)

What does your example show sorry? ie: What does the "-define jpeg:size=1920x1080" do?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Is it possible to produce 2 different sized images in on

Post by fmw42 »

It only reads as much as needed (only works for jpg). see http://www.imagemagick.org/Usage/formats/#jpg_read
NeilF

Re: Is it possible to produce 2 different sized images in on

Post by NeilF »

fmw42 wrote:It only reads as much as needed (only works for jpg). see http://www.imagemagick.org/Usage/formats/#jpg_read
Cheers! I had a quick test but didn't notice my 3 seconds reduce down... I'll give it some more tests...

But to be honest my 5-6 seconds for producing the two images, has now reduced down to about 3 with the single command doing it, which is a great increase already :)
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Is it possible to produce 2 different sized images in on

Post by anthony »

A couple of final points to think about. And ones that are repeated many times

use -thumbnail rather than -resize... This strips the meta-data and profiles from the jpeg making the later images much much smaller. It also removes details such as GPS location focus, time stamps etc, which is generally not wanted or needed on smaller images (typically used for web work).

using -define jpeg:size=??? is equivalent to doing a -sample (see Resize sample) better to use it at twice the resize that will be involved.

However in your case it may not see much improvement simply because your first resize is quite large, as such it probably ends up reading in the whole image in any case. The Jpeg size hint is more for generating very much smaller (thumbnail) images. Such as in your original second (very small) image command. As you are doing the two steps together, it would not have as big an advantage.

Finally the reason the original example has the resize AFTER the parenthesis, is so each of the two images are generated directly from the original. Processing one image, then re-processing the image again, while faster (at least in this case), may not produce the best quality. Though considering you are doing resizing, with large resize differences, it should not be a problem, at least not in this case.

See the top section of Thumbnailing Images for more info.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply