Put this script in a particular directory (or change the directory specified in startDir variable
and it will recurse through all sub-directories, creating thumbnails of the images (jpg, png, gif, or bmp).
If you change the deleteOriginal variable to true, it will delete the original image for you afterwards.
import javax.imageio.*
import java.awt.*;
import java.awt.image.*;
def startDir = new File(".")
def deleteOriginal = false
def thumbWidth = 200
def thumbHeight = 150
startDir.eachFileRecurse{file->
if(file.name =~ /\.(JPG|jpg|PNG|png|GIF|gif|BMP|bmp)$/)
{
def imgType = file.name.replaceAll(/[^\.]*\.(JPG|jpg|PNG|png|GIF|gif|BMP|bmp)$/, "\$1")
def imgPath = file.getCanonicalPath().replaceAll(/\.(JPG|jpg|PNG|png|GIF|gif|BMP|bmp)$/, "_Thumb.\$1")
println("$imgType, $imgPath")
//load each image
img = ImageIO.read(file)
println (file.name + " " + img.width + "x" + img.height)
//output scaled thumbnail image
width = thumbWidth
height = thumbHeight
if(img.width < img.height){width = thumbHeight; height = thumbWidth;}
BufferedImage thumbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D gThumbImg = thumbImg.createGraphics();
gThumbImg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
gThumbImg.drawImage(img, 0, 0, width, height, null);
ImageIO.write(thumbImg, "$imgType", new File(imgPath))
if(deleteOriginal)
{
file.delete()
}
}
}
This entry was posted on Sunday, October 4th, 2009 at 10:45 am and is filed under Groovy. You can follow any responses to this entry through the RSS 2.0 feed. RSS 2.0. You can leave a response, or trackback from your own site.





