Colors for very small JPEG images are way off: bug or feature?

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Locked
pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Colors for very small JPEG images are way off: bug or feature?

Post by pipitas »

I noticed that JPEGs do have colors which are very far off from the originals, if the image dimensions are very small. (It happened when I scaled down a larger image.)

To test this further, I created a very small, 2x2 pixel image with the following command:

PNG:

Code: Select all

 convert                                  \
     -size 1x1                            \
        \( xc:red  xc:green +append \)    \
        \( xc:blue xc:black +append \)    \
     -append                              \
       2x2-pixels-red-green-blue-black.png
Then I converted the PNG to JPEG.

JPEG:

Code: Select all

  convert 2x2-pixels-red-green-blue-black.png 2x2-pixels-red-green-blue-black---converted-from-png.jpeg
Inspecting these two images visually shows the "wrong" colors in the JPEG:

Code: Select all

 convert 2x2-pixels-red-green-blue-black---converted-from-png.jpeg -scale 10000% show:
 convert 2x2-pixels-red-green-blue-black.png  -scale 10000% show:
This is (of course) confirmed by outputting the image colors in "txt" format.

PNG:

Code: Select all

 convert 2x2-pixels-red-green-blue-black.png txt:-
     # ImageMagick pixel enumeration: 2,2,255,srgb
     0,0: (255,0,0)  #FF0000  red
     1,0: (0,128,0)  #008000  green
     0,1: (0,0,255)  #0000FF  blue
     1,1: (0,0,0)    #000000  black
JPEG:

Code: Select all

 convert 2x2-pixels-red-green-blue-black---converted-from-png.jpeg txt:-
     # ImageMagick pixel enumeration: 2,2,255,srgb
     0,0: (239,0,155)  #EF009B  srgb(239,0,155)
     1,0: (87,63,53)   #573F35  srgb(87,63,53)
     0,1: (69,0,141)   #45008D  srgb(69,0,141)
     1,1: (0,0,29)     #00001D  srgb(0,0,29)
Instead of converting the JPEG image from a PNG input, I tried a direct generation method:

JPEG:

Code: Select all

 convert                                  \
     -size 1x1                            \
        \( xc:red  xc:green +append \)    \
        \( xc:blue xc:black +append \)    \
     -append                              \
       2x2-pixels-red-green-blue-black---directly-generated.jpeg
But nope!, this one has still the exact same color values as the "converted-from-png" one.
  • Question 1: Is this a bug or a feature? ("Feature" as in this being a side-effect of the JPEG compression, which doesn't work for small dimensions anyway...)
  • Question 2: If this wrong colors cannot be avoided when using the methods shown above -- how else can I create a 2x2 pixels JPEG that returns the exact same color values as the PNG does ?!?

pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: Colors for very small JPEG images are way off: bug or feature?

Post by pipitas »

Nevermind... for now I succeeded with a "good enough" result by adding

Code: Select all

 -quality 100%
to the JPEG creation commands. Now the resulting color values are:

Code: Select all

  # ImageMagick pixel enumeration: 2,2,255,srgb
  0,0: (254,0,0)  #FE0000  srgb(254,0,0)
  1,0: (0,128,1)  #008001  srgb(0,128,1)
  0,1: (0,0,254)  #0000FE  srgb(0,0,254)
  1,1: (0,0,0)    #000000  black
These values are only a very small distance off from the real values in the red, green and blue pixels (black is perfect).

----

Now only a bonus question remains: is there a method to get the perfect match to the PNG color values?

User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Colors for very small JPEG images are way off: bug or feature?

Post by glennrp »

That is the nature of JPEG. Bonus answer: No. Extra info: You probably wanted "lime" rather than "green".

pipitas
Posts: 168
Joined: 2012-07-15T14:06:46-07:00
Authentication code: 15

Re: Colors for very small JPEG images are way off: bug or feature?

Post by pipitas »

glennrp wrote:That is the nature of JPEG.
I know JPEG is lossy....
glennrp wrote:Bonus answer: No.
...but I don't get this, to be honest. After all, these are only 4 pixels, all in all. So there is no way in JPEG to precisely describe the colors for each single one of only 4 pixels in a very small image, "losslessly" if you like, at the cost an increased file size for the JPEG (making it eve bigger than the matching PNG or GIF) if need be?
glennrp wrote:Extra info: You probably wanted "lime" rather than "green".
Indeed!

Thanks, Glenn!

User avatar
fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colors for very small JPEG images are way off: bug or feature?

Post by fmw42 »

I believe jpeg compression uses blocks of 8x8 pixels. I am not sure how it would handle an image of of only 4 pixels.

Locked