Fix uploading gifs

Closes #610
parent c7030ca1
import operator
import os
import logging
from datetime import timedelta
from functools import reduce
......@@ -19,6 +20,9 @@ from localflavor.generic.models import IBANField
from activemembers.models import Committee
logger = logging.getLogger(__name__)
class MemberManager(UserManager):
"""Get all members, i.e. all users with a profile."""
......@@ -427,15 +431,26 @@ class Profile(models.Model):
elif self.photo and self._orig_image != self.photo.path:
image_path = self.photo.path
image = Image.open(image_path)
image_path, _ext = os.path.splitext(image_path)
image_path = "{}.jpg".format(image_path)
# Image.thumbnail does not upscale an image that is smaller
logger.debug("Converting image %s", image_path)
image.thumbnail(settings.PHOTO_UPLOAD_SIZE, Image.ANTIALIAS)
image.save(image_path, "JPEG")
image.convert("RGB").save(image_path, "JPEG")
image_name, _ext = os.path.splitext(self.photo.name)
self.photo.name = "{}.jpg".format(image_name)
super().save(*args, **kwargs)
try:
os.remove(self._orig_image)
if self._orig_image:
logger.info("deleting", self._orig_image)
os.remove(self._orig_image)
except FileNotFoundError:
pass
self._orig_image = self.photo.path
else:
logging.warning("We already had this image")
def __str__(self):
return str(self.user)
......
import hashlib
import os
import random
import logging
from PIL.JpegImagePlugin import JpegImageFile
from django.conf import settings
......@@ -26,6 +27,9 @@ EXIF_ORIENTATION = {
}
logger = logging.getLogger(__name__)
def photo_uploadto(instance, filename):
num = instance.album.photo_set.count()
extension = os.path.splitext(filename)[1]
......@@ -91,13 +95,19 @@ class Photo(models.Model):
if self._orig_file != self.file.path:
image_path = self.file.path
image = Image.open(image_path)
image_path, _ext = os.path.splitext(image_path)
image_path = "{}.jpg".format(image_path)
self.rotation = determine_rotation(image)
# Image.thumbnail does not upscale an image that is smaller
image.thumbnail(settings.PHOTO_UPLOAD_SIZE, Image.ANTIALIAS)
image.save(image_path, "JPEG")
self._orig_file = self.file.path
logger.info("Trying to save to %s", image_path)
image.convert("RGB").save(image_path, "JPEG")
self._orig_file = image_path
image_name, _ext = os.path.splitext(self.file.name)
self.file.name = "{}.jpg".format(image_name)
hash_sha1 = hashlib.sha1()
for chunk in iter(lambda: self.file.read(4096), b""):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment