What's the simplest way to desaturate a BufferedImage
?
Asked
Active
Viewed 2,293 times
5
1 Answers
10
Use ColorConvertOp
:
public static BufferedImage desaturate(BufferedImage source) {
ColorConvertOp colorConvert =
new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
colorConvert.filter(source, source);
return source;
}
Update :
There is indeed a simpler way. You can use the GrayFilter
class. What's nice about this class is that it provides a static utility method (i.e. createDisabledImage(Image i)
) that will return a grayed-out version of the image i
.
That being said, I think the simplest way to desaturate a BufferedImage
instance is the following:
BufferedImage desaturatedImage = GrayFilter.createDisabledImage(originalImage);

mre
- 43,520
- 33
- 120
- 170
-
1Indeed. Is there a simpler way? ;-) i.e. without ConvertColorOp? – Dycey Jun 24 '11 at 17:39
-
4@Dycey That's three lines of code. You can't really get much simpler than that. – Reverend Gonzo Jun 24 '11 at 17:41
-
@Dycey Simpler as in call the desaturate() method implemented in this answer? – Marcelo Jun 24 '11 at 17:42
-
2@Dycey, I think using `ColorConvertOp`is as simple as it's going to get to perform pixel-by-pixel color conversion. :) – mre Jun 24 '11 at 17:51