Last active
May 4, 2018 00:39
-
-
Save BananaPuncher714/c9cd6953da169e41ceacc8501c4192c9 to your computer and use it in GitHub Desktop.
View images on the command line
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
import java.awt.Color; | |
import java.awt.Graphics2D; | |
import java.awt.Image; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.RescaleOp; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.Map; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.ArrayList; | |
import javax.imageio.ImageIO; | |
import javax.net.ssl.HttpsURLConnection; | |
/** | |
* Much like the famous AAlib, this is intended to draw images on a console. However, instead of using pure black and white along with ascii characters, | |
* this uses 2 unicode block characters to create different colors on a 16 color terminal. You must run this with the -Dfile.encoding=UTF-8 parameter! | |
* | |
* @author BananaPuncher714 | |
*/ | |
@SuppressWarnings("deprecation") | |
public final class CLImager { | |
private static Set< Color > colors; | |
private static Map< Color, String > charMap; | |
private static final Color BLACK = new Color( 0, 0, 0 ); | |
private static final double[] color_multipliers = { 0.4375D, 0.1875D, 0.3125D, 0.0625D }; | |
public static void main( String[] args ) { | |
String url = "http://www.rw-designer.com/icon-image/5547-64x64x32.png"; | |
if ( args.length > 0 ) { | |
url = args[ 0 ]; | |
} | |
int width = 200; | |
if ( args.length > 1 ) { | |
width = Math.max( 1, Integer.parseInt( args[ 1 ] ) ); | |
} | |
if ( url.endsWith( ".ci" ) ) { | |
drawBashPicture( new File( System.getProperty( "user.dir" ) + "/" + url ) ); | |
} else { | |
try { | |
if ( args.length > 2 ) { | |
drawBashPicture( saveToFile( resizeByWidth( getImage( url ), width ) ) ); | |
} else { | |
drawDirect( resizeByWidth( getImage( url ), width ) ); | |
} | |
} catch ( MalformedURLException e ) { | |
e.printStackTrace(); | |
} catch ( IOException e ) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
protected static void drawDirect( Image input ) { | |
BufferedImage image = toBufferedImage( input ); | |
dither( image ); | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
StringBuilder writer = new StringBuilder(); | |
for ( int y = 0; y < height; y++ ) { | |
StringBuilder builder = new StringBuilder(); | |
for ( int x = 0; x < width; x++ ) { | |
Color color = new Color( image.getRGB( x, y ) ); | |
builder.append( charMap.get( color ) + "," ); | |
} | |
String[] pixels = builder.toString().split( "," ); | |
int pixInd = 1; | |
for ( String pixel : pixels ) { | |
String[] parts = pixel.split( ":" ); | |
String ch = "\u2588"; | |
String fg = parts[ 1 ]; | |
String fb = parts[ 2 ]; | |
String bg = parts[ 3 ]; | |
String bb = parts[ 4 ]; | |
if ( parts[ 0 ].equalsIgnoreCase( "0" ) ) { | |
ch = "\u2591"; | |
} else if ( parts[ 0 ].equalsIgnoreCase( "1" ) ) { | |
ch = "\u2592"; | |
} | |
if ( pixInd++ > 200 ) { | |
break; | |
} | |
writer.append( "\u001B[" + bb + ";4" + bg + "m\u001B[" + fb + ";3" + fg + "m" + ch ); | |
} | |
writer.append( "\u001B[0m\n" ); | |
} | |
System.out.println( writer.toString() ); | |
} | |
public static void drawBashPicture( File file ) { | |
try { | |
FileReader fileReader = new FileReader(file); | |
BufferedReader bufferedReader = new BufferedReader(fileReader); | |
String line; | |
StringBuilder builder = new StringBuilder(); | |
while ( ( line = bufferedReader.readLine() ) != null ) { | |
String[] pixels = line.split( "," ); | |
int pixInd = 1; | |
for ( String pixel : pixels ) { | |
String[] parts = pixel.split( ":" ); | |
String ch = "\u2588"; | |
String fg = parts[ 1 ]; | |
String fb = parts[ 2 ]; | |
String bg = parts[ 3 ]; | |
String bb = parts[ 4 ]; | |
if ( parts[ 0 ].equalsIgnoreCase( "0" ) ) { | |
ch = "\u2591"; | |
} else if ( parts[ 0 ].equalsIgnoreCase( "1" ) ) { | |
ch = "\u2592"; | |
} | |
if ( pixInd++ > 200 ) { | |
break; | |
} | |
builder.append( "\u001B[" + bb + ";4" + bg + "m\u001B[" + fb + ";3" + fg + "m" + ch ); | |
} | |
builder.append( "\u001B[0m\n" ); | |
} | |
System.out.println( builder.toString() ); | |
fileReader.close(); | |
} catch ( Exception exception ) { | |
exception.printStackTrace(); | |
} | |
} | |
public static File saveToFile( Image input ) { | |
File output = new File( System.getProperty( "user.dir" ) + "/output.ci" ); | |
try { | |
BufferedImage image = toBufferedImage( input ); | |
dither( image ); | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
PrintWriter writer = new PrintWriter( output, "UTF-8" ); | |
for ( int y = 0; y < height; y++ ) { | |
StringBuilder builder = new StringBuilder(); | |
for ( int x = 0; x < width; x++ ) { | |
Color color = new Color( image.getRGB( x, y ) ); | |
builder.append( charMap.get( color ) + "," ); | |
} | |
writer.println( builder.toString() ); | |
} | |
writer.close(); | |
} catch ( Exception exception ) { | |
exception.printStackTrace(); | |
} | |
return output; | |
} | |
static { | |
colors = new HashSet< Color >(); | |
charMap = new HashMap< Color, String >(); | |
List< Color > cc = getNativeConsoleColors(); | |
for ( int c = 0; c < 2; c++ ) { | |
double percent = 0; | |
if ( c == 0 ) percent = .75; | |
if ( c == 1 ) percent = .5; | |
for ( int f = 0; f < cc.size(); f++ ) { | |
for ( int b = 0; b < cc.size(); b++ ) { | |
Color color = mixColors( cc.get( f ), cc.get( b ), percent ); | |
charMap.put( color, c + ":" + ( f % 8 ) + ":" + ( ( b > 7 ) ? "5" : "6" ) + ":" + ( b % 8 ) + ":" + ( ( f > 7 ) ? "1" : "0" ) ); | |
colors.add( color ); | |
} | |
} | |
} | |
// System.out.println( "Collected " + colors.size() + " unique colors!" ); | |
} | |
public static List< Color > getNativeConsoleColors() { | |
List< Color > cc = new ArrayList< Color >(); | |
// Unbolded colors | |
cc.add( new Color( 0, 0, 0 ) ); | |
cc.add( new Color( 170, 0, 0 ) ); | |
cc.add( new Color( 0, 170, 0 ) ); | |
cc.add( new Color( 170, 85, 0 ) ); | |
cc.add( new Color( 0, 0, 170 ) ); | |
cc.add( new Color( 170, 0, 170 ) ); | |
cc.add( new Color( 0, 170, 170 ) ); | |
cc.add( new Color( 170, 170, 170 ) ); | |
// Bolded colors | |
cc.add( new Color( 85, 85, 85 ) ); | |
cc.add( new Color( 255, 85, 85 ) ); | |
cc.add( new Color( 85, 255, 85 ) ); | |
cc.add( new Color( 255, 255, 85 ) ); | |
cc.add( new Color( 85, 85, 255 ) ); | |
cc.add( new Color( 255, 85, 255 ) ); | |
cc.add( new Color( 85, 255, 255 ) ); | |
cc.add( new Color( 255, 255, 255 ) ); | |
return cc; | |
} | |
public static Color mixColors( Color c1, Color c2, double percent ) { | |
double invP = 1 - percent; | |
double r1 = c1.getRed() * invP; | |
double g1 = c1.getGreen() * invP; | |
double b1 = c1.getBlue() * invP; | |
double r2 = c2.getRed() * percent; | |
double g2 = c2.getGreen() * percent; | |
double b2 = c2.getBlue() * percent; | |
int r3 = ( int ) ( r1 + r2 ) / 2; | |
int g3 = ( int ) ( g1 + g2 ) / 2; | |
int b3 = ( int ) ( b1 + b2 ) / 2; | |
return new Color( r3, g3, b3 ); | |
} | |
public static Image resizeByWidth( Image input, int length ) { | |
BufferedImage image = toBufferedImage( input ); | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
double scale = width / ( double ) length; | |
return image.getScaledInstance( ( int ) ( width / scale ), ( int ) ( height / ( 2 * scale ) ), BufferedImage.SCALE_SMOOTH ); | |
} | |
public static BufferedImage getImage( String url ) throws IOException, MalformedURLException { | |
if ( !url.startsWith( "http" ) ) { | |
try { | |
return ImageIO.read( new File( url ) ); | |
} catch ( Exception exception ) { | |
exception.printStackTrace(); | |
} | |
} | |
BufferedImage image = null; | |
URLConnection connection = null; | |
try { | |
connection = new URL( url ).openConnection(); | |
} catch ( MalformedURLException exception ) { | |
throw exception; | |
} | |
try { | |
( ( HttpsURLConnection ) connection ).setInstanceFollowRedirects( true ); | |
} catch ( Exception exception ) { | |
( ( HttpURLConnection ) connection ).setInstanceFollowRedirects( true ); | |
} | |
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); | |
image = ImageIO.read( connection.getInputStream() ); | |
return image; | |
} | |
public static BufferedImage shadeImage( double scale, Image img ) { | |
RescaleOp op = new RescaleOp( ( float ) scale, 0, null ); | |
return op.filter( toBufferedImage( img ), null ); | |
} | |
public static BufferedImage toBufferedImage( Image img ) { | |
if (img instanceof BufferedImage) return ( BufferedImage ) img; | |
BufferedImage bimage = new BufferedImage( img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB ); | |
Graphics2D bGr = bimage.createGraphics(); | |
bGr.drawImage(img, 0, 0, null); | |
bGr.dispose(); | |
return bimage; | |
} | |
public static Color getBestMapColor( Color color ) { | |
Color best_color = BLACK; | |
double best_distance = -1.0D; | |
for (Color check_color : colors) { | |
double distance = getDistance(color, check_color); | |
if ((distance < best_distance) || (best_distance == -1.0D)) { | |
best_distance = distance; | |
best_color = check_color; | |
} | |
} | |
return best_color; | |
} | |
private static double getDistance(Color color_1, Color color_2) { | |
double red_avg = (color_1.getRed() + color_2.getRed()) / 2.0D; | |
double r = color_1.getRed() - color_2.getRed(); | |
double g = color_1.getGreen() - color_2.getGreen(); | |
int b = color_1.getBlue() - color_2.getBlue(); | |
double weight_red = 2.0D + red_avg / 256.0D; | |
double weight_green = 4.0D; | |
double weight_blue = 2.0D + (255.0D - red_avg) / 256.0D; | |
return weight_red * r * r + weight_green * g * g + weight_blue * b * b; | |
} | |
public static void dither(BufferedImage image) { | |
int width = image.getWidth(); | |
int height = image.getHeight(); | |
int[][] dither_buffer = new int[2][Math.max(width, height) * 3]; | |
int[] y_temps = { 0, 1, 1, 1 }; | |
for (int x = 0; x < width; x++) { | |
dither_buffer[0] = dither_buffer[1]; | |
dither_buffer[1] = new int[Math.max(width, height) * 3]; | |
for (int y = 0; y < height; y++) { | |
int rgb = image.getRGB(x, y); | |
Color color = new Color(rgb); | |
if (color.getAlpha() != 0) { | |
int red = color.getRed(); | |
int green = color.getGreen(); | |
int blue = color.getBlue(); | |
red = Math.max(0, Math.min(red + dither_buffer[0][(y * 3)], 255)); | |
green = Math.max(0, Math.min(green + dither_buffer[0][(y * 3 + 1)], 255)); | |
blue = Math.max(0, Math.min(blue + dither_buffer[0][(y * 3 + 2)], 255)); | |
Color matched_color = getBestMapColor(new Color(red, green, blue)); | |
int delta_r = red - matched_color.getRed(); | |
int delta_g = green - matched_color.getGreen(); | |
int delta_b = blue - matched_color.getBlue(); | |
int[] x_temps = { y + 1, y - 1, y, y + 1 }; | |
for (int i = 0; i < x_temps.length; i++) { | |
int temp_y = y_temps[i]; | |
int temp_x = x_temps[i]; | |
if ((temp_y < height) && (temp_x < width) && (temp_x > 0)) { | |
dither_buffer[temp_y][(temp_x * 3)] = ((int)(dither_buffer[temp_y][(temp_x * 3)] + color_multipliers[i] * delta_r)); | |
dither_buffer[temp_y][(temp_x * 3 + 1)] = ((int)(dither_buffer[temp_y][(temp_x * 3 + 1)] + color_multipliers[i] * delta_g)); | |
dither_buffer[temp_y][(temp_x * 3 + 2)] = ((int)(dither_buffer[temp_y][(temp_x * 3 + 2)] + color_multipliers[i] * delta_b)); | |
} | |
} | |
image.setRGB(x, y, matched_color.getRGB()); | |
} else { | |
image.setRGB(x, y, 0); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment