Skip to content

Instantly share code, notes, and snippets.

@Andrewcpu
Created December 9, 2018 19:54
Show Gist options
  • Save Andrewcpu/248e313eeaf2cfe11175f91818d36edf to your computer and use it in GitHub Desktop.
Save Andrewcpu/248e313eeaf2cfe11175f91818d36edf to your computer and use it in GitHub Desktop.
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class ImageDisplay extends JFrame {
private BufferedImage image;
public ImageDisplay(BufferedImage image){
this.image = image;
setTitle("Image Display");
setBounds(0,0,image.getWidth(),image.getHeight());
setVisible(true);
}
@Override
public void paint(Graphics graphics){
graphics.drawImage(image,0,0, null);
}
}
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.List;
public class ImageModification {
public static BufferedImage grayScale(BufferedImage img){
BufferedImage grayScale = new BufferedImage(img.getWidth(),img.getHeight(),img.getType());
Graphics2D g = grayScale.createGraphics();
for(int i = 0; i<img.getWidth(); i++){
for(int j = 0; j<img.getHeight(); j++){
int pixel = img.getRGB(i,j);
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
int rgb = (int)((red + green + blue) / 3.0);
g.setColor(new Color(rgb,rgb,rgb));
g.fillRect(i,j,1,1);
}
}
return grayScale;
}
public static BufferedImage pixelize(BufferedImage image, int pixelSize){
Graphics g = image.createGraphics();
for(int x = 0; x<image.getWidth(); x+=pixelSize){
for(int y = 0; y<image.getHeight();y+=pixelSize){
double brightness = 0;
for(int i = 0; i<pixelSize; i++){
for(int j = 0; j<pixelSize; j++){
if( i + x >= image.getWidth() || j + y >= image.getHeight() ) continue;
brightness += (image.getRGB(i + x, y + j) >> 16) & 0xff;
}
}
brightness /= pixelSize * pixelSize;
Color color = new Color((int)brightness,(int)brightness,(int)brightness);
g.setColor(color);
g.fillRect(x,y,pixelSize,pixelSize);
}
}
return image;
}
public static BufferedImage createDiceImage(BufferedImage pixelizedImage, int pixelSize, boolean invertColors, List<String> lines) throws Exception{
BufferedImage one = ImageIO.read(ImageDisplay.class.getResource("images/1.png"));
BufferedImage two = ImageIO.read(ImageDisplay.class.getResource("images/2.png"));
BufferedImage three = ImageIO.read(ImageDisplay.class.getResource("images/3.png"));
BufferedImage four = ImageIO.read(ImageDisplay.class.getResource("images/4.png"));
BufferedImage five = ImageIO.read(ImageDisplay.class.getResource("images/5.png"));
BufferedImage six = ImageIO.read(ImageDisplay.class.getResource("images/6.png"));
double ratio = 122.0 / pixelSize;
BufferedImage diceImage = new BufferedImage((int)(pixelizedImage.getWidth() * ratio), (int)(pixelizedImage.getHeight() * ratio), pixelizedImage.getType());
Graphics diceGraphics = diceImage.createGraphics();
for(int x = 0; x<pixelizedImage.getWidth(); x += pixelSize){
String line = "";
for(int y = 0; y<pixelizedImage.getHeight(); y += pixelSize){
int brightness = (pixelizedImage.getRGB(x, y ) >> 16) & 0xff;
if(invertColors){
if(brightness <= 42.5 * 1){
diceGraphics.drawImage(five,(int)(x * ratio), (int)(y * ratio), null);
line += "5 ";
}
else if(brightness <= 42.5 * 2){
diceGraphics.drawImage(six,(int)(x * ratio), (int)(y * ratio), null);
line += "6 ";
}
else if(brightness <= 42.5 * 3){
diceGraphics.drawImage(four,(int)(x * ratio), (int)(y * ratio), null);
line += "4 ";
}
else if(brightness <= 42.5 * 4){
diceGraphics.drawImage(three, (int)(x * ratio), (int)(y * ratio), null);
line += "3 ";
}
else if(brightness <= 42.5 * 5){
diceGraphics.drawImage(two,(int)(x * ratio), (int)(y * ratio), null);
line += "2 ";
}
else if(brightness <= 42.5 * 6){
diceGraphics.drawImage(one,(int)(x * ratio), (int)(y * ratio), null);
line += "1 ";
}
}
else {
if(brightness <= 42.5 * 1){
diceGraphics.drawImage(one,(int)(x * ratio), (int)(y * ratio), null);
line += "1 ";
}
else if(brightness <= 42.5 * 2){
diceGraphics.drawImage(two,(int)(x * ratio), (int)(y * ratio), null);
line += "2 ";
}
else if(brightness <= 42.5 * 3){
diceGraphics.drawImage(three,(int)(x * ratio), (int)(y * ratio), null);
line += "3 ";
}
else if(brightness <= 42.5 * 4){
diceGraphics.drawImage(four, (int)(x * ratio), (int)(y * ratio), null);
line += "4 ";
}
else if(brightness <= 42.5 * 5){
diceGraphics.drawImage(six,(int)(x * ratio), (int)(y * ratio), null);
line += "6 ";
}
else if(brightness <= 42.5 * 6){
diceGraphics.drawImage(five,(int)(x * ratio), (int)(y * ratio), null);
line += "5 ";
}
}
}
lines.add(line);
}
return diceImage;
}
}
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception{
File imageFile = new File(JOptionPane.showInputDialog("Please enter the file path of the image:\nIMAGE WIDTH & HEIGHT MUST BE A MULTIPLE OF 7"));
BufferedImage img = ImageIO.read(imageFile);
new ImageDisplay(img);
BufferedImage grayScale = ImageModification.grayScale(img);
new ImageDisplay(grayScale);
BufferedImage pixelizedImage = ImageModification.pixelize(grayScale,6);
new ImageDisplay(pixelizedImage);
List<String> lines = new ArrayList<>();
BufferedImage diceScale = ImageModification.createDiceImage(pixelizedImage, 6, true, lines);
String fileName = imageFile.getParentFile().getAbsolutePath() + "\\DICE-IMAGE-" + System.currentTimeMillis();
ImageIO.write(diceScale, "png", new File(fileName + ".png"));
JOptionPane.showMessageDialog(null,"File has been exported...");
FileWriter writer = new FileWriter(fileName + "-INSTRUCTIONS.txt");
for(String str: lines) {
writer.write(str + "\n");
}
writer.close();
System.exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment