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.

Converting Java BufferedImage between Colorspaces

59 sec read

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 seemlessly 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(
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_save = image_to_save2;

IDRsolutions develop a Java PDF Viewer and SDK, an Adobe forms to HTML5 forms converter, a PDF to HTML5 converter and a Java ImageIO replacement. On the blog our team post anything interesting they learn about.

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.

Why we wrote our own Java jpeg2000 libraries

JPEG2000 is an important image file format which offers significant benefits over JPEG. For our specific usage it does generate significantly smaller file sizes...
Mark Stephens
52 sec read

How to choose JPG versus JPEG2000 for image files

Since we started to support both JPG and JPG2000 as image file outputs in our software, we have found that this is a very...
Mark Stephens
1 min read

One Reply to “Converting 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(),

    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”));

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2019. All rights reserved.