Commit 088596bd authored by fred's avatar fred
Browse files

scale down images to a reasonable value before storing them

parent 460b884a
......@@ -9,6 +9,8 @@ from django.db import models
from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager
from utils import maybe_resize
class Category(models.Model):
title = models.CharField(max_length=50)
......@@ -48,6 +50,11 @@ class Emission(models.Model):
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
super(Emission, self).save(*args, **kwargs)
if self.id is not None and self.image:
maybe_resize(self.image.path)
class Schedule(models.Model):
DAY_HOUR_START = 6
......@@ -138,6 +145,12 @@ class Episode(models.Model):
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
super(Episode, self).save(*args, **kwargs)
if self.id is not None and self.image:
maybe_resize(self.image.path)
def get_absolute_url(self):
return reverse('episode-view',
kwargs={'emission_slug': str(self.emission.slug),
......
from PIL import Image
def maybe_resize(image_path):
image = Image.open(image_path)
if max(image.size) > 1000:
# no sense storing images that large
factor = 1000. / max(image.size)
image = image.resize(
(int(image.size[0]*factor), int(image.size[1]*factor)),
Image.ANTIALIAS)
image.save(image_path)
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