Hi, we are using Processing to acquire data and write a video stream to disk. However, we are unable to figure out how to write one-channel 8-bit grayscale tiff images instead of the standard RGB images. Does anyone have some pointers on how to do this? Thank you.
Thanks for the pointer. We want to write only a single channel 8-bit grayscale image. My understanding reading the forum so far is that we need to write a custom function for this.
Yes, I think there’s not a built-in function in Processing for converting an image in 8-bit colors.
This is a hack function, but it works:
void convertAndSave8bitGray(PImage img) {
Image iimg = img.getImage();
BufferedImage bimage = new BufferedImage(iimg.getWidth(null), iimg.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(iimg, 0, 0, null);
bGr.dispose();
BufferedImage src = bimage;
BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
ColorConvertOp cco = new ColorConvertOp(src.getColorModel()
.getColorSpace(), dest.getColorModel().getColorSpace(), null);
cco.filter(src, dest);
File outputfile = new File(sketchPath("test2.jpg"));
try {
ImageIO.write(dest, "jpg", outputfile);
}
catch(IOException e) {
}
}
That’s great. Thanks so much.
For beginners looking to retrieve grayscale data or create grayscale images more generally (not 8-bit tiff specifically), there are also the HSB color functions – saturation()
and brightness()
, which return single values. Brightness in particular will return a single greyscale value for each pixel on the canvas or on a PImage / PGraphics, and you can use this as a simple built-in way to loop over pixels[] and either convert to grayscale or create a grayscale copy that can then be saved to disk.
https://processing.org/reference/brightness_.html
You can also set colorMode(HSB)
to hue-saturation-brightness and then use tint()
to remove the saturation on display, creating a grayscale image – although I believe it will still save as grayscale data in RGB if using the default saveFrame / saveImage.