ImageIO not printing proper color - java

I am trying to read a PNG image file from disk, draw some rectangles on it and save the modified image on the disk. Here's the scala code:
//l is a list of Rectangle objects of the form (x1,x2,y1,y2)
val image = ImageIO.read(sourceimage);
val graph=image.createGraphics()
graph.setColor(Color.GREEN)
l.foreach(x=>graph.draw(new java.awt.Rectangle(x.x1,x.y1,x.x2-x.x1,x.y2-x.y1)))
graph.dispose()
ImageIO.write(image,"png",new File(destimage))
The rectangles are drawn but in GREY color instead of GREEN. What am I doing wrong?

If the source image is a gray scale image, then it's unlikely that it will be capable of using any color of any sort.
Instead, you need to create a second, colored, BufferedImage and paint the original to it.
BufferedImage original = ImageIO.read(sourceimage);
BufferedImage iamge = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = image.createGraphics();
g2d.drawImage(original, 0, 0, null);
// Continue with what you have
Sorry, I have no experience with PIL, but that's how you'd do it (basically) in pure Java

Related

How do you simply obtain an array of pixel data from an image by java?

I need to work code to analyze the image to pixels in the form of an array.
Please help me
Paint the Image to a BufferedImage:
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = bi.createGraphics();
graphics.drawImage(originalImage, 0, 0, null);
graphics.dispose();
Now you can use method of the BufferedImage class to manipulate the image.
Maybe using the getRGB(...) or getSubImage(...) methods?

Java - crop image using trapezoid shape [duplicate]

by using Canvas and JS I can draw a shape like this and have the x,y of each point :
Tha area can be choosen by more than 4 points, look at this link to have an idea.
I need to save and crop the image of the selected area by using the points. I can not use BufferedImage as it is just rectangular. Which lib in java I can use?
Okay, so starting with...
I used...
BufferedImage source = ImageIO.read(new File("Example.jpg"));
GeneralPath clip = new GeneralPath();
clip.moveTo(65, 123);
clip.lineTo(241, 178);
clip.lineTo(268, 405);
clip.lineTo(145, 512);
clip.closePath();
Rectangle bounds = clip.getBounds();
BufferedImage img = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
clip.transform(AffineTransform.getTranslateInstance(-65, -123));
g2d.setClip(clip);
g2d.translate(-65, -123);
g2d.drawImage(source, 0, 0, null);
g2d.dispose();
ImageIO.write(img, "png", new File("Clipped.png"));
to generate...
Now, the image is rectangular, that's just the way it works
Now, setClip is quite rough and isn't effect by any RenderingHints, you could make use of "soft clipping" instead, which is more involved, but generates a nicer results. See this example and this exmaple for more details

Is there a function that converts any image into a circle of 150x150 pixels-java?

I need a function/method that can mold(crop and resize) an imported (.png format) image into a circle of exact 150x150 pixels and it should keep transparency. I have searched all over internet, also I have my own code but I think its completely useless. I need this function for a code I am using to make GUI of a social-media app database.
private ImageIcon logo = new ImageIcon(getClass().getResource("/test/test200x200.png"));
toCircle(logo);
I need the code for the following function:
public ImageIcon toCircle(ImageIcon icon)
{
//code
return icon;
}
This function should convert this picture:
To this:
Create a new transparent image
Get a Graphics object from the image.
Set a clip for the graphics object.
Paint the PNG format image.
See also this answer that uses a clipped region.
An alternative approach, that might be more straight-forward to implement for this use case, is:
Create a transparent BufferedImage the size of your icon
Create Graphics2D from image, set hints for antialias
Fill a circle the size of your background circle
Draw the image on top of your circle, using AlphaComposite.SrcIn
Something like:
public Icon toCircle(ImageIcon logo) {
BufferedImage image = new BufferedImage(150, 150); // Assuming logo 150x150
Graphics2D g = image.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.fillOval(1, 1, 148, 148); // Leaving some room for antialiasing if needed
g.setComposite(AlphaComposite.SrcIn);
g.drawImage(logo.getImage(), 0, 0, null);
g.dispose();
return new ImageIcon(image);
}

Add color to a grayscale BufferedImage

Can't find an answer to this, maybe its too basic. I have a gray scale BufferedImage (basically a section from a black-and-white PDF), and I'd like to draw a red box on the image. However, when I do so and save the image, the red box comes out as grey.
How to correctly add color to a gray scale BufferedImage?
I suppose I need to convert the color model(?) from gray scale to RGB? Although I don't need to convert the black and white parts of the image to color - that is to say the resulting image can be black and white. So long as I can draw a red line on the image without it saving as a shade of gray.
The image file is a GIF.
By using the following code, I could add RED rectangle to a grayscale image. See if this works for you. Else let us know what error you faced.
public static void main(String[] args) throws IOException {
BufferedImage old = ImageIO.read(new File("download.gif"));
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(
w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, null);
g2d.setColor(Color.red);
g2d.drawRect(10, 10, 100, 100);
g2d.dispose();
ImageIO.write(img, "gif", new File("out.gif"));
}

Java shapes convert to BufferedImage

I want to make rectangles on a JLabel and the convert that rectangle into a BufferedImage... like layers in paint shop... drage that BufferedImage and resize...can anyone help
I have done this but it didnt work
Rectangle2D rectangle2D;
BufferedImage bi = new BufferedImage(bimg.getWidth(), bimg.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D big = bi.createGraphics();
rectangle2D = new Rectangle2D.Float(eX, eY, sW, sH);
big.setStroke(new BasicStroke(5));
big.setColor(color);
shapePaint = new TexturePaint(bi, rectangle2D);
g2d.setPaint(shapePaint);
I want to make rectangles on a JLabel and the convert that rectangle into a BufferedImage
You are doing it the wrong way around. Draw to the buffered image, add it to a label, call label.repaint() to display any changes.
E.G.
As seen in..
This answer
This answer
This answer or..
..For an animated version, this answer

Categories

Resources