guys!
I have some problem. I need cutting some polygon image from jpg image and save it.
In this moment I use OpenSlideJNI.openslide_read_region, but OpenSlide can clip the only rectangle.
Do you have any idea?
The basic code would be:
// load the image
BufferedImage originalImage = ImageIO.read(...);
// create the polygon
Polygon polygon = new Polygon();
polygon.addPoint(50, 50);
polygon.addPoint(150, 50);
polygon.addPoint(250, 150);
polygon.addPoint(150, 150);
Rectangle bounds = polygon.getBounds();
// create a transparent clipped image based on the bounds of the Polygon
BufferedImage clippedImage = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = clippedImage.createGraphics();
polygon.translate(-bounds.x, -bounds.y);
g.setClip(polygon);
g.drawImage(originalImage, -bounds.x, -bounds.y, null);
// save the clipped image
ImageIO.write(...);
Of course the image will still be rectangular, but the non clipped areas will be transparent.
Related
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
I want to draw a circle on a buffered image that act like a png
i want to use this circle in order to replace the mouse cursor for a paint application i am working on.
i cant download a circle png from google as i need to keep changing the size of this circle depending on the width of the tool i am using it for.
here is my attempt so far
public static BufferedImage getCircle() {
BufferedImage bufferedImage = new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB);
Color transparent = new Color(0x00FFFFFF, true);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
//trying to make the bufferedImage transparent
g.setComposite(AlphaComposite.Src);
g.setColor(transparent);
g.setBackground(transparent);
g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
//drawing the circle
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.black);
g.drawOval(0, 0, 200, 200);
return bufferedImage;
}
it should look like:
However my code currently only creates a white square.
Your code has two problems (as already shown in the comments). The first is that you draw a circle with a radius of 200px into an image of dimensions 30px. If you closely look you can barely see a black pixel in the lower right corner.
Fix it by adjusting your dimensions such that it fits inside, for example like:
BufferedImage bufferedImage = new BufferedImage(60, 60, BufferedImage.TYPE_INT_RGB);
...
g.drawOval(5, 5, 50, 50);
Next is that you want to achieve a transparent background. To do so you need to set the type of the image to a color model which supports transparency, like ARGB (A = Alpha = transparency) instead of RGB:
BufferedImage bufferedImage = new BufferedImage(60, 60, BufferedImage.TYPE_INT_ARGB);
Last you probably want to increase the thickness of your border to achieve the image you showed. You do so by using g.setStroke(...):
g.setColor(Color.BLACK);
g.setStroke(new BasicStroke(5));
g.drawOval(5, 5, 50, 50);
With this setting you achieve the following result (with transparency):
Play with the values to adjust the circle to your exact needs.
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
I have a line. I know the endpoint of that line and an angle which tells by how many degrees it's tilted. I want to draw an image in such a way so that the bottom center point of the image would touch the endpoint of the line like so:
Sadly, I get the wanted result only if line isn't rotated, like above. If it's rotated by, for example, 90 degrees I get this:
This is the code that draws the image (line is already drawn by that point).
public void drawCreature(Graphics2D g) {
int centered = creature.getX() - creature.getImage().getWidth()/2;
BufferedImage image = creature.getImage();
BufferedImage rotatedImage = new BufferedImage(image.getWidth(), image.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D)rotatedImage.getGraphics();
g2d.rotate(Math.toRadians(creature.getAngle()), rotatedImage.getWidth()/2,
rotatedImage.getHeight()/2);
g2d.drawImage(creature.getImage(), 0, 0, null);
g.drawImage(rotatedImage, centered, creature.getY(), null);
}
Any help would be appreciated.
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