Skip to content

Instantly share code, notes, and snippets.

@GeorgiPachov
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgiPachov/cfe5027b1eca5dd97fa4 to your computer and use it in GitHub Desktop.
Save GeorgiPachov/cfe5027b1eca5dd97fa4 to your computer and use it in GitHub Desktop.
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