question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Greyscale JPG: Scaled image gets brighter

See original GitHub issue

Hello,

After reading, rescaling, and writing a JPG, the brightness gets changed. If the rescaling is skipped, the brightness is correct.

This is one of our test-images: https://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/Broadway_tower_grayscale.jpg/256px-Broadway_tower_grayscale.jpg (The other we cannot share)

This only seems to happen with the color-model grey. Screenshot 2020-11-27 at 16 36 43

Interestingly, it also happens when Image.getScaledInstance is used.

    public static void main(String ... args) throws Exception {
        Iterator<ImageReader> readers = ImageIO.getImageReaders(ImageIO.createImageInputStream(new FileInputStream(new java.io.File("/Users/x1fkirma/test2/original.jpg"))));
        int i = 0;
        while(readers.hasNext()) {
            i += 1;
            ImageReader reader = readers.next();
            System.out.println("Reader: " + reader);
            reader.setInput(ImageIO.createImageInputStream(new FileInputStream(new java.io.File("/Users/x1fkirma/test2/tower.jpg"))));
            BufferedImage input = reader.read(0);

            //twelvemonkey
            BufferedImage result = com.twelvemonkeys.image.ImageUtil.createScaled(input, 875, 583, Image.SCALE_DEFAULT);

            //java.awt.rescale
            //java.awt.Image img = input.getScaledInstance(875, 583, java.awt.Image.SCALE_SMOOTH);
            //BufferedImage result = new BufferedImage(875, 583, input.getType());
            //Graphics2D g2 = result.createGraphics();
            //g2.drawImage(img, 0, 0, null);
            //g2.dispose();


            javax.imageio.ImageIO.write(result, "jpg", new java.io.File("/Users/x1fkirma/test2/scaled_" + i + ".jpg"));
        }
        
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorianKirmaiercommented, Nov 30, 2020

You are right, it’s my fault. It works properly with ResampleOp. Sorry for the confusion. I was properly confused because the image already looked “light greyish”. I assume ResampleOp is the method which is also recommended to rescale an image?

0reactions
FlorianKirmaiercommented, Dec 1, 2020

@haraldk I’ve looked more into the topic and noticed the behavior depends on the performance hints. I’ve found the following cases: ImageUtil:

ImageUtil.createScaled(input, 875, 583, Image.SCALE_DEFAULT); // wrong
ImageUtil.createScaled(input, 875, 583, Image.SCALE_FAST); // correct

ResampleOp with the following RenderingHints, and filter is called with the second argument as null:

// No settings // correct
map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); // correct
map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);  // wrong
map.put(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); // wrong

ResampleOp seems to work in all cases when the second argument for filter is a pre created BufferedImage, created like the following:

BufferedImage result = new BufferedImage(875, 583, input.getType());
Read more comments on GitHub >

github_iconTop Results From Across the Web

Grayscale images appear brighter in Photoshop than after ...
Go to Edit menu and go down the list drop-down menu and see what color space you are in. The order I use...
Read more >
Gray scale images - Medium
In this post, we will learn how to create gray-scale images. A gray-scale (or gray level) image is simply one in which the...
Read more >
How to alter the brightness of a grey scale image?
Following Java program reads a colored image as greyscale, saves it, normalizes the brightness and increases the contrast of the given image ......
Read more >
Image conversion to Grayscale using ImageMagick is very dark
I converted a bunch of "normal" JPG photos via ... Is there a better way to convert a photo-realistic image with ImageMagick to...
Read more >
Image-6 Grayscale
Grayscale Conversion Algorithm · Pixel's average is effectively a "brightness" number 0..255 · Summarizes the 3 red/green/blue numbers as one number · To...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found