Last active
August 29, 2015 14:13
-
-
Save yshrsmz/286f3e98e6b8ececcea4 to your computer and use it in GitHub Desktop.
Requirement: ImageMagick
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
@Grapes([ | |
@Grab(group='org.im4java', module='im4java', version='1.4.0'), | |
@Grab(group='io.reactivex', module='rxjava', version='1.0.4') | |
]) | |
import java.math.RoundingMode | |
import java.util.* | |
import org.im4java.core.* | |
import org.im4java.process.* | |
import static groovy.io.FileType.FILES | |
import rx.Observable | |
def SRC_DIRECTORY = '../app/src/main/res/drawable-xxhdpi/' | |
def SRC_RATIO = 3 | |
def FILE_PATTERN = '**/*.png' | |
def OUTPUTS = [ | |
[ | |
ratio: 1, | |
dir: 'output/mdpi' | |
], | |
[ | |
ratio: 2, | |
dir: 'output/xhdpi' | |
], | |
[ | |
ratio: 3, | |
dir: 'output/xxhdpi' | |
] | |
] | |
/** | |
* get width/height of provided image | |
* @param path | |
* @return | |
*/ | |
Map identify(path) { | |
def KEY_GEOMETRY = ' Geometry: ' | |
def KEY_SIZE_END = '+' | |
def KEY_SIZE_SEPARATOR = 'x' | |
op = new IMOperation() | |
op.verbose() | |
op.addImage path | |
identify = new IdentifyCmd() | |
output = new ArrayListOutputConsumer() | |
identify.setOutputConsumer output | |
identify.run op | |
def result = [width: 0, height: 0] | |
output.output.each { line -> | |
if (line.startsWith(KEY_GEOMETRY) && line.indexOf(KEY_SIZE_END) != -1) { | |
size = line.substring(KEY_GEOMETRY.length(), line.indexOf(KEY_SIZE_END)) | |
.split KEY_SIZE_SEPARATOR | |
result = [ width: Integer.valueOf(size[0]), height: Integer.valueOf(size[1])] | |
} | |
} | |
result | |
} | |
/** | |
* resize | |
* @param ratio | |
* @param sourceData | |
* @param outputDir | |
* @return | |
*/ | |
def scale(BigDecimal ratio, sourceData, outputDir) { | |
file = new File(sourceData.path) | |
outputFile = new File(outputDir, file.name) | |
BigDecimal width = sourceData.width * ratio | |
BigDecimal height = sourceData.height * ratio | |
convert = new ConvertCmd() | |
op = new IMOperation() | |
op.addImage sourceData.path | |
op.resize width.setScale(0, RoundingMode.CEILING).intValue(), | |
height.setScale(0, RoundingMode.CEILING).intValue(), | |
'^' | |
op.unsharp 2, 1.4, 0.5, 0 | |
op.addImage outputFile.absolutePath | |
convert.run op | |
} | |
/** | |
* get list of absolute path matching provided pattern | |
* @param srcDir | |
* @param pattern | |
* @return | |
*/ | |
String[] findFile(srcDir, pattern) { | |
String[] files = new FileNameFinder().getFileNames(srcDir, pattern) | |
return files | |
} | |
def directory = new File(SRC_DIRECTORY) | |
if (!directory.isDirectory()) { | |
println "provided directory name $SRC_DIRECTORY is NOT directory" | |
System.exit(-2) | |
} | |
OUTPUTS.each { new File(it.dir).mkdirs() } | |
Observable.from(findFile(SRC_DIRECTORY, FILE_PATTERN)) | |
.map { | |
def imgData = identify it | |
imgData.path = it | |
return imgData | |
} | |
.subscribe { | |
println "${it.path}, ${it.width}, ${it.height}" | |
OUTPUTS.each { outputData -> | |
scale(outputData.ratio / SRC_RATIO, it, outputData.dir) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment