How to convert an image to greyscale? - java

I want to simply convert an image to greyscale. But all I get is a completely black image. Why?
BufferedImage original = ImageIO.read(url);
BufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
It has the correct width and height like the source image, but is completely black...

You are writing empty image to itself.
Change
g.drawImage(image, 0, 0, null);
to
g.drawImage(original, 0, 0, null);

Related

How do I downscale a BufferedImage to a specific number of pixels

I have an Image of 1000px X 1000px how do I go about and shrink it down to say 250x250 pixels and get an useable array of rgb colors values out of that
I tried using Java's Graphics2D but it turns out that doesn't actually reduce the amount of pixels
BufferedImage result = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
Graphics2D gra = result.createGraphics();
gra.drawImage(ImageIO.read(new File("")), 0, 0, 128, 128, null);
gra.dispose();
System.out.println(ImageIO.write(result, "png", new File("")));
The code that you posted works, you just need to properly specify the filename. I pointed it to a an image that was 3024x3024 and it saved as a 128x128 image.
BufferedImage result = new BufferedImage(128, 128, BufferedImage.TYPE_INT_ARGB);
Graphics2D gra = result.createGraphics();
gra.drawImage(ImageIO.read(new File("/tmp/3024x3024.jpeg")), 0, 0, 128, 128, null);
gra.dispose();
System.out.println(ImageIO.write(result, "png", new File("/tmp/128x128.png")));

How do I write a jpeg2000 with a transparent background in Java?

I'm using Java Advanced Imaging to read and write JPEG2000 images in java. When debugging in Intellij, I can view the BufferedImage and see that it's transparent. So I can stack layers on top of each other. But when I write it to a file, the background is always black. Is it something I'm doing wrong? Or is it the writer that's the problem? Do I need to be doing something specific with the J2KImageWriteParam regarding encoding etc? Unfortunately I'm fairly new to handling images programmatically, and there is little to no information or support for jpeg2000 that I can find.
BufferedImage image = ImageIO.read(new File("image.jp2"));
BufferedImage tmpImage = new BufferedImage(
image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE
);
Graphics2D g = (Graphics2D) tmpImage.getGraphics();
g.setComposite(AlphaComposite.Clear);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setComposite(AlphaComposite.Src);
g.drawImage(image, 0, 0, null);
ImageIO.write(tmpImage, "jpeg2000", new File("outputImage.jp2"));
g.dispose();

Java rescaling an image creating half white half grey image

I am having a problem with the Grapics2D API, I am trying to re-size an image but instead of getting the smaller image its showing a half white and half grey area. what am I doing wrong? I tried with and without render hints and it didn't work, or is there another approach that will give me a new Buffered Image object at the specified size?
//height = 400, width = 600, capture_rect = screen size
BufferedImage img = robot.createScreenCapture(CAPTURE_RECT);
BufferedImage resized = new BufferedImage(WIDTH, HEIGHT, img.getType());
Graphics2D g = resized.createGraphics();
g.drawImage(img, 0, 0, WIDTH, HEIGHT, 0, 0, img.getWidth(), img.getHeight(), null);
g.dispose();
setImage(resized);
See the article on Perils of Image.getScaledImage. It has some recommendations.
Here is some code it recommends:
private float xScaleFactor, yScaleFactor = ...;
private BufferedImage originalImage = ...;
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
int newW = (int)(originalImage.getWidth() * xScaleFactor);
int newH = (int)(originalImage.getHeight() * yScaleFactor);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(originalImage, 0, 0, newW, newH, null);
}
It looks like you just need to specify the new width/height and it will do the scaling for you automatically.
Or maybe the problem is with the img.getType() method. You should be able to just using:
BufferedImage.TYPE_INT_RGB // (or ARGB)
See which works the best.

How to get rid of White rectangle from an image using java

I had tired the following code to generate the thumb nail but when I place it on another image, the thumbnail has a white rectangle. How can I remove it using java graphics 2D?
Image image = javax.imageio.ImageIO.read(new File(originalFile));
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
File file = new File(thumbnailFile);
if(javax.imageio.ImageIO.write(thumbImage, "png", file))
return file;
}
Even if i remove these three lines I am getting black rectangle.
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE);
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
How to get this image as transparent? Please some one help me out.
Color transparent = new Color(0,0,0,0)
by setting Composite . here : AlphaComposite
// Create an image that supports arbitrary levels of transparency
Graphics2D g2d = (Graphics2D) image.getGraphics();
BufferedImage thumbImage = g2d.getDeviceConfiguration().createCompatibleImage(
thumbWidth, thumbHeight, Transparency.TRANSLUCENT);
..BufferedImage.TYPE_INT_RGB
That should be:
..BufferedImage.TYPE_INT_ARGB

How to resize tiff image in java?

I want to resize a .tiff file. I have used JAI toolkit to resize different types of images. Here is what I have tried to implement:
int imageWidth = 330;
int imageHeight = 490;
BufferedImage tempImage = new BufferedImage(imageWidth, imageHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = tempImage.createGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
graphics2D.dispose();
File outfile = new File("D:/Work/YoursGallery/output.tif");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
FileSeekableStream ss = new FileSeekableStream("D:/Work/YoursGallery/sample1.tif");
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", ss, null);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setTileSize(tempImage.getWidth(), tempImage.getHeight());
TIFFImageEncoder encoder = (TIFFImageEncoder) TIFFCodec.createImageEncoder("tiff", out, param);
encoder.encode(dec.decodeAsRenderedImage());
out.close();
The image created is having same size as original image has. Can anyone please tell what is the issue?
Here is the sample tiff image which I am using to test it.
http://docs.google.com/fileview?id=0BxCDhEXNFvbeMTYyMGZmNDYtODhhNy00YWI3LTkxNDgtZTNhM2FhMjg5Y2Q3&hl=en&authkey=CPCEypgM
Thanks in advance.
That's because you are writing out tempImage which is still the original image.
graphics2D.drawImage(image, 0, 0, imageWidth, imageHeight, null);
change that to:
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
or change your other code to write out image instead of tempImage
--Edit--
OK Attempt 2. Maybe having the source and destination the same is daft.
BufferedImage bsrc = ImageIO.read(new File(src));
BufferedImage bdest =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bdest.createGraphics();
AffineTransform at =
AffineTransform.getScaleInstance((double)width/bsrc.getWidth(),
(double)height/bsrc.getHeight());
g.drawRenderedImage(bsrc,at);
Try that :)
1) You are writing tempImage inside itself:
graphics2D.drawImage(tempImage, 0, 0, imageWidth, imageHeight, null);
Should be:
graphics2D.drawImage(originalImage, 0, 0, imageWidth, imageHeight, null);
2) You are writing the image that you just read (why are You reading it btw?):
encoder.encode(dec.decodeAsRenderedImage());
Should be:
encoder.encode(tempImage);

Categories