Skip to content

Instantly share code, notes, and snippets.

@spudtrooper
Created September 7, 2012 15:10
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 spudtrooper/3667016 to your computer and use it in GitHub Desktop.
Save spudtrooper/3667016 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.*;
public final class LesPaulImage {
public static void main(String[] args) {
if (args == null || args.length == 0) {
args = new String[]{"test.jpg"};
}
try {
new LesPaulImage().realMain(args);
} catch (Exception e) {e.printStackTrace();}
}
void realMain(String[] args) throws Exception {
for (int i=0; i<args.length; i++) convert(args[i]);
}
private void convert(String image) throws Exception {
note("image: " + image);
// First create an image and get the array of pixels
Image img = Toolkit.getDefaultToolkit().createImage(image);
JButton obs = new JButton(new ImageIcon(img));
int w = img.getWidth(obs);
int h = img.getHeight(obs);
note("size: " + w + "x" + h);
// Get the pixels and analyze them
int[] pixels = new int[w*h];
PixelGrabber pg = new PixelGrabber(img,0,0,w,h,pixels,0,w);
try {pg.grabPixels();} catch (Exception e) {e.printStackTrace();}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) return;
// Width and height of the small images
final int width = 50;
final int height = 50;
// Output image
int newWidth = w*width;
int newHeight = h*height;
note("Creating new image of size " + newWidth + "x" + newHeight + "...");
final BufferedImage outImage = new BufferedImage(newWidth,newHeight,
BufferedImage.TYPE_INT_RGB);
note("Done creating new image");
Graphics2D g2d = outImage.createGraphics();
outer: for (int i=0; i<h; i++) {
note((i+1) + " / " + h + " [" + (100*i/h) + "%]");
inner: for (int j=0; j<w; j++) {
int pixel = pixels[i*w+j];
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;
String colorDir = colorDir(red,green,blue);
File imageString = null;
if ("white".equals(colorDir)) {
// Write real images to to the pick guard and blank white to the outer
if ((i < 50*h/100 || i > 80*h/100) ||
(j < 20*w/100 || j > 37*w/100)) {
imageString = blankFile;
}
}
if (imageString != null) {
URL url = imageString.toURL();
BufferedImage bufImage = ImageIO.read(url);
int x = width*j;
int y = height*i;
g2d.drawImage(bufImage,x,y,null);
} else {
// Write 9 images
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
imageString = randomImage(colorDir);
URL url = imageString.toURL();
BufferedImage bufImage = ImageIO.read(url);
int x = width*j + width/2*dx;
int y = height*i + height/2*dy;
if (x >= 0 && x <= width*w && y >= 0 && y <= height*h) {
g2d.drawImage(bufImage,x,y,null);
}
}
}
}
}
}
File outFile = new File("out-" + image);
out("Writing to " + outFile + "...");
ImageIO.write(outImage, "png", outFile);
out("Done");
}
private final static File blankFile = new File("blank50.jpg");
// This is where the small images are
private final File imgDir = new File("img");
/**
* @return random image from imgDir/colorDir
*/
private File randomImage(String colorDir) {
File dir = new File(imgDir,colorDir);
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("images_");
}
});
int i = (int)(Math.random() * (float)files.length);
return files[i];
}
private String hex(int i) {
return Integer.toHexString(i);
}
private void out(Object msg) {
System.out.print(msg);
}
protected final void note(Object msg) {
System.err.println("[" + getClass().getName() + "] " + String.valueOf(msg));
}
private String colorDir(int r, int g, int b) {
return color2string(nearestColor(r,g,b));
}
private String color2string(Color c) {
if (c == Color.black) return "black";
if (c == Color.orange) return "orange";
if (c == Color.red) return "red";
if (c == Color.white) return "white";
return "brown";
}
private Color nearestColor(int r, int g, int b) {
Color color = new Color(r,g,b);
Color[] constantColors = new Color[] {
Color.black, Color.orange, Color.yellow, Color.red, Color.white, };
Color nearestColor = null;
double nearestDistance = Double.MAX_VALUE;
for (Color constantColor : constantColors) {
double dist = Math.sqrt(Math.pow(color.getRed() - constantColor.getRed(), 2)
+ Math.pow(color.getGreen() - constantColor.getGreen(), 2)
+ Math.pow(color.getBlue() - constantColor.getBlue(), 2)
);
if (nearestDistance > dist) {
nearestColor = constantColor;
nearestDistance = dist;
}
}
// XXX Use yellow instead of orange
if (nearestColor == Color.orange) nearestColor = Color.yellow;
return nearestColor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment