Page 1 of 1

2-colors only JPEG

Posted: 2010-11-10T17:27:15-07:00
by naoliv
It seems that it's not possible to generate a JPEG file with exactly two colors only (with RGB #000000 and RGB #FFFFFF only) in all cases, due to limitations in JPEG itself (since it's a lossy format).
For example, it's possible to convert this PNG file into a exactly 2-colors only JPEG file.

identify -verbose gives for both files:

Code: Select all

  Histogram:
    153600: (  0,  0,  0) #000000 black
    153600: (255,255,255) #FFFFFF white
However, using http://upload.wikimedia.org/wikipedia/e ... graphy.png as a second example, and trying to also get a 2-colors only JPEG file, it seems that it's not possible, even after trying with:

convert -colors 2 Munch_The_Scream_lithography.png Munch_The_Scream_lithography.jpg or
convert -depth 1 Munch_The_Scream_lithography.png Munch_The_Scream_lithography.jpg or
convert -monochrome Munch_The_Scream_lithography.png Munch_The_Scream_lithography.jpg

The result is a color count of 29.

Is it right to affirm that it won't be possible to have the desired result (for the second case) because the way that JPEG works won't allow this?

Re: 2-colors only JPEG

Posted: 2010-11-10T18:18:59-07:00
by fmw42
First, your syntax is not current. It should be in this case

convert input options output


The following gives me 4 colors 0,1,254,255

convert Munch_The_Scream_lithography.png +dither -colors 2 -quality 100 -type bilevel Munch_The_Scream_lithography1.jpg

Histogram:
409277: ( 0, 0, 0) #000000 black
7985: ( 1, 1, 1) #010101 rgb(1,1,1)
7605: (254,254,254) #FEFEFE rgb(254,254,254)
412123: (255,255,255) #FFFFFF white

Filesize: 523KBB

But why not just use gif, it gets smaller filesize

convert Munch_The_Scream_lithography.png +dither -colors 2 -type bilevel Munch_The_Scream_lithography1.gif

Histogram:
417262: ( 0, 0, 0) #000000 black
419728: (255,255,255) #FFFFFF white

Filesize: 43.5KBB

Re: 2-colors only JPEG

Posted: 2010-11-10T18:25:54-07:00
by naoliv
fmw42 wrote:The following gives me 4 colors 0,1,254,255

convert Munch_The_Scream_lithography.png +dither -colors 2 -quality 100 -type bilevel Munch_The_Scream_lithography1.jpg
So it's not possible to get 2 colors?
fmw42 wrote:But why not just use gif, it gets smaller filesize
Actually it's a bug report (http://bugs.debian.org/603097) from an user that wants to have a JPEG file with exactly 2 colors.

Re: 2-colors only JPEG

Posted: 2010-11-10T18:34:16-07:00
by fmw42
I doubt it will be possible due to the compression. You could try JP2000 (lossless jpg)

Re: 2-colors only JPEG

Posted: 2010-11-10T18:41:37-07:00
by naoliv
Understood. Thank you!

Re: 2-colors only JPEG

Posted: 2010-11-10T19:54:29-07:00
by fmw42
I am not an expert on jpg, so my comments were just my estimation. Perhaps one of the IM developers can clarify for you more definitively.

Re: 2-colors only JPEG

Posted: 2010-11-10T20:46:51-07:00
by anthony
To clarify things. Especially in terms that fwm42 understands.

JPEG is lossy, it will not give back the exact same colors that was saved.

The reason is that it will take each 8x8 pixel square in the image can convert it into the frequency domain, using a Fast fourier transform. It then compresses the small square of data by removing the smaller frequencies, and thus reducing the data size without lossing too much of the way it looks.

But some data is lost and destroyed, as such when the image is restored by reversing the process, colors will not be exact.

The lossy nature will appear in the form of 'ringing' at sharp boundaries, which generate lots of high frequencies.
And in '8x8 square block' type of effects on the image as quality become low and repetitive save/read cycles increase.

You can see some of these effects in the examples shown in IM examples. JPEG Compression.
http://www.imagemagick.org/Usage/formats/#jpg