Converting from png to jpeg using image.Decode and jpeg.Encode produces incorrect colors

1,978 views
Skip to first unread message

Viktor Kojouharov

unread,
Dec 12, 2015, 7:52:42 AM12/12/15
to golang-nuts
I'm trying to convert pngs to jpegs using go's image package. The output images are jpegs, but the color is all messed up.

Consider the following sample code and two input images (4x4):


The resulting 'out.jpg' has incorrect colors, whereas 'out.png' retains the proper ones. It seems the colors get messed up during decoding. Is there any way to preserve or convert them to their proper values?

Frits van Bommel

unread,
Dec 12, 2015, 8:10:32 AM12/12/15
to golang-nuts
On Saturday, December 12, 2015 at 1:52:42 PM UTC+1, Viktor Kojouharov wrote:
Consider the following sample code and two input images (4x4):

Those last two URLs are exactly the same. Copy-paste failure?

Viktor Kojouharov

unread,
Dec 12, 2015, 8:12:00 AM12/12/15
to golang-nuts
http://i.imgur.com/VeFtbKu.png - test.png

it's so easy to mess up these urls

Viktor Kojouharov

unread,
Dec 12, 2015, 8:29:53 AM12/12/15
to golang-nuts
Also, creating a new gif input image (same color as the others) and coverting it to jpeg produces messed up colors as well.

png -> gif and gif -> png is fine, as well as jpg -> gif and jpg -> png

Chris Kastorff

unread,
Dec 12, 2015, 10:30:22 PM12/12/15
to Viktor Kojouharov, golang-nuts
I'm pretty sure this is just JPEG chroma subsampling. It's 4:2:2 (1 chroma pixel covers a 2x2 box of luma pixels) by default, and image/jpeg doesn't appear to support doing any other subsampling. The JPEG spec *does* allow 4:4:4 (1 chroma pixel covers 1 luma pixel), but it's pretty rare to see.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Viktor Kojouharov

unread,
Dec 13, 2015, 11:06:31 AM12/13/15
to golang-nuts, vkojo...@gmail.com
I'm guessing the go encoder can't be set to use this '4:4:4' you speak of. For a comparison, when I use the gimp to convert that same png to jpeg, the resulting image has very little color distortion (e.g.: the red pixel would be (255,1,0) as opposed to (255,0,0)).

Klaus Post

unread,
Dec 13, 2015, 12:01:27 PM12/13/15
to golang-nuts, vkojo...@gmail.com
On Sunday, 13 December 2015 17:06:31 UTC+1, Viktor Kojouharov wrote:
I'm guessing the go encoder can't be set to use this '4:4:4' you speak of. For a comparison, when I use the gimp to convert that same png to jpeg, the resulting image has very little color distortion (e.g.: the red pixel would be (255,1,0) as opposed to (255,0,0)).

Libjpeg has a magic "switchover" point. If I recall correctly it is quality 90 and above, where it switches from 4:2:0 to 4:4:4. Some programs also expose a "disable color subsampling" option.


/Klaus
Reply all
Reply to author
Forward
0 new messages