import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class ImageIOTest {
public static void main( String[] args ){
BufferedImage img = null; // buffer type
try {
// Name of file and directories
String name = "MonaLisa";
String in = "images/";
String out = "images/tmp/";
// Either use URL or File for reading image using ImageIO
File imageFile = new File(in + name + ".png");
img = ImageIO.read(imageFile); // set buffer of image data
// ImageIO Image write to gif in Java
// Documentation https://docs.oracle.com/javase/tutorial/2d/images/index.html
ImageIO.write(img, "gif", new File(out + name + ".gif") ); // write buffer to gif
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Success");
}
}
ImageIOTest.main(null);
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class ImageIOTest {
public static void main( String[] args ) throws Exception {
BufferedImage img = null; // buffer type
try {
// Name of file and directories
String name = "RGB";
String in = "images/";
String out = "images/smp/";
// Either use URL or File for reading image using ImageIO
File imageFile = new File(in + name + ".png");
img = ImageIO.read(imageFile); // set buffer of image data
File outputfile = new File(out + name + ".jpg");
// ImageIO Image write to gif in Java
// Documentation https://docs.oracle.com/javase/tutorial/2d/images/index.html
ImageIO.write(img, "jpg", outputfile ); // write buffer to gif
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Success");
}
}
ImageIOTest.main(null);
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
public class Pics {
private final String inDir = "images/"; // location of images
private final String outDir = "images/tmp/"; // location of created files
private String inFile;
private String resizedFile;
private String asciiFile;
private String ext; // extension of file
private long bytes;
private int width;
private int height;
// Constructor obtains attributes of picture
public Pics(String name, String ext) {
this.ext = ext;
this.inFile = this.inDir + name + "." + ext;
this.resizedFile = this.outDir + name + "." + ext;
this.asciiFile = this.outDir + name + ".txt";
this.setStats();
}
// An image contains metadata, namely size, width, and height
public void setStats() {
BufferedImage img;
try {
Path path = Paths.get(this.inFile);
this.bytes = Files.size(path);
img = ImageIO.read(new File(this.inFile));
this.width = img.getWidth();
this.height = img.getHeight();
} catch (IOException e) {
}
}
// Console print of data
public void printStats(String msg) {
System.out.println(msg + ": " + this.bytes + " " + this.width + "x" + this.height + " " + this.inFile);
}
// Convert scaled image into buffered image
public static BufferedImage convertToBufferedImage(Image img) {
// Create a buffered image with transparency
BufferedImage bi = new BufferedImage(
img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
// magic?
Graphics2D graphics2D = bi.createGraphics();
graphics2D.drawImage(img, 0, 0, null);
graphics2D.dispose();
return bi;
}
// Scale or reduce to "scale" percentage provided
public void resize(int scale) {
BufferedImage img = null;
Image resizedImg = null;
int width = (int) (this.width * (scale/100.0) + 0.5);
int height = (int) (this.height * (scale/100.0) + 0.5);
try {
// read an image to BufferedImage for processing
img = ImageIO.read(new File(this.inFile)); // set buffer of image data
// create a new BufferedImage for drawing
resizedImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
} catch (IOException e) {
return;
}
try {
ImageIO.write(convertToBufferedImage(resizedImg), this.ext, new File(resizedFile));
} catch (IOException e) {
return;
}
this.inFile = this.resizedFile; // use scaled file vs original file in Class
this.setStats();
}
//
// convert every pixel to an ascii character (ratio does not seem correct)
public void convertToAscii() {
BufferedImage img = null;
PrintWriter asciiPrt = null;
FileWriter asciiWrt = null;
try {
File file = new File(this.asciiFile);
Files.deleteIfExists(file.toPath());
} catch (IOException e) {
System.out.println("Delete File error: " + e);
}
try {
asciiPrt = new PrintWriter(asciiWrt = new FileWriter(this.asciiFile, true));
} catch (IOException e) {
System.out.println("ASCII out file create error: " + e);
}
try {
img = ImageIO.read(new File(this.inFile));
} catch (IOException e) {
}
for (int i = 0; i < (img.getHeight()); i++) {
int num = i%2;
if (num == 0) {
for (int j = 0; j < img.getWidth(); j++) {
Color col = new Color(img.getRGB(j, i));
double pixVal = ((((col.getRed()/255.0) * 0.30) + ((col.getBlue()/255.0) * 0.59) + ((col
.getGreen()/255.0) * 0.11)));
try {
asciiPrt.print(asciiChar(pixVal));
asciiPrt.flush();
asciiWrt.flush();
} catch (Exception ex) {
}
}
try {
asciiPrt.println("");
asciiPrt.flush();
asciiWrt.flush();
} catch (Exception ex) {
}
}
}
}
// conversion table, there may be better out there ie https://www.billmongan.com/Ursinus-CS173-Fall2020/Labs/ASCIIArt
public String asciiChar(double g) {
double[] GRAYSCALE_VALUES = {0, 0.07863, 0.08205, 0.1316, 0.1453, 0.147, 0.1504, 0.1641, 0.1761, 0.1761, 0.1778, 0.2017, 0.2154, 0.2274, 0.2274, 0.2291, 0.2427, 0.2598, 0.2615, 0.2684, 0.2752, 0.2769, 0.2889, 0.2923, 0.294, 0.2974, 0.3026, 0.3077, 0.3077, 0.3094, 0.3162, 0.3197, 0.3333, 0.3333, 0.3385, 0.3538, 0.3607, 0.3795, 0.3846, 0.4017, 0.4034, 0.4068, 0.4291, 0.4325, 0.4444, 0.4598, 0.4632, 0.4667, 0.4752, 0.4855, 0.5077, 0.5128, 0.5128, 0.5128, 0.5179, 0.5214, 0.5231, 0.535, 0.535, 0.5368, 0.5385, 0.5436, 0.5624, 0.5624, 0.5624, 0.5641, 0.5778, 0.5829, 0.5846, 0.5863, 0.5863, 0.5915, 0.6017, 0.6068, 0.641, 0.6581, 0.6889, 0.694, 0.6974, 0.7333, 0.7333, 0.7402, 0.7675, 0.7744, 0.7863, 0.8068, 0.8342, 0.8427, 0.8598, 0.894, 0.947, 0.959, 0.9675, 1};
String GRAYSCALE_CHARS = "@#BW8gMN6&9RQqE0d$DbpmOa5AH2GZeKPS3X4U%kwohFVyzIsCu{xfn}tJTi17c=jYL?l[]v<>+|)r(/\\*_!;^~\":,-'.`";
double distance = Math.abs(GRAYSCALE_VALUES[0] - g);
int idx = 0;
for(int c = 1; c < GRAYSCALE_VALUES.length; c++){
double cdistance = Math.abs(GRAYSCALE_VALUES[c] - g);
if(cdistance < distance){
idx = c;
distance = cdistance;
}
}
char yes = GRAYSCALE_CHARS.charAt(93-idx);
String s = "" + yes;
return s;
}
// tester/driver
public static void main(String[] args) throws IOException {
Pics monaLisa = new Pics("MonaLisa", "png");
monaLisa.printStats("Original");
monaLisa.resize(33);
monaLisa.printStats("Scaled");
monaLisa.convertToAscii();
Pics pumpkin = new Pics("pumpkin", "png");
pumpkin.printStats("Original");
pumpkin.resize(33);
pumpkin.printStats("Scaled");
pumpkin.convertToAscii();
Pics briabanner = new Pics("briabanner", "png");
briabanner.printStats("Original");
briabanner.resize(33);
briabanner.printStats("Scaled");
briabanner.convertToAscii();
Pics teamlogo = new Pics("teamlogo", "png");
teamlogo.printStats("Original");
teamlogo.resize(33);
teamlogo.printStats("Scaled");
teamlogo.convertToAscii();
Pics aadya = new Pics("aadya", "png");
aadya.printStats("Original");
aadya.resize(33);
aadya.printStats("Scaled");
aadya.convertToAscii();
Pics mort2 = new Pics("mort2", "png");
mort2.printStats("Original");
mort2.resize(33);
mort2.convertToAscii();
}
}
Pics.main(null);
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.ImageReader;
import javax.imageio.ImageTypeSpecifier;
public class Pics {
private final String inDir = "images/"; // location of images
private final String outDir = "images/tmp/"; // location of created files
private final String greyDir = "images/tmp/greyscale/";
private String inFile;
private String resizedFile;
private String asciiFile;
private String ext; // extension of file
private long bytes;
private int width;
private int height;
private String greyscaleFile;
// Constructor obtains attributes of picture
public Pics(String name, String ext) {
this.ext = ext;
this.inFile = this.inDir + name + "." + ext;
this.resizedFile = this.outDir + name + "." + ext;
this.asciiFile = this.outDir + name + ".txt";
this.greyscaleFile = this.greyDir + name + "_grey" + "." + ext;
this.setStats();
}
// An image contains metadata, namely size, width, and height
public void setStats() {
BufferedImage img;
try {
Path path = Paths.get(this.inFile);
this.bytes = Files.size(path);
img = ImageIO.read(new File(this.inFile));
this.width = img.getWidth();
this.height = img.getHeight();
} catch (IOException e) {
}
}
// Console print of data
public void printStats(String msg) {
System.out.println(msg + ": " + this.bytes + " " + this.width + "x" + this.height + " " + this.inFile);
}
// Convert scaled image into buffered image
public static BufferedImage convertToBufferedImage(Image img) {
// Create a buffered image with transparency
BufferedImage bi = new BufferedImage(
img.getWidth(null), img.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
// magic?
Graphics2D graphics2D = bi.createGraphics();
graphics2D.drawImage(img, 0, 0, null);
graphics2D.dispose();
return bi;
}
// Scale or reduce to "scale" percentage provided
public void resize(int scale) {
BufferedImage img = null;
Image resizedImg = null;
int width = (int) (this.width * (scale/100.0) + 0.5);
int height = (int) (this.height * (scale/100.0) + 0.5);
try {
// read an image to BufferedImage for processing
img = ImageIO.read(new File(this.inFile)); // set buffer of image data
// create a new BufferedImage for drawing
resizedImg = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
} catch (IOException e) {
return;
}
try {
ImageIO.write(convertToBufferedImage(resizedImg), this.ext, new File(resizedFile));
} catch (IOException e) {
return;
}
this.inFile = this.resizedFile; // use scaled file vs original file in Class
this.setStats();
}
//
// convert every pixel to an ascii character (ratio does not seem correct)
public void greyscale() {
BufferedImage img = null;
BufferedImage greyImg = null;
try {
img = ImageIO.read(new File(this.inFile));
greyImg= img;
for (int i = 0; i < (img.getHeight()); i++) {
for (int j = 0; j < img.getWidth(); j++) {
Color col = new Color(img.getRGB(j, i));
int pixVal = ((col.getRed()+ col.getBlue() + col.getGreen())/3);
Color color = new Color(pixVal, pixVal, pixVal);
greyImg.setRGB(j, i, color.getRGB());
}
}
} catch (IOException e) {
return;
}
try {
ImageIO.write(convertToBufferedImage(greyImg), this.ext, new File(greyscaleFile));
} catch (IOException e) {
return;
}
this.inFile = this.greyscaleFile;
this.setStats();
}
// conversion table, there may be better out there ie https://www.billmongan.com/Ursinus-CS173-Fall2020/Labs/ASCIIArt
// tester/driver
public static void main(String[] args) throws IOException {
Pics monaLisa = new Pics("MonaLisa", "png");
monaLisa.printStats("Original");
monaLisa.resize(33);
monaLisa.printStats("Scaled");
monaLisa.greyscale();
Pics pumpkin = new Pics("pumpkin", "png");
pumpkin.printStats("Original");
pumpkin.resize(33);
pumpkin.printStats("Scaled");
Pics briabanner = new Pics("briabanner", "png");
briabanner.printStats("Original");
briabanner.resize(33);
briabanner.printStats("Scaled");
briabanner.greyscale();
}
}
Pics.main(null);