Colors for very small JPEG images are way off: bug or feature?
Posted: 2015-02-23T03:16:50-07:00
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:
Then I converted the PNG to JPEG.
JPEG:
Inspecting these two images visually shows the "wrong" colors in the JPEG:
This is (of course) confirmed by outputting the image colors in "txt" format.
PNG:
JPEG:
Instead of converting the JPEG image from a PNG input, I tried a direct generation method:
JPEG:
But nope!, this one has still the exact same color values as the "converted-from-png" one.
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
JPEG:
Code: Select all
convert 2x2-pixels-red-green-blue-black.png 2x2-pixels-red-green-blue-black---converted-from-png.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:
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
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)
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
- 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 ?!?