Skip to content

Instantly share code, notes, and snippets.

@paramsen
Last active January 15, 2018 12:06
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 paramsen/5f32a92220e20d349365c4dc7a313aa9 to your computer and use it in GitHub Desktop.
Save paramsen/5f32a92220e20d349365c4dc7a313aa9 to your computer and use it in GitHub Desktop.
[Android util] [kotlin script] Batch convert Android res from xxxhdpi to *dpi sizes using imagemagick
/**
* Batch conversion from one dpi to the others using imagemagick
* (got some Windows and personal machine hardcoded stuff, change that)
*
* Usage: kotlinc -script batch-convert-dpi.kts <output dir> <paths to input files (pngs)>
*/
import java.io.File
import jdk.nashorn.internal.runtime.ScriptingFunctions.readLine
import java.io.BufferedReader
import java.io.InputStreamReader
val output = File(args[0])
val conversions = arrayOf(File(output, "xxxhdpi") to 1.0, File(output, "xxhdpi") to .75, File(output, "xhdpi") to .5, File(output, "hdpi") to .375, File(output, "mdpi") to .25)
conversions.forEach { it.first.mkdir() }
conversions.forEach { (dir, factor) ->
args.drop(1).filter{ it.endsWith(".png") }.map{ File(it) }.forEach {
val dest = "$dir\\${it.name}"
println("$it $dest $dir $factor")
println("convert -resize ${Math.round(100.0 * factor)}% ${it.path} $dest")
val proc = Runtime.getRuntime().exec(arrayOf("C:\\cygwin64\\bin\\convert.exe", "-resize", "${Math.round(100.0 * factor)}%", it.path, dest))
val stdError = BufferedReader(InputStreamReader(proc.errorStream))
var s: String?
val sb = StringBuilder()
while (true) {
s = stdError.readLine()
if(s == null) break
sb.appendln(s)
}
if(sb.isNotEmpty()) System.err.println("\u001B[31m$sb\u001B[0m")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment