put JPG on diff-size background without loss of information

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
jta2010
Posts: 6
Joined: 2012-08-14T15:56:50-07:00
Authentication code: 67789

put JPG on diff-size background without loss of information

Post 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.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

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

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

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

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

Post 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.
jta2010
Posts: 6
Joined: 2012-08-14T15:56:50-07:00
Authentication code: 67789

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

Post 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.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

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