After my holiday I captured hundreds of photos. I want to upload some of them to my online photo album. Before uploading the photos I need to scale the photos to a smaller size to speed up the file transfer and save my internet bandwidth. A popular bulk image processor - BIMP can be found from Cerebral Synergy.
I have some pretty unique requirements to bulk scale these photos. They have been captured using different cameras - N95, XM5800, Canon, Olympus, Pentax, etc. - each having different image resolutions and therefore sizes. So I cannot just apply a single factor to all the pictures. Also, I have some panorama pictures shot with Panoman which are very wide. So I cannot apply a single size to all pictures - I need to keep the aspect ratio. I decided to write a little bulk image scaler myself.
The basic image resizer is pretty simple using Java:
... BufferedImage img=ImageIO.read(new File(fileName)); if(img==null) return; int w=(int)(img.getWidth()*factor); int h=(int)(img.getHeight()*factor); String imageType=getFileSuffix(fileName); Image scaledImg=img.getScaledInstance(w, h, Image.SCALE_DEFAULT); BufferedImage bi=new BufferedImage(w, h, img.getType()); bi.createGraphics().drawImage(scaledImg, 0, 0,null); ImageIO.write(bi, imageType, new File(newFileName)); ...
It is not the most efficient way to do this but it is simple and works. To keep aspect ratio is simply a matter of using the new width and the original picture's aspect ratio to calculate the new height:
double ratio=(double)img.getHeight() / img.getWidth(); int h=(int) (ratio*w);
The full source code is PhotoScaler.java and ScaleParameters.java. It still needs some refactoring and cleaning up. The usage of the utility:
Usage: java com.laws.photo.PhotoScaler directory_name [-f scale_factor] | [-s w h] | [-k w] -f scale by factor -s scale by size, i.e. width and height -k keep aspect ratio, use w for width and calculate the height e.g. java PhotoScaler c:\temp\photos -f 0.5 java PhotoScaler /tmp/photos -s 800 600 java PhotoScaler c:\temp\photos -k 800