~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlprofile/fields.py

  • Committer: kaputtnik
  • Date: 2019-06-14 18:40:56 UTC
  • mfrom: (532.1.31 widelands)
  • Revision ID: kaputtnik-20190614184056-l0ha8pm5zais9mxk
Adapted code for use with python 3.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from StringIO import StringIO
 
1
from io import BytesIO
2
2
from django.db import models
3
3
import logging
4
4
from django.core.files.uploadedfile import SimpleUploadedFile
30
30
        """Resize image to fit it into (width, height) box."""
31
31
        from PIL import Image
32
32
 
33
 
        image = Image.open(StringIO(rawdata))
 
33
        image = Image.open(BytesIO(rawdata))
34
34
        try:
35
35
            oldw, oldh = image.size
36
36
 
42
42
                    y = int(round((oldh - oldw) / 2.0))
43
43
                    image = image.crop((0, y, oldw - 1, (y + oldw) - 1))
44
44
                image = image.resize((width, height), resample=Image.ANTIALIAS)
45
 
        except Exception, err:
 
45
        except Exception as err:
46
46
            logging.error(err)
47
47
            return ''
48
48
 
49
 
        string = StringIO()
 
49
        string = BytesIO()
50
50
        image.save(string, format='PNG')
51
51
        return string.getvalue()