Created
August 1, 2011 15:42
-
-
Save nobusue/1118384 to your computer and use it in GitHub Desktop.
自炊ヘルパーその1: JPEGファイルを縦横半分にリサイズ、指定したフォルダ以下を階層的に処理
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.Image | |
import java.awt.image.BufferedImage | |
import javax.imageio.ImageIO | |
def rootDir = new File(args[0]) | |
rootDir.eachFileRecurse { file -> | |
if(file.name ==~ /[^s].*\.jpg/) { // skip file starting with "s" | |
def srcFileName = file.name | |
def path = file.absoluteFile.parent | |
def dstFileName = 's' + srcFileName | |
println "Shrinking: ${path}\\${srcFileName}" | |
resizeImage(file, "${path}/${dstFileName}") | |
file.delete() | |
} | |
} | |
def resizeImage(srcFile, dstPath) { | |
def image = ImageIO.read(srcFile) | |
def w = image.width.intdiv(2) | |
def h = image.height.intdiv(2) | |
def resizedImage = new BufferedImage(w, h, image.type) | |
resizedImage.getGraphics().drawImage( | |
image.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING), | |
0, 0, w, h, null) | |
ImageIO.write(resizedImage, "jpeg", new File(dstPath)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment