python - Which one I have to use to read image in Django, StringIO or BytesIO? -


i'm trying compress image file before uploading in django application.

i found nice code snippet site : https://djangosnippets.org/snippets/10460/

but doesn't work in python3. think problem str or byte.

someone advise use bytesio instead of stringio.

so, edit code this.

from django.db import models django.core.urlresolvers import reverse django.utils import timezone django.utils.text import slugify django.core.files.uploadedfile import inmemoryuploadedfile  pil import image img io import stringio, bytesio  def upload_location(instance, file_name):     return "{}/{}/{}/{}".format(         "album",         instance.day,         instance.week,         file_name     )   class album(models.model):      days = (         ('sun', '일요일'),         ('mon', '월요일'),     )     name = models.charfield(max_length=50)     description = models.charfield(max_length=100, blank=true)     image = models.imagefield(upload_to=upload_location)     day = models.charfield(max_length=3, choices=days)     week = models.integerfield()     slug = models.slugfield(unique=true, allow_unicode=true)     date = models.datefield()      created_at = models.datetimefield(auto_now_add=true)     updated_at = models.datetimefield(auto_now=true)      class meta:         ordering = ['day', 'week']      def __str__(self):         return "{} - {}주차".format(self.get_day_display(), self.week)      def get_absolute_url(self):         return reverse(             "album:album_detail",             kwargs={                 "slug": self.slug             }         )      def save(self, *args, **kwargs):         if not self.id:             self.slug = self.slug + "주차"          if self.image:             img = img.open(bytesio(self.image.read()))             if img.mode != 'rgb':                 img = img.convert('rgb')             img.thumbnail((self.image.width/1.5,self.image.height/1.5), img.antialias)             output = bytesio()             img.save(output, format='jpeg', quality=70)             output.seek(0)             self.image= inmemoryuploadedfile(                 output,'imagefield',                 "%s.jpg" %self.image.name.split('.')[0],                 'image/jpeg',                 output.len, none             )         super().save(*args, **kwargs) 

but occurs error : '_io.bytesio' object has no attribute 'len' --> output.len in code occurs error.

i start doubt right way use bytesio instead of stringio.

and need helps how edit code, too. thanks.

i modified code use with statement there no need close files yourself.

def save(self, *args, **kwargs):     if not self.id:         self.slug = self.slug + "주차"      if self.image:         img.open(bytesio(self.image.read())) img:             if img.mode != 'rgb':                 img = img.convert('rgb')              img.thumbnail((self.image.width/1.5,self.image.height/1.5), img.antialias)             bytesio() output:                 img.save(output, format='jpeg', quality=70)                 output.seek(0)                 self.image = inmemoryuploadedfile(                     output,'imagefield',                     "%s.jpg" %self.image.name.split('.')[0],                     'image/jpeg',                     output.getbuffer().nbytes, none                 )      super().save(*args, **kwargs) 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -