2-colors only JPEG

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
naoliv
Posts: 110
Joined: 2007-12-10T18:54:27-07:00
Location: Brazil

2-colors only JPEG

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

Re: 2-colors only JPEG

Post 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
naoliv
Posts: 110
Joined: 2007-12-10T18:54:27-07:00
Location: Brazil

Re: 2-colors only JPEG

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

Re: 2-colors only JPEG

Post by fmw42 »

I doubt it will be possible due to the compression. You could try JP2000 (lossless jpg)
naoliv
Posts: 110
Joined: 2007-12-10T18:54:27-07:00
Location: Brazil

Re: 2-colors only JPEG

Post by naoliv »

Understood. Thank you!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 2-colors only JPEG

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: 2-colors only JPEG

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply