JPG Quality
July 27, 2005The age old choice between image quality and file size, thanks to our friend JPG.
One from the inbox this morning. I suppose it couldn’t hurt to share my response here:
Let’s say you’ve a JPEG file, a photo straight from your camera-phone, and its filesize is 32k. You open that file into Photoshop, don’t make any changes to it, and then want to save it again (don’t ask why, you just do). You’re asked to choose a quality/compression level. You select one, and save your new image over the old one.
Now, my question is: how do you save the file so that it’s not loosing any more detail to the JPEG compression algorithm, but at the same time you’re not tripling the file size?
Short answer: you can’t.
Because JPG is a lossy compression, no matter what you do with the settings, if you open up a file and re-save it as a JPG, you will lose image quality (whether you perceive it or not).
When you open a JPG, it’s converted to the internal bitmapping model of the editor (we’ll again assume Photoshop here) and converted to a full, uncompressed 24-bit image. From that point on it’s lossless — aside from any quality degradation from the camera’s original save — until the point when you save it again.
The JPG algorithm isn’t reversible so nothing from the original save influences the new save, which is why you won’t be able to achieve an identical file size or quality level. Instead, Photoshop recalculates the compression as if it’s working on an uncompressed image, which it technically is. Therefore the results you get when saving are entirely dependent on Photoshop’s JPG saving options.
The choice between a huge file size or better image quality is simply a consequence of using a lossy format. The only way to maintain the integrity of the image after the save is to choose a lossless format like PNG or TIF, but then your file sizes suffer. The only way to keep file size down is to drop the quality settings, but then your image suffers.
But hey, we’ve been dealing with that choice on the web for years now anyway.
Exactly. This is the reason that I shoot in RAW for all my photos. (that and the magic that can be done with exposure with RAW). Though the camera’s RAW images are considerably larger, you can use Adobe’s DNG format to losslessly compress your images - I use that for my Canon RAW images and save considerable amounts of space.
When I got my first Digi-Cam capable of shooting RAW, I wasn’t instantly converted. After experimenting I came to the conclusion that shooting with RAW is best. (my conclusions: http://www.californiafox.com/notebook/photography_raw_versus_jpeg_f.php )
(Photoshop) File > Save for Web…
That should do the trick.
I got my image down from 110kb to 22kb.
Although JPEG is a lossy format, that’s not always an issue. Chris (#6) mentioned using Photoshop’s Save For Web, which I think is great.
The truth is, that although the image is compressed, and data is lost, it isn’t always perceptible, and then one can usually push it a bit more since the eye filters out a fair amount of noise in images.
I think anyone who deals with graphics should do a quick readup on lossy formats like JPEG and lossless formats like PNG and GIF.
.adam
http://www.faqs.org/faqs/compression-faq/part2/ has a reasonable description of the full JPEG algorithm (no actual maths). Note that it’s not obvious that a jpeg->jpeg conversion has to loose quality (assuming the quality settings are exactly the same in the first and second saves) because although the initial transformation to jpeg will loose some information irretreviably it looks (to my quick read) like a second transformation would “loose” exactly the same information that’s already been discarded (unless the converter just keeps chopping away at things like the luminance). But the decompressor can add extra steps (e.g. smoothing) so that compression and decompression are not reversible and in that case the quality would get worse at constant file size. Moreover much of the algorithm seems to be implementable in a variety of ways and switching between a digital camera with one implementation and photoshop (other bitmap editing packages are, as they say on the BBC, avaliable) with another could cause further problems.
Funny. I too have pondered the same thing for so many years, but don’t really do anything about it.
As far as what I’m doing? Basically, it just comes down to whether or not this is going on the web, or if I’m printing it. The choice then, becomes obvious.
Dave…
What about immediately saving the JPG as a PS doc upon opening? Then do whatever you need to do to it, so the next time you spit it out (as a JPG) you’re not saving JPG to JPG but actually PSD to JPG.
I remember reading something about this a while back, at least this is the workflow I use when they absolutely must use a JPG.
For the record, I always shoot raw files…
As some other people mentioned, I guess what you said is not true. As far as I know JPEG compression loses high frequency detailed (DCT base compression) and so if you choose a better quality you will not lose more.
Doesn’t JPEG2 offer some solutions to these problems with the original JPEG spec - what’s browser support for this format like these days?
There are also a limited set of operations (I think flipping and rotating images were examples) which can be performed without the decompressing/recompressing steps, making them nondestructive.
Off topic but the same applies to MP3, which is also a lossy format.
Re-encoding your MP3 files at a high bitrate won’t make them sound better and you’ll actually lose some quality.
As with all things to be posted on the internet, store the source in lossless format (PSD in the case of images) and export them in a lossy format if you want to save bandwidth.
It’s important to remember that it’s just the jpeg -> bitmap -> jpeg that causes the loss. There’s specialized lossless algorithms for rotations and color shifts in a lot of image viewers.
Hmm, nice to finally see how JPG really works.
I’ve been experiencing this JPG loss-loss problem for years, too. Even if you save the file and it’s 200% bigger in filesize, it will still look worse.
Only option is a 2gb memory card and a digital camera capable of saving raw images. Gets quite expensive though ;)
RAW really is the best format for shooting, if you have that choice. That puts all compression choices firmly in your own hands, rather than having the camera do it for you. It’s more work, but if image quality is important, it’s worth it.
With the support now offered in photo organization programs - like iPhoto and Photoshop Album - using RAW images is far easier than it used to be.
Sure, it takes up a bit more space, but when you can get external hard drives for less than $1 per gig, storing the larger images costs far less than one might think.
As Joel Bernstein pointed out, there are indeed operations (cropping, rotating, inserting bitmap with partial recompress) that can be done without loss of quality, though not much tools exist to do that ( Aurigma Graphics Mill being among them, http://www.origma.ru/Support/Help/GraphicsMilldotNET/PhotoImaging.htm ), so, if you find yourself in a need to do some simple operations without quality loss you can take a look at it. Though, as I think that you will post-process your photos, this may not be of much use to you :)
There’s a very good FAQ on jpeg and jpeg compression, dating back a few years, at http://www.faqs.org/faqs/jpeg-faq/part1/
Item 10 is especially relevant here. A partial quote -
“It turns out that if you decompress and recompress an image at the same quality setting first used, relatively little further degradation occurs. This means that you can make local modifications to a JPEG image without material degradation of other areas of the image. (The areas you change will still degrade, however.) Counterintuitively, this works better the lower the quality setting. But you must use *exactly* the same setting, or all bets are off. Also, the decompressed image must be saved in a full-color format; if you do something like JPEG=>GIF=>JPEG, the color quantization step loses lots of information.
Unfortunately, cropping doesn’t count as a local change! JPEG processes the image in small blocks, and cropping usually moves the block boundaries, so that the image looks completely different to JPEG. You can take advantage of the low-degradation behavior if you are careful to crop the top and left margins only by a multiple of the block size (typically 16 pixels), so that the remaining blocks start in the same places. (True lossless cropping is possible under the same restrictions about where to crop, but again this requires specialized software.)
The bottom line is that JPEG is a useful format for compact storage and transmission of images, but you don’t want to use it as an intermediate format for sequences of image manipulation steps. Use a lossless 24-bit format (PNG, TIFF, PPM, etc) while working on the image, then JPEG it when you are ready to file it away or send it out on the net. If you expect to edit your image again in the future, keep a lossless master copy to work from. The JPEG you put up on your Web site should be a derived copy, not your editing master.”
A lot of JPG’s position is due to that it has been around since the web started and it was supported by the browsers from day one of the web. It is hard for a new and better algorithm to break through and become de facto.
Ok … and so?
PS: There is a beautiful “Save for Web” option in Adobe Photoshop :)
Don’t want to be pedantic, but I’ll look like I am anyway.
You ‘lose’ image quality - loose laces will trip you up - and all this as a comment about a lossy file format.
My image manipulation usually involves overly big JPGs from digital cameras, intermediate stages of the process are in The GIMP’s natural format of XCF - which like PSD has layers and other goodies - then you save to PNG / JPG [ or GIF if you’re desparate ] for the web.
As someone else has rightly pointed out :
desparate != desperate
my very own misspelling : ]
I stopped keeping things as JPGs a while back. Now all my images get saved as PSDs with the RAW (or sometimes JPG) originals archived just in case. My photos folder is just starting to approach my music folder in size… Scary!
I could do with a decent online service to archive all the originals. I’ve had so much hardware trouble that I just don’t trust my current method of mirroring my HD to an external drive.
The use of the word “loose” when you mean “lose” is very annoying. Not only does it occur in comment 8, but in the quoted email in the main post! I have seen this mistake so often that I can only conclude it is something our minds cannot easily fathom. Either that or people are reading a lot less, which I doubt.
Back on topic, the JPEG2000 format features a lossless setting. Of course no browsers support it yet, I believe.
The MaPiVi Picture viewer and organizer for UNIX, Mac OS X and Windows is able to do lossless rotation and cropping.
MaPiVi is a stand alone tool, there is no need for a web server, online access or a database.
I think that the original question was a great one! But if you’re cropping, or rotating a picture look at MaPiVi:
http://mapivi.sourceforge.net/mapivi.shtml
Gif is not lossless if you have a picture taken from a camera, as you talk about in your example. Gif is even worse than JPEG as it can store, what is it, 255 colors maximum.
PNG on the other hand is ok, in that it can save losslessly, but Photoshop doesn’t do a good job saving it - there are different compression techniques when saving as PNG. Try PNG Crush, and I think you’ll get both your quality and file-size.
One thing - there are some reviews of PNG Crush around the net stating that it doesn’t compress much more than photoshop - this is not correct - it all depends on the image. I tried images (100x50 approx.) featuring semi- and non-transparent pixels and the resulting size was 33% of photoshop’s image.
Google
define:jpeg => Joint Photographic Experts Group
Always thought about this issue when resaving a jpeg-file. But was a nice read and something I will remember everytime I do save another one ; )
I found this handy little tool to show show how much image information has been lost with different levels of compression or different formats. It’s free too, which is nice, but Windows only.
http://www.imagediff.com
When I compare images I look at how much the Color information has changed rather than the Pixel percentage to gauge the relative quality of the compression.
Just a note that gif files can be saved with the same quality if the same color settings are used on the save.
http://www.betterjpeg.com/
ep! it ownz! (;
I was using a program by Ulead a few years back… it spells out like SmartSaver Pro (used version 3.0). It is the best gif/jpeg optimizer I’ve seen and I haven’t seen or heard any application that compares.
Jpeg is a cool format. Why replace it? If it is not broken, embrace it!
People have been posting here their personal views about how JPEG works and whether it loses information or not when they do the JPEG->lossless->JPEG thing.
The answer to this question is not a matter of personal view, but the sad truth is: it depends. And most of times this translates to ‘yes, you will lose quality’.
As pooya (#11) said, JPEG compression loses high frequency detail by eliminating DCT transform coefficients. Which coefficients are discarded depends on the quality setting you choose AND on the encoder design. The truth is different encoders make different choices as to what will be discarded. The JPEG standard suggests using certain matrices, but encoder designers are free to make their own.
Conclusion: if you use the same quality settings AND the same encoder (or one with identical design), you won’t lose quality on unchanged blocks - like Thomas Passin (#4) said, JPEG processes the image in square blocks and each block is processed as a whole. Most of the time though, you will have absolutely no guarantee that the different encoders you are using have identical design. This means your images will lose quality.
The same can be said of mp3. It has been designed to avoid quality loss on multiple encode/decode operations, but that depends on using encoders that make identical choices on what to eliminate when compressing.
Cheers,
Carlos
In case this is news to anybody, and hopefully helpful to someone, there are SLR and all-in-one cameras that support TIFF in addition to or instead of RAW. My Olympus UZ700 lets me shoot in TIFF (but not RAW).
Shooting in RAW requires you to use a utility provided by the camera maker to convert to a usable format before editing (unless I misunderstand, every make has their own RAW format).
Shooting in TIFF sounds like a big time saver when it actually comes to editing images in this case. The main and only disadvantage I have read about is that it usually takes quite a bit longer for a camera to write to the memory card in TIFF or RAW rather than JPG, so you’ll get fewer shots per second. My camera is old and slow so it is probably a bad indicator, but I probably get close to one shot per second (a little slower than that) in JPG while it takes somewhere between 6 and 8 seconds (I rarely use it obviously) to write a TIFF to the card.