Example #1
0
 def save(self, clear_cache=True, *args, **kwargs):
     is_new_object = self._get_pk_val is None
     super(ImageModel, self).save(*args, **kwargs)
     if is_new_object:
         clear_cache = False
         spec = self._ik.preprocessor_spec
         if spec is not None:
             newfile = self._imgfield.storage.open(str(self._imgfield))
             img = Image.open(newfile)
             img = spec.process(img, None)
             format = img.format or 'JPEG'
             if format != 'JPEG':
                 imgfile = img_to_fobj(img, format)
             else:
                 imgfile = img_to_fobj(img, format,
                                       quality=int(spec.quality),
                                       optimize=True)
             content = ContentFile(imgfile.read())
             newfile.close()
             name = str(self._imgfield)
             self._imgfield.storage.delete(name)
             self._imgfield.storage.save(name, content)
     if clear_cache and self._imgfield != '':
         self._clear_cache()
         self._pre_cache()
Example #2
0
 def save(self, clear_cache=True, *args, **kwargs):
     is_new_object = self._get_pk_val() is None
     super(ImageModel, self).save(*args, **kwargs)
     if is_new_object:
         clear_cache = False
         spec = self._ik.preprocessor_spec
         if spec is not None:
             newfile = self._imgfield.storage.open(str(self._imgfield))
             img = Image.open(newfile)
             img, format = spec.process(img, self)
             if format != 'JPEG':
                 imgfile = img_to_fobj(img, format)
             else:
                 imgfile = img_to_fobj(img,
                                       format,
                                       quality=int(spec.quality),
                                       optimize=True)
             content = ContentFile(imgfile.read())
             newfile.close()
             name = str(self._imgfield)
             self._imgfield.storage.delete(name)
             self._imgfield.storage.save(name, content)
     if clear_cache and self._imgfield != '':
         self._clear_cache()
     self._pre_cache()
Example #3
0
    def _process_content(self, filename, content):
        img = open_image(content)
        original_format = img.format
        img = self.field.process(img, self)

        # Determine the format.
        format = self.field.format
        if not format:
            if callable(getattr(self.field, self.field._upload_to_attr)):
                # The extension is explicit, so assume they want the matching format.
                extension = os.path.splitext(filename)[1].lower()
                # Try to guess the format from the extension.
                try:
                    format = extension_to_format(extension)
                except UnknownExtensionError:
                    pass
        format = format or img.format or original_format or 'JPEG'

        if format != 'JPEG':
            imgfile = img_to_fobj(img, format)
        else:
            imgfile = img_to_fobj(img, format,
                                  quality=int(self.field.quality),
                                  optimize=True)
        content = ContentFile(imgfile.read())
        return img, content
Example #4
0
 def _get_imgfile(self):
     format = self._img.format or 'JPEG'
     if format != 'JPEG':
         imgfile = img_to_fobj(self._img, format)
     else:
         imgfile = img_to_fobj(self._img, format,
                               quality=int(self.spec.quality),
                               optimize=True)
     return imgfile
Example #5
0
 def _get_imgfile(self):
     format = self._img.format or 'JPEG'
     if format != 'JPEG':
         imgfile = img_to_fobj(self._img, format)
     else:
         imgfile = img_to_fobj(self._img, format,
                               quality=int(self.spec.quality),
                               optimize=True)
     return imgfile
Example #6
0
    def _process_content(self, filename, content):
        img = open_image(content)
        original_format = img.format
        img = self.field.process(img, self)

        # Determine the format.
        format = self.field.format
        if not format:
            if callable(getattr(self.field, self.field._upload_to_attr)):
                # The extension is explicit, so assume they want the matching format.
                extension = os.path.splitext(filename)[1].lower()
                # Try to guess the format from the extension.
                try:
                    format = extension_to_format(extension)
                except UnknownExtensionError:
                    pass
        format = format or img.format or original_format or 'JPEG'

        if format == 'JPEG':
            img_to_fobj_kwargs = dict(quality=int(self.field.quality),
                optimize=True)
        else:
            img_to_fobj_kwargs = {}

        # Run the AutoConvert processor
        if getattr(self.field, 'autoconvert', True):
            autoconvert_processor = AutoConvert(format)
            img = autoconvert_processor.process(img)
            img_to_fobj_kwargs.update(autoconvert_processor.save_kwargs)

        imgfile = img_to_fobj(img, format, **img_to_fobj_kwargs)
        content = ContentFile(imgfile.read())
        return img, content
Example #7
0
    def _process_content(self, filename, content):
        img = open_image(content)
        original_format = img.format
        img = self.field.process(img, self)

        # Determine the format.
        format = self.field.format
        if not format:
            if callable(getattr(self.field, self.field._upload_to_attr)):
                # The extension is explicit, so assume they want the matching format.
                extension = os.path.splitext(filename)[1].lower()
                # Try to guess the format from the extension.
                try:
                    format = extension_to_format(extension)
                except UnknownExtensionError:
                    pass
        format = format or img.format or original_format or 'JPEG'

        if format == 'JPEG':
            img_to_fobj_kwargs = dict(quality=int(self.field.quality),
                                      optimize=True)
        else:
            img_to_fobj_kwargs = {}

        # Run the AutoConvert processor
        if getattr(self.field, 'autoconvert', True):
            autoconvert_processor = AutoConvert(format)
            img = autoconvert_processor.process(img)
            img_to_fobj_kwargs.update(autoconvert_processor.save_kwargs)

        imgfile = img_to_fobj(img, format, **img_to_fobj_kwargs)
        content = ContentFile(imgfile.read())
        return img, content
Example #8
0
def save_obj_attr_image(obj, attr, img, suffix='', format='png', save=True):
    """
    Saves a PIL image to a file field (attr) on a model instance (obj).
    The filename is a hash of the image contant plus an optional suffix.
    """

    # save to buffer
    format = format.lower()
    content = img_to_fobj(img, format=EXT_TO_FORMAT[format])

    # choose a name that will not collide
    basename = unique_instance_name(obj)
    filename = '%s%s.%s' % (basename, suffix, format)

    # save object
    save_obj_attr_file(obj, attr, filename, content, save=save)