Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

How to Convert a Java BufferedImage between Colorspaces

1 min read

Java icon

The Java BufferedImage class provides a very powerful ‘abstraction’ of images in Java. It lets you create a huge array of image types which can all be seamlessly accessed. One of its key features is that you can decide what type of image you have – black and white, grayscale or full ARGB.

When we create an image from a PDF in JPedal, we use an ARGB BufferedImage because it is the only mode which support the color range and transparency which can be found in many PDFs.

Sometimes, you want to convert a BufferedImage from one type to another, and Java makes this very easy. We have a method in our ColorSpaceConvertor class convertColorspace(image, type) which does the conversion but the code is very simple and reproduced below. The Type is a constant from BufferedImage so making a page GRAY would need

image=ColorSpaceConvertor.convertColorspace(image, BufferedImage.TYPE_BYTE_GRAY);


/**
* convert a BufferedImage to RGB colourspace
*/
final public static BufferedImage convertColorspace(
BufferedImage image,
int newType) {
try {
BufferedImage raw_image = image;
image =
new BufferedImage(
raw_image.getWidth(),
raw_image.getHeight(),
newType);
ColorConvertOp xformOp = new ColorConvertOp(null);
xformOp.filter(raw_image, image);
} catch (Exception e) {
LogWriter.writeLog("Exception " + e + " converting image");
}
return image;
}

One issue that can arise is that detail can be lost so an alternative method is to create an image in the format you need and then draw the original image onto it. Here is how you can do this
BufferedImage image_to_save2=new BufferedImage(image_to_save.getWidth(),image_to_save.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
image_to_save2.getGraphics().drawImage(image_to_save,0,0,null);
image_to_save = image_to_save2;



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
Mark Stephens Mark has been working with Java and PDF since 1999 and is a big NetBeans fan. He enjoys speaking at conferences. He has an MA in Medieval History and a passion for reading.

One Reply to “How to Convert a Java BufferedImage between Colorspaces”

  1. Hi Mark, thanks for this, unfortunately neither method seems to work for me. The converted image is all black with both methods…

    final BufferedImage in = ImageUtils.loadFromResource(BRITISHRAIL);
    assertThat(in.getType(), is(BufferedImage.TYPE_3BYTE_BGR));

    final BufferedImage out = new BufferedImage(in.getWidth(), in.getHeight(),
    BufferedImage.TYPE_INT_ARGB);

    System.out.println(“Converting from TYPE_3BYTE_BGR to TYPE_INT_ARGB…”);
    //ColorConvertOp converter = new ColorConvertOp(null);
    //converter.filter(in, out); // doesn’t work

    out.getGraphics().drawImage(in,0,0,null); // doesn’t work either

    ImageIO.write(out, “jpg”, new File(“test5.jpg”));

Comments are closed.