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)

Comments

Anonymous said…
Good work, man! ;)
O 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.
Tamim Shahriar 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.
FERO said…
Hi ,
I need to know whether python or jython able to do cross platform activity like accessing outlook 2007 objects (dll) and to send mail through outlook via source code.
send me the sample code to test it.

Best Regards,
Fernandez
fernandez10273@gmail.com
Unknown said…
i made one that can specifies where to get the pics and where to put the resized ones... The code is so simple and seems with yours.
wingi said…
Your script ignores the original aspect ratio - if you mix portait and landscapeformat image the result would be ugly. Seconds give the thumbnail method a try or look at united-coders.com a closer look.

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code