Python Tornado framework for image upload and image size modification

Source: Internet
Author: User
Tornado is an asynchronous PythonWeb development framework and an excellent asynchronous server development library, here we will explain some of the key points of the Python Tornado framework for image upload and image size modification: Image Upload

The uploaded image uses form submission. The following is the html part. enctype = "multipart/form-data" indicates that bytes are not encoded. you must specify the file type when uploading files. the type = "file" of the input tag specifies the upload type.

 

Below is the part of the tornado acceptance file

Class UploadHandler (BaseHandler): def post (self): # This part is the uploaded file. to view more, you can print self. request to see # This file returns a dictionary list of elements imgfile = self. request. files. get ('uploadmg ') for img in imgfile: # img has three key-value pairs available through img. keys () view # 'filename', 'body', and 'content _ type' obviously correspond to the file name, content (binary), and file type with open ('. /static/uploads/'+ img ['filename'], 'wb') as f: # save the file content to'/static/uploads/{filename} 'f. write (f ['body'])

In this way, access through/static/uploads/file name

Modify the image size

The following describes how to resize an image.

Class UploadHandler (BaseHandler): @ tornado. web. authenticated def post (self): # It should be written above. in order to display the import time written to the function # PIL is the python module for image operations, if you are interested, please take a look at from PIL import Image # you can use it like a file, but it is stored in the memory from cStringIO import StringIO # determine the size of the uploaded file size = int (self. request. headers. get ('content-length') if size/1000.0> 2000: self. write ("The size of the uploaded image cannot exceed 2 MB. ") imgfile = self. request. files. get ('uploadmg ') for img in imgfile: # rename the file name = str (time. strftime ('% Y % m % d %'), time. localtime () \ + '_' + self. current_user + '_headimg.png 'with open ('. /static/uploads/'+ name, 'WB') as f: # image can be opened in multiple ways. one is Image.open('xx.png ') # The other is Image. open (StringIO (buffer) im = Image. open (StringIO (img ['body']) # modify the image size resize to accept two parameters. The first is the wide and high tuples, and the second is the processing of image details, this article indicates anti-Sawtooth im = im. resize (72, 72), Image. ANTIALIAS) # Open io like a file im_file = StringIO () im. save (im_file, format = 'PNG ') # obtain the content im_data = im_file.getvalue () f in io. write (im_data)

In this way, you can modify the file size during the upload.

For more information about how to upload images and modify the image size in Python Tornado, see PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.