def save_persona_image(src, full_dst, **kw): """Creates a PNG of a Persona header/footer image.""" log.info('[1@None] Saving persona image: %s' % full_dst) img = ImageCheck(storage.open(src)) if not img.is_image(): log.error('Not an image: %s' % src, exc_info=True) return with storage.open(src, 'rb') as fp: i = Image.open(fp) with storage.open(full_dst, 'wb') as fp: i.save(fp, 'png') return True
def validate_picture_upload(self, value): image_check = ImageCheck(value) if (value.content_type not in amo.IMG_TYPES or not image_check.is_image()): raise serializers.ValidationError( ugettext(u'Images must be either PNG or JPG.')) if image_check.is_animated(): raise serializers.ValidationError( ugettext(u'Images cannot be animated.')) if value.size > settings.MAX_PHOTO_UPLOAD_SIZE: raise serializers.ValidationError( ugettext(u'Please use images smaller than %dMB.' % (settings.MAX_PHOTO_UPLOAD_SIZE / 1024 / 1024))) return value
def validate_picture_upload(self, value): image_check = ImageCheck(value) if (value.content_type not in amo.IMG_TYPES or not image_check.is_image()): raise serializers.ValidationError( ugettext(u'Images must be either PNG or JPG.')) if image_check.is_animated(): raise serializers.ValidationError( ugettext(u'Images cannot be animated.')) if value.size > settings.MAX_PHOTO_UPLOAD_SIZE: raise serializers.ValidationError( ugettext(u'Please use images smaller than %dMB.' % (settings.MAX_PHOTO_UPLOAD_SIZE / 1024 / 1024 - 1))) return value
def test_animated_images(self): img = ImageCheck(open(get_image_path('animated.png'))) assert img.is_animated() img = ImageCheck(open(get_image_path('non-animated.png'))) assert not img.is_animated() img = ImageCheck(open(get_image_path('animated.gif'))) assert img.is_animated() img = ImageCheck(open(get_image_path('non-animated.gif'))) assert not img.is_animated()
def clean_icon(self): icon = self.cleaned_data['icon'] if not icon: return icon_check = ImageCheck(icon) if (icon.content_type not in amo.IMG_TYPES or not icon_check.is_image()): raise forms.ValidationError( ugettext('Icons must be either PNG or JPG.')) if icon_check.is_animated(): raise forms.ValidationError(ugettext('Icons cannot be animated.')) if icon.size > settings.MAX_ICON_UPLOAD_SIZE: size_in_mb = settings.MAX_ICON_UPLOAD_SIZE / 1024 / 1024 - 1 raise forms.ValidationError( ugettext('Please use images smaller than %dMB.') % size_in_mb) return icon
def clean_photo(self): photo = self.cleaned_data['photo'] if not photo: return image_check = ImageCheck(photo) if (photo.content_type not in amo.IMG_TYPES or not image_check.is_image()): raise forms.ValidationError( ugettext('Images must be either PNG or JPG.')) if image_check.is_animated(): raise forms.ValidationError(ugettext('Images cannot be animated.')) if photo.size > settings.MAX_PHOTO_UPLOAD_SIZE: msg = ugettext('Please use images smaller than %dMB.') size_in_mb = settings.MAX_PHOTO_UPLOAD_SIZE / 1024 / 1024 raise forms.ValidationError(msg % size_in_mb) return photo
def test_junk(self): img = ImageCheck(open(__file__, 'rb')) assert not img.is_image() img = ImageCheck(open(get_image_path('non-animated.gif'))) assert img.is_image()