Combine jpeg color maps

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
danieljsinclair
Posts: 5
Joined: 2012-01-11T08:59:05-07:00
Authentication code: 8675308

Combine jpeg color maps

Post by danieljsinclair »

I'm trying to combine two jpegs into a single image strip (think sprites) with a single shared palette in the hope of saving space under compression. I then want to be able to extract those two images into new files losslessly. Can anyone help me with the windows syntax for this? Essentially;


1) image1.jpg|image2.jpg --> uniqueColors.map

2) image1.jpg + image2.jpg | uniqueColors.map --> tiledImage.jpg

3) split tiledImage.jpg into newImage1.jpg, newImage2.jpg.


I've got quite close with various convert and montage commands, but everything I end up with is lower in quality and much larger than the sum of the file parts. The images I've got appear very, very similar but they're not based on the same image. They're actually a picture of the same item taken at slightly different angles, so a mathematical pixel/pixel subtraction so they don't overlay at all.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine jpeg color maps

Post by fmw42 »

IM will compress and recompress your jpgs. So you should try using some other tool. see http://www.imagemagick.org/script/comma ... hp#quality

I am not sure what you are trying to do exactly, but you could append the two jpgs and save as 256 colors gif

convert image1 image2 +append -colors 256 result.gif

Then use that for you the color map. See -map (-remap) http://www.imagemagick.org/Usage/quantize/#remap_common
danieljsinclair
Posts: 5
Joined: 2012-01-11T08:59:05-07:00
Authentication code: 8675308

Re: Combine jpeg color maps

Post by danieljsinclair »

Well I've been reading the other similar posts and I've tried following those but the results I get are surprising. For instance, I understand (now) that by default, tiling two JPEGS would harmonize the color table in any case, but the resulting file is around 10% bigger than the sum of it's parts.

I don't want to go to GIF because that doesn't retain the fidelity of the original JPEGS which I later want to extract.

Perhaps I should explain the problem rather than bring a solution I can't get to work;

I want to improve the compression ratio of multiple JPEGS for archiving. I figured that by combining them into a single JPEG I'd get savings in the color table not to mention in compression, *as long as* they were very similar. Here are my two test images;

https://plus.google.com/photos/10166861 ... ys7J5Jqddw

I first thought about using a delta style compression like MPEG but that isn't really suitable. It's designed for moving images so too lossy for archiving photos. I couldn't find a lossless video encoder in ImageMagick and nothing I tried worked very well.

Next I tried creating a static difference file with ImageMagick where image1.jpg+image2Delta.diff == image2.jpg but that didn't get me anywhere.

So finally, I just wondered if tiling images together as I've described above might work, but everything I try results in poorer quality, but larger images.

What I ultimately want to do is reduce the storage requirements for multiple images where there's a lot of similarity but be able to reproduce/extract the original images to the same quality.

It's possible for instance that if I could harmonize the color tables between two similar JPEGs files that when both were simply thrown into a ZIP archive in the usual way that I'd get better compression. That would depend of course on whether the archiver used the same dictionary across files - I'm not sure that it does.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Combine jpeg color maps

Post by fmw42 »

Are you using 8-bit 256 color jpgs? Most jpgs that have any quality are using 24-bits.

The reason you lose quality and gain size is that IM will decompress the images before appending them and then recompress the output. Thus you lose quality by the fact that you are decompressing and then recompressing. Furthermore, IM will use the first images quality if available or else quality=92, which may be more than what you started with. So the get lower files sizes add -quality and try different values until you get the size you want. However, the images will still lose quality due the decompress and recompress as jpg is a lossy compression.

convert image1 image2 +append -quality XX result.

For lossless jpgs, see JPEG 2000 (.jp2) using the Jasper delegate library.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Combine jpeg color maps

Post by glennrp »

danieljsinclair wrote:I'm trying to combine two jpegs into a single image strip (think sprites) with a single shared palette in the hope of saving space under compression.
You are going down a blind alley. JPEG does not use colormaps. Color JPEGs use 24 bits and are not 256-color. About all you can do is to reduce the "quality" until you
can't stand the image degradation any more. It is said that the new WebP format offers somewhat better compression, but I have not tested that much. By the way, your approach does
work with indexed PNGs; a strip of PNGs will be encoded with one PLTE chunk, as long as there are a total of 256 or fewer colors present in the strip.
danieljsinclair
Posts: 5
Joined: 2012-01-11T08:59:05-07:00
Authentication code: 8675308

Re: Combine jpeg color maps

Post by danieljsinclair »

Doh. Thanks - you're absolutely right of course, I should have remembered that about JPEGs.

A PNG approach would be worth testing, but presumably a conversion between JPG and PNG wouldn't be an isomorphic lossless conversion.

I wonder if saving JPEG files *without* compression first and then compressing the multiple files into a single archive might provide better results.
danieljsinclair
Posts: 5
Joined: 2012-01-11T08:59:05-07:00
Authentication code: 8675308

Re: Combine jpeg color maps

Post by danieljsinclair »

Answering myself;

...except that JPEGs aren't compressed as such, like you said previously, there's just a knob for quality. Without the raw pre-jpegged files to hand, there's probably little that can be done to reduce storage even across files without losing quality.

They're probably already as compressed as they can possibly be :(
danieljsinclair
Posts: 5
Joined: 2012-01-11T08:59:05-07:00
Authentication code: 8675308

Re: Combine jpeg color maps

Post by danieljsinclair »

In case anyone else finds their way to this thread, look at jpegtran which recompresses jpg files and also packjpg which creates jpeg archive files which are around 30% better than the original file. The original file can be extracted as JPG byte for byte.
Post Reply