Skip to content

Instantly share code, notes, and snippets.

@SavvasStephanides
Created February 11, 2014 14:54
Show Gist options
  • Save SavvasStephanides/8936308 to your computer and use it in GitHub Desktop.
Save SavvasStephanides/8936308 to your computer and use it in GitHub Desktop.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Pixelate
{
public void pixelateFile(int pixelSize, File inputFile, File outputFile) throws IOException
{
// get a BufferedImage object from file
BufferedImage bufferedImage = ImageIO.read(inputFile);
// loop through the image and produce squares pixelSize*pixelSize
for(int w = 0 ; w < bufferedImage.getWidth() ; w+=pixelSize)
{
for(int h = 0 ; h < bufferedImage.getHeight() ; h+=pixelSize)
{
Color pixelColor = new Color(bufferedImage.getRGB(w, h));
Graphics graphics = bufferedImage.getGraphics();
graphics.setColor(pixelColor);
graphics.fillRect(w, h, pixelSize, pixelSize);
}
}
// output file
ImageIO.write(bufferedImage, "jpg", outputFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment