def test_make_thumbnail(self): """ Test that image output by make_thumbnail uses dimensions provided by make_small_dimensions """ thumbnail_size = 64 thumb_width = FuzzyInteger(1, 1024).fuzz() thumb_height = FuzzyInteger(1, 1024).fuzz() # To do the asserts correctly thumbnail dimensions have to have the same aspect ratio width = thumb_width * 4 height = thumb_height * 4 image = Image.new('RGBA', (width, height)) full_image_file = BytesIO() image.save(full_image_file, "PNG") full_image_file.seek(0) with patch( 'profiles.util.shrink_dimensions', return_value=(thumb_width, thumb_height, thumbnail_size) ) as mocked: thumb_file = util.make_thumbnail(full_image_file, 64) thumb_image = Image.open(thumb_file) mocked.assert_called_with(width, height, thumbnail_size) assert thumb_image.width == thumb_width assert thumb_image.height == thumb_height
def save(self, *args, update_image=False, **kwargs): # pylint: disable=arguments-differ """Set the student_id number to the PK number and update thumbnails if necessary""" if update_image: if self.image: small_thumbnail = make_thumbnail(self.image.file, IMAGE_SMALL_MAX_DIMENSION) medium_thumbnail = make_thumbnail(self.image.file, IMAGE_MEDIUM_MAX_DIMENSION) # name doesn't matter here, we use upload_to to produce that self.image_small.save("{}.jpg".format(uuid4().hex), small_thumbnail) self.image_medium.save("{}.jpg".format(uuid4().hex), medium_thumbnail) else: self.image_small = None self.image_medium = None super(Profile, self).save(*args, **kwargs) # if there is no student id, assign the same number of the primary key if self.student_id is None: self.student_id = self.id super(Profile, self).save()
def populate_image_medium(apps, schema_editor): """ Populate image_medium with thumbnail of image if it exists """ Profile = apps.get_model('profiles.Profile') for profile in Profile.objects.all(): if profile.image and not profile.image_medium: try: thumbnail = make_thumbnail(profile.image.file, IMAGE_MEDIUM_MAX_DIMENSION) profile.image_medium.save("{}.jpg".format(uuid4().hex), thumbnail) except OSError: pass
def populate_image_small(apps, schema_editor): """ Populate image_small with thumbnail of image if it exists """ Profile = apps.get_model('profiles.Profile') for profile in Profile.objects.all(): if profile.image and not profile.image_small: try: thumbnail = make_thumbnail(profile.image.file, IMAGE_SMALL_MAX_DIMENSION) profile.image_small.save("{}.jpg".format(uuid4().hex), thumbnail) except OSError: pass