views:

45

answers:

3

Hello. I have a image from

files = request.FILES['new_photo'].read()

and i want to know it's size and possibly resize it. How i can do it?

thanks.

UPD

before. i read() file and gave this string and some info? to SQL function. And she save it on ftp.

how i can get data same as the data returned from read() method?

i try to use Image.tostring()

and save it. But i have 0x0px image.

+2  A: 

you can use PIL for processing images . http://www.pythonware.com/products/pil/

ozk
Yes. But how? i don't don't know image size. and function like fromString() have size argument
Falcon
use StringIO:img = Image.open(StringIO(files))after which:img.size contains the size of the image.
ozk
+3  A: 

Here is a small snippet from one of my web applications (sightly modified), which creates a thumbnail with the maximum size of 200x200 pixels. Maybe you can use some parts of it:

from PIL import Image

image = request.FILES['new_photo']
if image:
    img = Image.open(image)
    img.save(path.join(app.config['UPLOAD_FOLDER'],
        secure_filename('upload-%d.jpg' % self.obj.id)), 'JPEG')
    img.thumbnail((200, 200), Image.ANTIALIAS)
    img.save(path.join(app.config['UPLOAD_FOLDER'],
        secure_filename('upload-%d.200.jpg' % self.obj.id)), 'JPEG')
tux21b
thanks. i update
Falcon
A: 

User one of the thumbnail libraries available such as http://djangothumbnails.com/ or more http://code.djangoproject.com/wiki/ThumbNails

Frank Malina