Created
August 21, 2011 13:52
-
-
Save osima/1160636 to your computer and use it in GitHub Desktop.
image resize groovy
This file contains hidden or 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 | |
// 事前準備 | |
// resize メソッドを追加する | |
java.awt.Image.metaClass.resize = { mWidth,mHeight-> | |
def imgScaled = delegate.getScaledInstance(mWidth,mHeight,Image.SCALE_SMOOTH) | |
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() | |
img2 | |
} | |
// ここからがメイン | |
int width=100 // default width | |
// 1) 引数の処理 | |
def inputFile = new File(args[0]) | |
def outputFile = new File(args[1]) | |
if( args.length>2 ) | |
width = args[2] as Integer | |
// 2) 画像のリサイズ | |
def img = ImageIO.read(inputFile) | |
def imgScaled = img.resize(width,-1) | |
ImageIO.write(imgScaled,'PNG',outputFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment