resize to make a JPG *bigger* (surround with whitespace)

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
jta2012
Posts: 4
Joined: 2012-09-21T11:17:14-07:00
Authentication code: 67789

resize to make a JPG *bigger* (surround with whitespace)

Post by jta2012 »

Hi friends,

I want to take an arbitrary hi-res JPG (say 4000 x 3000 pixels) and make its pixel size *larger* while *changing its aspect ratio*. The new JPG must exactly match the aspect ratio of a particular print size, say 10x8.

For example, 4000x3000 native JPG printed to 10"x8" using shrink-to-fit logic... 10x8 == 40x32 == 4000x3200 so I take my original JPG 4000x3000 and add two white strips 100px wide to either side of the short dimension, so final JPG is grown 4000x3200. But how to best do this?

The larger JPG will be submitted to be printed automatically by one of ~20 print labs, whose APIs don't offer shrink-to-fit and are unlikely to, so we want to build a JPG ourselves, done right, and submit that. The end-customer will be paying in some cases $100's per print so preserving quality is important... my earlier tests of creating a blank white 4000x3200 JPG and then stacking the original atop that raised concerns because the final JPG was so much smaller in bytes than our original, and the final JPG had the null metadata of my blank 4000x3200, rather than the IPTC/Exif/XMP of the original. I suspect the original metadata might include hints like colorspace which are needed to produce the best possible print.

I guess I'm looking for ideas on how to grow the original JPG by N white pixels in a particular dimension, so the final JPG is of same or larger byte size, and preserves its original metadata & internal JPG data as much as possible. Ideas? Something like:

mogrify original.jpg -resize 4000!x3200! -fill white

Thanks in advance,
R
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: resize to make a JPG *bigger* (surround with whitespace)

Post by fmw42 »

See -splice and -border at
http://www.imagemagick.org/Usage/crop/#border
http://www.imagemagick.org/Usage/crop/#splice

To get the aspect ratio you want, you will want to compute the number of pixels in W and H corresponding to the density you want to achieve 10x8 inches at X dpi from your image Width and Height
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: resize to make a JPG *bigger* (surround with whitespace)

Post by GeeMack »

jta2012 wrote: 2017-05-19T11:42:57-07:00For example, 4000x3000 native JPG printed to 10"x8" using shrink-to-fit logic... 10x8 == 40x32 == 4000x3200 so I take my original JPG 4000x3000 and add two white strips 100px wide to either side of the short dimension, so final JPG is grown 4000x3200. [...] Ideas?
Using IM6 and running this command from a Windows CMD prompt you can pad any input image to make its aspect 8:10 (or 10:8 depending in the orientation). I'd put it in a script and work it from a FOR loop to do multiple files.

Code: Select all

set INIMAGE=inputpic.jpg

convert ^
   %INIMAGE% ^
   -alpha set ^
   -write mpr:input ^
   -set filename:f "_%[t] [%[fx:w<h?8:10]x%[fx:w<h?10:8]]" ^
   -fill white ^
   -colorize 100 ^
   -set option:distort:viewport "%[w]x%[fx:w>h&&w/10>h/8?w/10*8:w<h&&h/10<w/8?w/8*10:h]" ^
   -distort SRT 0 ^
   -set option:distort:viewport "%[fx:w<h&&h/10>w/8?h/10*8:w>=h&&w/10<h/8?h/8*10:w]x%[h]" ^
   -distort SRT 0 ^
   mpr:input ^
   -gravity center ^
   -composite ^
      "%[filename:f].png"
To run it from a BAT script, the single percent signs "%" would need to be made into doubles "%%" — except the ones around SET variables like "%INIMAGE%".

To make this into a *nix shell or script command you need to change the continued-line carets "^" into backslashes "\", set the input file variable a different way, and possibly escape some other special characters. I can't test it on a *nix system right now.

This outputs a PNG file with the input file name and 8x10 or 10x8 in square brackets like this "input [10x8].png". Change the last line if you want it to output a JPG. Any additional tweaking can be done after the final "-composite" and before the output file name. As far as retaining other metadata, I'm not sure what might remain with a command like this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: resize to make a JPG *bigger* (surround with whitespace)

Post by fmw42 »

Suppose you have a 4000x3000 pixel image and want 10x8 inches printed.

4000/10=400 dpi
3000/8=375 dpi

So at 400 dpi, you would need an image that was 10*400=4000 px in width and 8*400=3200 px in height. The height is too big for your image, so you need to resize by 3000/3200=93.75%. That would make the width 4000*0.9375 = 3750 and the height 3200*0.9375=3000

This can be done as

Code: Select all

convert image.jpg -resize 93.75% -background white -gravity center -extent 3750x3000 -units pixelsperinch -density 375 result.jpg
Thus 3750/375=10 and 3000/375=8 and the aspect ratio = 3750/3000 = 1.25 = 10/8
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: resize to make a JPG *bigger* (surround with whitespace)

Post by snibgo »

jta2012 wrote:... preserving quality is important...
Your inputs are JPG, and so are your outputs. The writing will be lossy -- it will change values. JPEG compression works well for photos, not solid colours, so the white margins won't be exactly white (but this probably won't matter).

By default, the output will have the same quality setting as the input. "-quality 100" will change the input by the smallest amount, but will give larger file sizes.
snibgo's IM pages: im.snibgo.com
Post Reply