Sunday, January 4, 2009

Resize image before uploading

Where Internet connection is slow, it takes too much time to upload images. Few days ago I was trying to upload some images in facebook and it seemed to take forever. The I wrote the following python program to reduce the image size (around 4MB to around 100 KB!):


import Image
import os, sys

# adjust width and height to your needs
width = 800
height = 600

for root, dirs, files in os.walk('./'):
for name in files:
filename = os.path.join(root, name)
if filename.endswith('jpg'):
print filename
imin = Image.open(filename)
imout = imin.resize((width, height), Image.BICUBIC)
imout.save(filename)

5 comments:

barecube said...

Good work, man! ;)

BlÄ Filuren said...

If you have 10 dirs on the same level but only want to do it to the files in for example gfx_a gfx_b and gfx_c dirs and not dirs not starting with gfx can you do that with the walk?

Peppe said...

Hello, I wrote a program similar to yours with graphic interface, which let to resize all the images in a directory (including subfolders). If you like I can send you a link with the code.

subeen said...

Hello Peppe, in which language you wrote the program? if it's in Python, then I am really interested to get it with source code of course! Thanks.

Peppe said...

python...of course. I used wxPython for the graphical interface and PIL to manage images. Here is my post with the core code and here is the complete script.