Page 1 of 1

put JPG on diff-size background without loss of information

Posted: 2014-01-10T13:11:08-07:00
by jta2010
Hi all,

I want to take a huge JPG and create a new, larger JPG consisting of the original JPG surrounded by a solid-color border. The new JPG should have the same IPTC metadata as the original (especially same colorspace). There should be no loss of quality for the original image in the center. Can it be done?

I've tried creating a solid-color background image, and then composing the original on top of it, but this loses my IPTC metadata, and the final image has a 5% lower byte size than the original (suggesting some loss of quality). Here's what I've tried:

K:\>identify rainbow.jpg
rainbow.jpg JPEG 3008x1960 3008x1960+0+0 8-bit sRGB 1.188MB 0.000u 0:00.001

K:\>convert -resize 4000x4000 xc:white back.jpg

K:\>composite -gravity center rainbow.jpg back.jpg out.jpg

K:\>identify out.jpg
out.jpg JPEG 4000x4000 4000x4000+0+0 8-bit sRGB 1.146MB 0.000u 0:00.000

NOTE: final 1.146MB smaller than original 1.188MB, despite massive larger px size

Is there some better technique to use here? I also tried "convert -resize 4000x4000! -fill white original.jpg out.jpg" but it stretches/distorts my source image, instead of just surrounding it with a border.

Our scenario is that our API sends photo JPGs to dozens of different print labs. Our convention has always been that if the image aspect ratio doesn't exactly match the print aspect ratio, the lab should crop-to-fit. We now have some images for which we want to shrink the image and fill with a background color, so that nothing is ever cropped if the image-size & print-size don't match. These are huge & expensive prints, so I want to avoid quality loss, and want to avoid losing any IPTC metadata which the print lab might require for doing the best-quality print.

Re: put JPG on diff-size background without loss of informat

Posted: 2014-01-10T13:50:34-07:00
by GreenKoopa
Have you looked at -extent? It doesn't require a background image and should keep your metadata.
http://www.imagemagick.org/script/comma ... php#extent
http://www.imagemagick.org/Usage/crop/#extent

Re: put JPG on diff-size background without loss of informat

Posted: 2014-01-10T13:58:35-07:00
by snibgo
The main problem is that JPEG is a terrible compression method for flat colour. Your print labs should be able to accept other formats, especially if these are large and expensive prints. JP2 may be a good format for you. Alternatives are PNG or TIFF with a lossless compression such as zip but not jpeg.

For the border: if you compose your image on top of another, you'll get the metadata of the "other". You could use "-compose Dst_Over" but "-border" or "-extent" seem obvious choices.

You may find that some metadata isn't copied, eg camera settings. If you want it, use exiftool to copy it.

Re: put JPG on diff-size background without loss of informat

Posted: 2014-01-10T14:14:15-07:00
by GreenKoopa
With ImageMagick, as with most image tools, simply opening and resaving a jpeg results in recompression and a tiny, often imperceptible, loss of quality. If you must use jpeg and really need zero loss, jpegtran is a specialized tool that may help. Specifically, look at crop 'n' drop. jpegtran can avoid loss of quality, keep your files as small as possible while doing so, and should be faster. It isn't too difficult to learn, but it is hugely more limited than IM.
jpegclub.org/jpegtran/ wrote: Here is a sample output image and an equivalent shell script producing the same image using one temporary file.

Re: put JPG on diff-size background without loss of informat

Posted: 2014-01-10T14:54:35-07:00
by jta2010
Thanks so much for the quick replies!! -extent was what I was looking for... it should meet our needs.

When I test the trivial case of using -extent to output the exact same size as the original, my file size GROWS by 9%, instead of shrinking by 5%. At least that could be attributed to data duplication (imperfect compression), whereas a shrinking file points pretty clearly to information loss.

Totally understand/agree about the issues with JPG format... some loss will happen, but I just want to make sure we're using a reasonable technique.

Re: put JPG on diff-size background without loss of informat

Posted: 2014-01-10T15:01:16-07:00
by GreenKoopa
Some of your shrinkage could have been the loss of metadata. Rather than judging by file size, you can use IM's compare to quantify the loss.

Re: put JPG on diff-size background without loss of informat

Posted: 2014-01-10T18:38:58-07:00
by fmw42
IM will always decompress and then recompress the jpg. So some file size change can be associated with different jpg compression tables and/or amount of compression.