Uploading Grayscale images causes the image to be darkened

Uploading a purely grayscale image causes the image to be darkened when displayed by discourse. This applies to images in posts, as well as images uploaded as avatars. for example, take this image:

This is much darker that the image is supposed to be. However, if I shift the R,G, or B value of a single pixel, it will display correctly. In this case, I have shifted the top left pixel from (172,172,172) to (173,172,172). This is the only change I have made between the source images.

Now, because of the R value being shifted in one pixel, the image is being displayed accurately.

Unfortunately, this fix is not reliable for avatars. Once the image is scaled down, the shifted pixel gets lost and the smaller image gets darkened again. As a result, you can end up with a grab bag of light and dark avatars. To make it work for avatars, you pretty much need to shift a region of pixels, instead of just a single pixel.

7 Likes

Here’s an example of a user who tried uploading a grayscale avatar with a single altered pixel. Note the version in the usercard is lighter than the resized version next to the post:

Here is an example of an avatar that works successfully. The entire background on this image was shifted slightly using the technique described in the OP.

We’re using both ImageMagick and ImageOptim to make sure thumbnails are as small as possible

https://github.com/discourse/discourse/blob/master/app/models/optimized_image.rb#L81-L115

Not sure what’s causing the image to be darkened though :frowning:

  • GIF: convert in.gif -coalesce -gravity center -thumbnail 100x100^ -extent 100x100 -layers optimize out.gif
  • OTHER: convert in.png[0] -background transparent -gravity center -thumbnail 100x100^ -extent 100x100 -interpolate bicubic -unsharp 2x0.5+0.7+0 -quality 98 out.png
2 Likes

We have had a similar report of this in the past by @pakl I think did not get to the bottom of it yet, something about those command lines

Dupe of

https://meta.discourse.org/t/discourse-gravatar-processing-screws-up-gamma/17993

I will reclassify that bug since it is reported often enough to be, and seems legit.

2 Likes