0

Recently I wrote a method which takes a photo and does a horizontal mirror (also called horizontal flip) which was working fine with all the pictures I tried (until today). Today I tried the following photo and I get the following result. For some reason most of the image color has changed to red.

Original image

enter image description here

Image After processing

enter image description here

The original image is a png and the output is jpeg. I'm not sure if that has to do with it. Here is the code which is doing the flipping

public BufferedImage flipImageHorizentally(String imagePath) throws IOException {
    BufferedImage bufferedImage = ImageIO.read(new File(imagePath));

    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-bufferedImage.getWidth(null), 0);

    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);

    System.out.println("Flipped the image horizentally!");

    return bufferedImage;
}

I then save the bufferedImage which is returned using the code below

static void SaveImageToDisk(BufferedImage bufferedImage) {
    String folderName = "ProcessedImages";
    Common.CreateFolderIfNotExists(folderName);
    try {
        // retrieve image
        File outputfile = new File(folderName + File.separator + UUID.randomUUID().toString() + ".jpg");
        ImageIO.write(bufferedImage, "jpg", outputfile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

What is causing the modified image to get red? How can I prevent this from happening?

Arya
  • 8,473
  • 27
  • 105
  • 175
  • Could it be that the original image is a cmyk jpg? Did you try reading and writing a different format, e.g. an unsophisticated one like bmp? – Thomas Apr 07 '17 at 08:23
  • 4
    You've taken a alpha based image and saved it as a jpg, this is a known issue – MadProgrammer Apr 07 '17 at 08:23
  • 4
    [For example](http://stackoverflow.com/questions/8962990/image-transformation-results-in-a-red-image) and [example](http://stackoverflow.com/questions/13072312/jpeg-image-color-gets-drastically-changed-after-just-imageio-read-and-imageio) and [example](http://stackoverflow.com/questions/12963685/java-buffered-image-created-with-red-mask) – MadProgrammer Apr 07 '17 at 08:27
  • 1
    The basic solution is to create another `BufferedImage` using `TYPE_RGB` and painting the original image to it and the saving it – MadProgrammer Apr 07 '17 at 08:28

0 Answers0