I'm reading a PNG image with the following code:
BufferedImage img = ImageIO.read(new URL(url));
Upon displaying it, there is a black background, which I know is caused from PNG transparency.
I found solutions to this problem suggesting the use of BufferedImage.TYPE_INT_RGB, however I am unsure how to apply this to my code above.
Create a second BufferedImage of type TYPE_INT_RGB...
BufferedImage copy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
Paint the original to the copy...
Graphics2D g2d = copy.createGraphics();
g2d.setColor(Color.WHITE); // Or what ever fill color you want...
g2d.fillRect(0, 0, copy.getWidth(), copy.getHeight());
g2d.drawImage(img, 0, 0, null);
g2d.dispose();
You now have a non transparent version of the image...
To save the image, take a look at Writing/Saving an Image
Related
Can I somehow insert an image by coordinates into another image?
A MultipartFile comes to me with an image, if its aspect ratio != 16:9, then I generate a new image with that aspect ratio and black and in the middle of it I need to insert the image that came to me.
At the moment I can only generate a black image, but I have no way to figure out how to insert the image by coordinates into another image.
I tried using Graphics2D.drawImage(), but it didn't work for me.
`public static String getImageAndReturnPathToResult(MultipartFile multipartFile){
try{
> //Image taken from the front
BufferedImage image = ImageIO.read(multipartFile.getInputStream());
> //Generating a new black image
BufferedImage bufferedImage = new BufferedImage(1920, 1080, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.setColor(Color.black);
g2d.fillRect(0, 0, 1920, 1080);
g2d.dispose();
} catch (IOException e) {
e.printStackTrace();
}
return "error";
}`
Most likely you failed with Graphics2D.drawImage() because you didn't pass the arguments correctly. Try:
Graphics2D.drawImage(image, x, y, null);
Graphics2D.dispose();
After that everything should work.
I have been following some StackOverflow links using Graphics2D to change the background color of a BufferedImage.
The project I am working on requires that I read in a png image from a given url; the png image retrieved has a transparent background, and I would like to set it to white.
Here is what I have:
String u = this.format() ;
BufferedImage image = null ;
try{
URL url = new URL(u) ;
image = ImageIO.read(url) ;
Graphics2D graphics = image.createGraphics() ;
graphics.setBackground(Color.WHITE) ;
graphics.clearRect(0, 0, image.getWidth(), image.getHeight()) ;
ImageIO.write(image, "png", new File(outPath + fileName)) ;
graphics.dispose() ;
}
catch(IOException e){
e.printStackTrace() ;
}
The problem I am running into is that when I view the image, the image appears as a solid white box. Apparently I have overlaid a white background on top of the existing image that I retrieved.
How can I preserve the original image and only change the background? Or set the background first and then overlay the retrieved image?
1- Load your image
image = ImageIO.read(url) ;
2- Create a new BufferedImage of the same size
BufferedImage background = new BufferedImage(image.getWidth(), image.getHeight, BufferedImage.TYPE_INT_RGB);
3- Fill the background image with the desired color
Graphics2D g2d = background.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, background.getWidth(), background.getHeight());
4- Draw the original image onto the background...
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
background is now filled with the desired color and has the image painted on top o it.
How can I transform color image to grayscale using java code?
A sample grayscale image for reference:
A simplest way is to draw to GRAY buffered image
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
See more ways here
Currently, when I load an image off disk,
BufferedImage img = ImageIO.read(new File("myfile.png");
The resulting color space is one of ARGB. What I'd prefer is a plain old RGB, but without having to do the filtering myself.
Is there a way to open an image in a certain color mode?
I'm not aware of a way to open it with a specific color format, but you can create a new BufferedImage of the desired format and draw the old image on to it:
BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g = img2.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
Any transparent parts in the original image will be drawn over a black background in the new image. If you'd prefer a different background, you can insert these lines before the drawImage call:
g.setColor(Color.white); // or whichever
g.fillRect(0, 0, img.getWidth(), img.getHeight());
I'm converting an image with transparency in it into a Colorspace that doesn't have transparency. I'd like to set a background color for the transparent areas. Right now when I convert it any area that is transparent turns to black in the final image. Is there a way to do that while I'm converting between ColorSpaces? Here is my code I use to convert between color spaces:
public BufferedImage convertColorspace( BufferedImage source, int newType) {
BufferedImage destination = new BufferedImage( source.getWidth(), source.getHeight(), newType);
ColorConvertOp colorConvertOp = new ColorConvertOp(null);
colorConvertOp.filter(source, destination);
return destination;
}
// here is how its used
BufferedImage converted = convertColorspace(combinedImage, BufferedImage.TYPE_3BYTE_BGR);
I'm converting from BufferedImage.TYPE_4BYTE_ARGB to BufferedImage.TYPE_3BYTE_BGR.
How about:
BufferedImage temp = new BufferedImage(source.getWidth(), source.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = temp.createGraphics();
g2.setColor(Color.green);
g2.fillRect(0, 0, source.getWidth(), source.getHeight());
g2.drawImage(0, 0, source, null);
g2.dispose();
Then call colorConvertOp.filter with temp instead of source.