How to convert between color models

I am very new to image processing. I have a PNG image (using ImageIO.read() ) that gives BufferedImage.TYPE_CUSTOM when I call getType() on it.

 BufferedImage bi = ImageIO.read(new URL("file:/C:/samp1.png")); int type =bi.getType(); //TYPE_CUSTOM for samp1.png 

Now I would like to convert it to one of the following models:

  • TYPE_USHORT_GRAY
  • TYPE_3BYTE_BGR
  • TYPE_BYTE_GRAY
  • TYPE_INT_RGB
  • TYPE_INT_ARGB

The above should be performed for further image processing using a library that recognizes only the above types.

How can I convert from TYPE_CUSTOM color model to other models?

Any hints / pointers would be much appreciated. If there is no existing library for this, any link / message for steps / algorithm will be great.

+4
source share
2 answers

Have you tried this?

 BufferedImage rgbImg = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_RGB); 
0
source

Try the following:

 public static BufferedImage convert(BufferedImage src, int bufImgType) { BufferedImage img= new BufferedImage(src.getWidth(), src.getHeight(), bufImgType); Graphics2D g2d= img.createGraphics(); g2d.drawImage(src, 0, 0, null); g2d.dispose(); return img; } 
+10
source

All Articles