Skip to content

Instantly share code, notes, and snippets.

@LeRoiDesKiwis
Created July 1, 2019 15:53
Show Gist options
  • Save LeRoiDesKiwis/5af6901bf1bc5580669d400c1daaa557 to your computer and use it in GitHub Desktop.
Save LeRoiDesKiwis/5af6901bf1bc5580669d400c1daaa557 to your computer and use it in GitHub Desktop.
Gist for compressing pictures
package fr.leroideskiwis.compressingimage;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public Main() throws IOException {
while(true) {
Scanner scanner = new Scanner(System.in);
String path = scanner.nextLine();
File file = new File(path);
BufferedImage image = ImageIO.read(file);
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
int taux = 64;
for (int x = 0; x < newImage.getWidth(); x+=taux/2) {
for (int y = 0; y < newImage.getHeight(); y+=taux/2) {
for (int x1 = 0; x1 < taux/2; x1++) {
for (int y1 = 0; y1 < taux / 2; y1++) {
int xDest = x+x1;
int yDest = y+y1;
if(xDest >= newImage.getWidth() || yDest >= newImage.getHeight()) break;
newImage.setRGB(xDest, yDest, image.getRGB(x,y));
}
}
}
}
ImageIO.write(newImage, "png", getFile(path, "png"));
}
}
public File getFile(String path, String extension){
File file = new File(path+"."+extension);
for (int i = 0; file.exists(); i++) {
file = new File(path+i+"."+extension);
}
return file;
}
public static void main(String[] args) throws IOException {
new Main();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment