Django uploads images, uses PIL to make thumbnails and saves to sea's storage

Source: Internet
Author: User
Tags shallow copy

Upload image analysis:

The SAE setup guidelines are as follows:

Process user upload files

Add the following configuration in setttings.py.

# The maximum size that a file can hold in memory when modifying an upload is 10m File_upload_max_memory_size = 10485760# The local file system of the SAE is read-only, modifying the Django file storage backend for storage'  sae.ext.django.storage.backend.Storage'#  using media this bucket' Media ' # ref:https://docs.djangoproject.com/en/dev/topics/files/

In two cases:
    • A picture is a field in a database, such as:
class Picture (models. Model):    "" "docstringfor picture"    "" = models. ForeignKey (Customer)    = Thumbnailimagefield (upload_to='goods_photo')

Then you just need to write in the view function is OK, this time SAE can automatically help you to upload images to storage in the domain for media under the Goods_ Photo folder (remember to activate STORAGE on the SAE and create a storage_bucket_name named you set in settings.py , and create a upload_ under this domain named you. To the value of the folder)

def Upload_photo (Request):     = Request. files['upload_file']    name=copy.copy (f.name)    = Picture ()    = f    = Request. post['user_id'    = Customer.objects.get (pk=user_id)    = user    p.save ()
    • Upload images directly to storage

Refer to Sea's Storage API documentation, links

Let's get down to business now. My goal is to upload a picture and generate a thumbnail image, which is a field in the database, with a custom attribute for the thumbnail image through the database.Thumb_urlTo access this thumbnail to customize a Thumbnailimagefield, refer to the code below
#thumbnailimagefield.py fromDjango.db.models.fields.filesImportImagefield,imagefieldfile fromPILImportImagedef_add_thumb (s): Parts= S.split (".") Parts.insert (-1,"Thumb")    ifParts[-1].lower () not inch['JPEG','jpg']: parts[-1]='jpg'    return ".". Join (Parts)classThumbnailimagefieldfile (imagefieldfile):def_get_thumb_url (self):return_add_thumb (self.url) Thumb_url=Property (_get_thumb_url)defSave (self,name,content,save=True): Super (Thumbnailimagefieldfile, self). Save (Name,content,save)defDelete (self,save=True): Super (Thumbnailimagefieldfile, self). Delete (save)classThumbnailimagefield (ImageField):"""docstring for Thumbnailimagefield"""Attr_class=Thumbnailimagefieldfiledef __init__(self,*args,**Kwargs): Super (Thumbnailimagefield, self).__init__(*args,**kwargs)
View functions refer to the following
defUpload_photo (Request): F= Request. files['Upload_file'] Name=copy.copy (f.name) p=Picture () p.image=F user_id= Request. post['user_id'] User= Customer.objects.get (pk=user_id) P.user=user P.save () C=sae.storage.Connection () bucket= C.get_bucket ('Media') obj= Bucket.get_object_contents ("goods_photo/"+name) Image=Image.open (Stringio (obj)) Image.thumbnail ((90,90), Image.antialias) Imgout=Stringio () image.save (Imgout,'JPEG') Img_data=Imgout.getvalue () bucket.put_object ('goods_photo/'+_add_thumb (name), Img_data) imgout.close ()

The view functions are explained in detail:

P.save () consumes the request. files[' Upload_file ', so the recommended practice is to save this file to storage and then remove the image from the storage for processing.
Name=copy.copy (F.name), a shallow copy is also used because P.save () consumes the request. files[' Upload_file '], direct name=f.name words get the name don't know what is, of course you can also use deepcopy, but not necessary, because name is a string
Bucket.get_object_contents ("goods_photo/" +name), sea's storage does not support a direct directory structure, but can be modeled by file names

Django uploads images, uses PIL to make thumbnails and saves to sea's storage

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.