Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image transformation results in a red image?

I am trying to transform an image by flipping it horizontally and resizing it. The problem is that when the transformation is done the picture's colors are all weird, it has gotten this reddish tone. Is it possible to fix this somehow, I think I read somewhere that it might be some bug in the AWT library but I am not sure?

Here is the code:

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class LocalImageSizeFlip {

public static void main(String[] args) {
    BufferedImage img = null;

    try {
        img = ImageIO.read(new File("C:\\picture.jpg"));
        AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
        tx.translate(0, -img.getHeight(null));
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        img = op.filter(img, null);
        img = resize(img, 100, 75);
        File newFile = new File("newPicture.jpg");
        ImageIO.write(img, "JPEG", newFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static BufferedImage resize(BufferedImage image, int width, int height) {
    BufferedImage resizedImage = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();
    return resizedImage;
    }   
}
like image 948
user1075481 Avatar asked Jan 22 '12 17:01

user1075481


1 Answers

Having an image develop a tint usually means the image is being rendered using the wrong colorspace, Adobe RGB vs. sRGB being a perennial favorite. Try changing TYPE_INT_ARGB to TYPE_INT_RGB in your code.

like image 53
Kyle Jones Avatar answered Nov 07 '22 21:11

Kyle Jones