0

How can i crop an image to a specified number of pixels or create image that the output will be base on number of pixel not rectangular shape. By using the code below i can only get square or rectangle shape.

BufferedImage out = img.getSubimage(0, 0, 11, 11);

But it only crops it to rectangular shape

import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
  public class raNd{
  public static void main(String args[])throws IOException{
    //image dimension
    int width = 10;
    int height = 10;
    //create buffered image object img
    BufferedImage img = new BufferedImage(width, height, 
    BufferedImage.TYPE_INT_ARGB);
    //file object
    File f = null;
    //create random image pixel by pixel
    for(int y = 0; y < height; y++){
      for(int x = 0; x < width; x++){
        int a = 255;//(int)(Math.random()*256); //alpha
        int r = (int)(Math.random()*256); //red
        int g = (int)(Math.random()*256); //green
        int b = (int)(Math.random()*256); //blue
        int p = (a<<24) | (r<<16) | (g<<8) | b; //pixel
        img.setRGB(x, y, p);
      }
    }
    //write image
    try{
      f = new File("/Users/kingamada/Documents/Java/Test6.png");
      BufferedImage out = img.getSubimage(0, 0, 5, 2);
      ImageIO.write(out, "png", f);
    }catch(IOException e){
    System.out.println("Error: " + e);
   }
  }//main() ends here
}//class ends here

Sample Picture

I want the last white pixels cropped out, so the picture will not be rectangle.

king amada
  • 139
  • 2
  • 14
  • You can't. You can make them transparent though, by setting alpha to zero. – Maurice Perry Mar 26 '18 at 06:50
  • I read this [https://stackoverflow.com/questions/43541086/crop-image-by-polygon-area-in-java], which suggest that i use Generatepath, but i can't get it to cut out as i want. I'm having difficulties with the coordinates. – king amada Mar 26 '18 at 07:36
  • 1
    You can use a clipping `Shape` when you paint the image. – Maurice Perry Mar 26 '18 at 07:49
  • I'm really a beginner, so code or explanation will definitely help me through. – king amada Mar 26 '18 at 07:54
  • It would be much simpler to make the pixels transparent, wouldn't it? – Maurice Perry Mar 26 '18 at 07:56
  • I would have loved to crop, but how can i change the last pixels to transparent? – king amada Mar 26 '18 at 08:00
  • What do you want to use to define your path, or clipping region? – matt Mar 26 '18 at 08:04
  • I seriously don't know... I copy the codes from [https://stackoverflow.com/questions/43541086/crop-image-by-polygon-area-in-java]] and try to crop the image... so anything to define the path or clipping the region I will use. I think they used Generalpath in the link – king amada Mar 26 '18 at 08:09

2 Answers2

0

Java images are rectangular, but people have suggested you can set the pixels you don't want to be transparent.

Ellipse2D clip = new Ellipse2D.Double(0, 0, width, height);

for(int y = 0; y < height; y++){
  for(int x = 0; x < width; x++){
    if(!clip.contains(x,y)){
      img.setRGB(x, y, 0);
    }
  }
}

This could directly be added to existing code to make your image an ellipse. Another way would be to use a clipping shape and a graphics object. I've replaced your complete write image block.

//write image
try{

  f = new File("Test6.png");

  Ellipse2D clip = new Ellipse2D.Double(0, 0, width, height);
  BufferedImage clipped = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  Graphics g = clipped.getGraphics();
  g.setClip(clip); //ellipse from above.
  g.drawImage(img, 0, 0, null);
  g.dispose();
  ImageIO.write(clipped, "png", f);
}catch(IOException e){
  System.out.println("Error: " + e);
}

This compiled for me and wrote a tiny circular image.

matt
  • 10,892
  • 3
  • 22
  • 34
0

So assuming the number of pixels you need to keep is in variable int pixelsLimit;:

int pixels = 0;
for(int y = 0; y < height; y++){
  for(int x = 0; x < width; x++){
    int p = 0;
    if (pixels < pixelsLimit) {
        int a = 255;//(int)(Math.random()*256); //alpha
        int r = (int)(Math.random()*256); //red
        int g = (int)(Math.random()*256); //green
        int b = (int)(Math.random()*256); //blue
        p = (a<<24) | (r<<16) | (g<<8) | b; //pixel
    }
    img.setRGB(x, y, p);
    ++pixels;
  }
}
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24