Skip to content

Instantly share code, notes, and snippets.

@mykelangelo
Created September 12, 2020 17:14
Show Gist options
  • Save mykelangelo/ec83f76be0dab3da80b01bb8bca04a98 to your computer and use it in GitHub Desktop.
Save mykelangelo/ec83f76be0dab3da80b01bb8bca04a98 to your computer and use it in GitHub Desktop.
First we resize, then we convert all sizes to all formats
package com.papenko.imgresizing;
import org.imgscalr.Scalr;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class Main {
private static final int INITIAL_VARIABLES_LENGTH = 4;
public static void main(String[] args) {
if (args.length < INITIAL_VARIABLES_LENGTH) {
System.out.println("arguments array length is less than " + INITIAL_VARIABLES_LENGTH);
return;
}
int sizes = Integer.parseInt(args[0]);
int formats = Integer.parseInt(args[1]);
String pathName = args[2];
String newPath = args[3];
if (sizes < 0 || formats < 0) {
System.out.println("sizes or formats < 0");
return;
}
if (sizes + formats == 0) {
System.out.println("nothing to do");
return;
}
if (args.length < sizes + formats + INITIAL_VARIABLES_LENGTH) {
System.out.println("arguments array length is less than " + sizes + " sizes + " + formats + " formats + " +
INITIAL_VARIABLES_LENGTH);
return;
}
new File(newPath).mkdirs();
String fileNameWithoutExtension;
int lastSlash = pathName.lastIndexOf('/');
final int lastDot = pathName.lastIndexOf('.');
if (lastSlash == -1) {
fileNameWithoutExtension = pathName.substring(0, lastDot);
} else {
fileNameWithoutExtension = pathName.substring(lastSlash, lastDot);
}
String formatName = pathName.substring(lastDot + 1);
String newPathNameWithoutExtension = newPath + (newPath.endsWith("/") ? "" : "/") + fileNameWithoutExtension;
Map<String, BufferedImage> resizedImages = new HashMap<>(sizes);
System.out.println("Started resizing");
for (int i = 0; i < sizes; i++) {
String sizeStr = args[INITIAL_VARIABLES_LENGTH + i];
String newPathName = newPathNameWithoutExtension + sizeStr + '.' + formatName;
resizedImages.put(sizeStr, resize(pathName, formatName, newPathName, Integer.parseInt(sizeStr)));
}
System.out.println("Ended resizing");
System.out.println("Started converting");
for (int i = 0; i < formats; i++) {
String newFormatName = args[INITIAL_VARIABLES_LENGTH + sizes + i];
String newPathName = newPathNameWithoutExtension + '.' + newFormatName;
convert(pathName, newPathName, newFormatName);
for (Map.Entry<String, BufferedImage> resized : resizedImages.entrySet()) {
String newPathNameResized = newPathNameWithoutExtension + resized.getKey() + '.' + newFormatName;
convert(resized.getValue(), newPathNameResized, newFormatName);
}
}
System.out.println("Ended converting");
}
private static BufferedImage resize(String oldPathName, String formatName, String newPathName, int targetSize) {
try {
BufferedImage src = ImageIO.read(Files.newInputStream(Paths.get(oldPathName)));
BufferedImage resized = Scalr.resize(src, targetSize);
File file = new File(newPathName);
file.createNewFile();
ImageIO.write(resized, formatName, file);
return resized;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void convert(String oldPathName, String newPathName, String newFormatName) {
File inputFile = new File(oldPathName);
File outputFile = new File(newPathName);
try (InputStream is = new FileInputStream(inputFile)) {
BufferedImage image = ImageIO.read(is);
try (OutputStream os = new FileOutputStream(outputFile)) {
ImageIO.write(image, newFormatName, os);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void convert(BufferedImage image, String newPathName, String newFormatName) {
try (OutputStream os = new FileOutputStream(new File(newPathName))) {
ImageIO.write(image, newFormatName, os);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment