1

I have a lot of images that taken by my Digital camera with very high resolution 3000 * 4000 and it takes a lot of Hard disk space, I used Photoshop to open each Image and re-size it o be with small resolution, but it needs a lot of time and effort

I think that I can write simple program that open the folder of images and read each file and get it's width and height and if it's very high change it and overwrite the image with the small one.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • 1
    Photoshop has batch actions: http://www.digital-photography-school.com/how-to-batch-resize-in-photoshop . If you still want to DIY in Java, look at BufferedImage – I82Much May 06 '11 at 18:37
  • 6
    Are you sure you want to use Java? Something like [ImageMagick](http://www.imagemagick.org/script/index.php) is likely much better suited for this task. Even if you have to go through Cygwin. – Jim Mitchener May 06 '11 at 18:38
  • If there is any other easy solution I will be happy doing it :D – Amira Elsayed Ismail May 06 '11 at 18:39
  • 2
    if you're on windows, there's this [Image Resizer powertoy](http://windows.microsoft.com/en-US/windows/downloads/windows-xp?T1=PT). This can help convert all images at one shot. – asgs May 06 '11 at 18:59
  • I have tried to use The embedded image processor in Photoshop , but I didn't find it , I'm using Photoshop 8 , so could you please specify which version of Photoshop – Amira Elsayed Ismail May 06 '11 at 18:59
  • Use the Image Resizer Powertoy, that's the ultimate and all the other methods are overkills. – adarshr May 06 '11 at 19:06
  • [`ImageJ`](http://rsbweb.nih.gov/ij/) is easy to script for this. – trashgod May 06 '11 at 19:11

5 Answers5

7

Here some code I use in a Java-EE project (should work in normal application to:

int rw = the width I needed;

BufferedImage image = ImageIO.read(new File(filename));

ResampleOp  resampleOp = new ResampleOp(rw,(rw * image.getHeight()) / image.getWidth() );
resampleOp.setFilter(ResampleFilters.getLanczos3Filter()); 
image = resampleOp.filter(image, null);

File tmpFile = new File(tmpName);
ImageIO.write(image, "jpg", tmpFile);

The resample filter comes from java-image-scaling library. It also contains BSpline and Bicubic filters among others if you don't like the Lanczos3. If the images are not in sRGB color space Java silently converts the color space to sRGB (which accidentally was what I needed).

Also Java loses all EXIF data, thought it does provide some (very hard to use) methods to retrieve it. For color correct rendering you may wish to at least add a sRGB flag to the file. For that see here.

Community
  • 1
  • 1
Eelke
  • 20,897
  • 4
  • 50
  • 76
3

+1 to what some of the other folks said about not specifically needing Java for this, but I imagine you must have known this and were maybe asking because you either wanted to write such a utility or thought it would be fun?

Either way, getting the image file listing from a dir is straight forward, resizing them correctly can take a bit more leg work as you'll notice from Googling for best-practices and seeing about 9 different ways to actually resize the files.

I wrote imgscalr to address this exact issue; it's a dead-simple API (single class, bunch of static methods) and has some good adoption in webapps and other tools utilizing it.

Steps to resize would look like this (roughly):

  1. Get file list
  2. BufferedImage image = ImageIO.read(files[i]);
  3. image = Scalr.resize(image, width);
  4. ImageIO.write(image);

There are a multitude of "resize" methods to call on the Scalr class, and all of them honor the image's original proportions. So if you scale only using a targetWidth (say 1024 pixels) the height will be calculated for you to make sure the image still looks exactly right.

If you scale with width and height, but they would violate the proportions of the image and make it look "Stretched", then based on the orientation of the image (portrait or landscape) one dimension will be used as the anchor and the other incorrect dimension will be recalculated for you transparently.

There are also a multitude of different Quality settings and FIT-TO scaling modes you can use, but the library was designed to "do the right thing" always, so using it is very easy.

You can dig through the source, it is all Apache 2 licensed. You can see that it implements the Java2D team's best-practices for scaling images in Java and pedantically cleans up after itself so no memory gets leaked.

Hope that helps.

Riyad Kalla
  • 10,604
  • 7
  • 53
  • 56
1

AffineTransformOp offers the additional flexibility of choosing the interpolation type, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

You do not need Java to do this. It's a waste of time and resources. If you have photoshop you can do it with recording actions: batch resize using actions

sepo
  • 394
  • 1
  • 8
  • Yeah that doesn't work if you are distributing an application. I am not going to make my users spend hundreds of dollars just so I can use photoshops batch features. – Codeguy007 Jun 01 '13 at 12:29
0

You can individually or batch resize with our desktop image resizing application called Sizester. There's a full functioning 15-day free trial on our site (www.sizester.com).

Marko
  • 20,385
  • 13
  • 48
  • 64
Dan
  • 1