简体   繁体   中英

Using StringIO With Python 3.4

So now i'm using python 3.4 my code isn't working. I was importing from cStringIO and now i've fixed that it's getting an unexpected error.

Heres my error:

Traceback:
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/contrib/admin/options.py" in wrapper
  567.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/contrib/admin/sites.py" in inner
  204.             return view(request, *args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/contrib/admin/options.py" in add_view
  1437.         return self.changeform_view(request, None, form_url, extra_context)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/utils/decorators.py" in bound_func
  25.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/db/transaction.py" in inner
  394.                 return func(*args, **kwargs)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/contrib/admin/options.py" in changeform_view
  1388.                 self.save_model(request, new_object, form, not add)
File "/home/callumb/webapps/photo/lib/python3.4/Django-1.7-py3.4.egg/django/contrib/admin/options.py" in save_model
  1029.         obj.save()
File "/home/callumb/webapps/photo/app/images/models.py" in save
  77.       self.create_thumbnail()
File "/home/callumb/webapps/photo/app/images/models.py" in create_thumbnail
  50.     image = Image.open(StringIO(self.image.read()))

Exception Type: TypeError at /admin/images/image/add/
Exception Value: initial_value must be str or None, not bytes

And here is the code:

def create_thumbnail(self):

   if not self.image:
        return

    from PIL import Image
    from io import StringIO, BytesIO
    from django.core.files.uploadedfile import SimpleUploadedFile
    import os

    THUMBNAIL_SIZE = (280, 600)
    DJANGO_TYPE = self.image.file.content_type
    if DJANGO_TYPE == 'image/jpeg':
        PIL_TYPE = 'jpeg'
        FILE_EXTENSION = 'jpg'
    elif DJANGO_TYPE == 'image/png':
        PIL_TYPE = 'png'
        FILE_EXTENSION = 'png'

    image = Image.open(StringIO(self.image.read()))
    image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)

    temp_handle = StringIO()
    image.save(temp_handle, PIL_TYPE)
    temp_handle.seek(0)

    suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
            temp_handle.read(), content_type=DJANGO_TYPE)
    self.thumbnail.save(
        '%s_thumbnail.%s' % (os.path.splitext(suf.name)[0], FILE_EXTENSION),
        suf,
        save=False
    )

The traceback is telling you that StringIO expects a string but you are providing bytes. Use BytesIO instead os StringIO .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM