Napstablook
Napstablook

Reputation: 614

Image quality reduces (turns reddish) as JPEG with BufferedImage

I am making a program where I extract out pixel array from an image, Take out ARGB values. And write them back again to make another image.

        BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
        iw = imagebuffer.getWidth();
        ih = imagebuffer.getHeight();
       
        pixels = new int[iw * ih];
        PixelGrabber pg = new PixelGrabber(imagebuffer, 0, 0, iw, ih, pixels, 0, iw);
        pg.grabPixels();  
    
      BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
      image.setRGB(0, 0, width, height, pixels, 0, width);
        

        ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
        ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
        ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));

Now output image for png and gif look fine but the output jpg image turns out quite reddish.

This is the original image enter image description here And this is the output jpg image enter image description here

Any idea what might be causing this? Any push towards the right direction will be appreciated.

Upvotes: 0

Views: 591

Answers (1)

Luatic
Luatic

Reputation: 11171

I dont know if this will work for you, but I always did it pixel-by-pixel. So :

BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();
BufferedImage image = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_ARGB);
for (int x=0; x < iw; x++) {
     for (int y=0; y < ih; y++) {
          image.setRGB(x,y,imagebuffer.getRGB(x,y));
     }
}
ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));

And it this way has a similar count of lines, so I think you could give it a try.

If you'd like to insert text, id ìmport java.awt.*;, what includes Graphics and Graphics2D and Font, and then :

Font font=new Font("Sans,0,20); //Name, type(none, bold, italic), size
Graphics2D imagegraphics=imagebuffer.createGraphics();
imagegraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //enable antialiasing
imagegraphics.setFont(font);
imagegraphics.setColor(Color.BLACK);
String yourtext="Fighter";
int h=imagegraphics.getFontMetrics().getHeight();
int w=imagegraphics.getFontMetrics().stringWidth(yourtext);
imagegraphics.drawString(yourtext,5,h); //Draw text upper left corner, note that y-value is the bottom line of the string

EDIT :

It was the Alpha value. Fixed by :

BufferedImage image = new
BufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB); //RGB, jpeg hasnt got alpha, ints have been converted as if they contain red first, but its alpha(the first bytes, these ints are interpreted bitwise i think) (argb), so it became more red.

Upvotes: 1

Related Questions