Last active
August 29, 2015 14:07
-
-
Save GeorgiPachov/cfe5027b1eca5dd97fa4 to your computer and use it in GitHub Desktop.
ImageSlicing for puzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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