Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ImageSlicing for puzzle
public class ImageSlicer {
public static void main(String[] args) throws IOException {
if (args.length < 3) {
System.out.println("Usage: java -jar imageSlicer.jar /path/to/image.jpg <number-of-pieces-wide> <number-of-pieces-high>");
System.out.println("Usage: java -jar imageSlicer.jar /home/georgi/Desktop/sarah.jpg 4 4");
return;
}
Path imagePath = Paths.get(args[0]);
int piecesWide = Integer.parseInt(args[1]);
int piecesHigh = Integer.parseInt(args[2]);
BufferedImage bufferedImage = ImageIO.read(imagePath.toFile());
int pieceWidth = (int) (bufferedImage.getWidth() / (float) piecesWide);
int pieceHeight = (int) (bufferedImage.getHeight() / (float) piecesHigh);
for (int y = 0; y < piecesHigh; y++) {
for (int x = 0; x < piecesWide; x++) {
BufferedImage region = bufferedImage.getSubimage(x * pieceWidth, y * pieceHeight, pieceWidth, pieceHeight);
ImageIO.write(region, "jpg", new File(Paths.get(".").toAbsolutePath().toFile(), "a" + (y * piecesWide + x) + ".jpg"));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.