Can we change size of an JPEG image with out decoding and re-encoding it?

12

1

I want to convert a 640x480 JPEG image to 320x240. I know it can be done by decoding the JPEG image into raw YCbCr, scale it and re-encode it into JPEG. But is it possible possible with JPEG encoding to directly scale the image with out decoding?

Necktwi

Posted 2014-07-03T11:51:28.773

Reputation: 291

Answers

14

You can, but only if your JPEG viewer supports the SmartScale JPEG extension. jpegtran can do this for sizes of N/8, with N=1..16. Basically, the DCT block size is changed from 8x8 to something else during recompression (e.g. 4x4), which effectively scales the image.

Note: SmartScale was introduced in version 8 of the libjpeg library, but it's not widely supported by viewers.

A document describing the changes in detail can be found here: Evolution of JPEG.

EDIT: It seems that most viewers cannot actually display these images, as they are based on libjpeg-turbo. And libjpeg-turbo chose not to implement this feature. In fact, I've tried quite a few programs (on Ubuntu 14.04 and Windows 8) and none have been able to display the downscaled image created using jpegtrans. Even Photoshop, IrfanView and GIMP failed.

EDIT 2: In fact, Ubuntu and Fedora don't even ship the libjpeg8 library, but completely replace it with the libjpeg-turbo version. So none of the programs will be able to read JPEG SmartScale files, save for a few binaries that are statically linked to the original libjpeg8 library.

jmiserez

Posted 2014-07-03T11:51:28.773

Reputation: 977

Is there a codec that constructs the image that grows on just by appending further data to the image? – Necktwi – 2014-07-03T14:41:22.560

what about the progressive jpeg? Doesn't it does exactly what i'm asking? – Necktwi – 2014-07-03T15:44:57.903

No, that does something slightly different by increasing the quality of the image (from low to full), but the resolution is always unchanged (reference). However, there is another JPEG extension called "hierarchical mode", which encodes different resolutions (see also here). But again, libjpeg-turbo doesn't support it (see here, search for "progressive" in the text).

– jmiserez – 2014-07-03T17:54:21.957

1

And to answer the first comment, of course there are such codecs. Just not JPG. What you are looking for is called an image pyramid, or multiscale representation. E.g. JPEG2000 uses a wavelet transform that creates a wavelet pyramid. There are other pyramids, such as Gaussian or Laplacian pyramids. This slideset gives a good overview on how things work, while JPEG2000 is discussed here and here.

– jmiserez – 2014-07-03T18:10:49.353

Thanks for the information about the new JPEG formats. I don't expect many source images (cameras, scanners, etc) to use them yet. – AFH – 2014-07-14T11:01:50.420

3

The short answer is no. A JPEG image uses compression, which means that each output byte depends on all the others. If you change the number of image bytes, then you must decompress and recompress.

There will be a loss of quality as a result of the recompression, since JPEG uses lossy compression, but you are losing quality anyway by quartering the resolution. You can get the best possible quality from the low-resolution image by increasing the JPEG quality level when recompressing, though this will of course increase the file size.

If you do a lot of work with images, it is best to work in a lossless compression format, such as PNG, converting to JPEG only when images are finalised, provided of course that you have the extra disc space.

AFH

Posted 2014-07-03T11:51:28.773

Reputation: 15 470