6

I have a directory full of images that I would like to resize to around 60% of their original size.

How would I go about doing this? Can be in either Python or Perl

Cheers

Eef

Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
RailsSon
  • 19,897
  • 31
  • 82
  • 105
  • I'm not sure. First of all - it doesn't say it's one time job. Second - "Irfan View" might not be an option - for example due to operating system constraints. Third - ImageMagick is so complex, that even using it as a batch tool is kind of programming. –  Jun 26 '09 at 14:05

6 Answers6

17

If you want to do it programatically, which I assume is the case, use PIL to resize e.g.

newIm = im.resize((newW, newH)

then save it to same file or a new location.

Go through the folder recursively and apply resize function to all images.

I have come up with a sample script which I think will work for you. You can improve on it: Maybe make it graphical, add more options e.g. same extension or may be all png, resize sampling linear/bilinear etc

import os
import sys
from PIL import Image

def resize(folder, fileName, factor):
    filePath = os.path.join(folder, fileName)
    im = Image.open(filePath)
    w, h  = im.size
    newIm = im.resize((int(w*factor), int(h*factor)))
    # i am saving a copy, you can overrider orginal, or save to other folder
    newIm.save(filePath+"copy.png")

def bulkResize(imageFolder, factor):
    imgExts = ["png", "bmp", "jpg"]
    for path, dirs, files in os.walk(imageFolder):
        for fileName in files:
            ext = fileName[-3:].lower()
            if ext not in imgExts:
                continue

            resize(path, fileName, factor)

if __name__ == "__main__":
    imageFolder=sys.argv[1] # first arg is path to image folder
    resizeFactor=float(sys.argv[2])/100.0# 2nd is resize in %
    bulkResize(imageFolder, resizeFactor)
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
11

How about using mogrify, part of ImageMagick? If you really need to control this from Perl, then you could use Image::Magick, Image::Resize or Imager.

mirod
  • 15,923
  • 3
  • 45
  • 65
10

Can it be in shell?

mkdir resized
for a in *.jpg; do convert "$a" -resize 60% resized/"$a"; done

If you have > 1 core, you can do it like this:

find . -maxdepth 1 -type f -name '*.jpg' -print0 | xargs -0 -P3 -I XXX convert XXX -resize 60% resized/XXX

-P3 means that you want to resize up to 3 images at the same time (parallelization).

If you don't need to keep originals you can use mogrify, but I prefer to use convert, and then rm ...; mv ... - just to be on safe side if resizing would (for whatever reason) fail.

2

Use PerlMagick, it's an interface to the popular ImageMagick suite of command line tools to do just this kind of stuff. PythonMagic is available as well.

UncleZeiv
  • 18,272
  • 7
  • 49
  • 77
1

I use Python with PIL (Python Image Library). Of course there are specialized programs to do this.

Many people use PIL to such things. Look at: Quick image resizing with python

PIL is very powerful and recently I have found this recipe: Putting watermark to images in batch

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
0

do you need to just resize it or you want to resize programmatically? If just resize use PixResizer. http://bluefive.pair.com/pixresizer.htm

Ganesh R.
  • 4,337
  • 3
  • 30
  • 46