Skip to content

Instantly share code, notes, and snippets.

@osima
Created January 16, 2012 15:29
Show Gist options
  • Save osima/1621382 to your computer and use it in GitHub Desktop.
Save osima/1621382 to your computer and use it in GitHub Desktop.
resize all png images in current dir.
import java.awt.Image
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
class Conv {
File inputFile
File outputFile
void proc(){
int width=500 // default width
// イメージの読み込みとリサイズ
def img = ImageIO.read(inputFile)
def imgScaled = img.getScaledInstance(width,-1,Image.SCALE_SMOOTH)
// リサイズ(スケール)されたイメージを直接 ImageIO.write() できないので、処理を追加
def img2 = new BufferedImage((int)imgScaled.width,(int)imgScaled.height,BufferedImage.TYPE_4BYTE_ABGR)
def g = img2.getGraphics();
g.drawImage(imgScaled,0,0,null)
g.dispose()
// リサイズされたイメージを保存
ImageIO.write(img2,'PNG',outputFile)
}
}
new File('.').listFiles( { it.isFile() && it.name.endsWith('png') } as FileFilter ).each{
new Conv( inputFile:it,outputFile:it ).proc()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment