Ejemplo n.º 1
0
    def save(self, force_insert=False, force_update=False):
        super(EPS, self).save(force_insert, force_update)

        # resize thumbnail
        im = PILImage.open(os.path.join(settings.MEDIA_ROOT, self.thumbnail.name))
        #im = im.resize((100, 100))
        im = self.resize_thumbnail(im)
        im = round_image(im, {}, 10)
        base, ext = os.path.splitext(self.thumbnail.name)
        im.save(os.path.join(settings.MEDIA_ROOT, '%s_thumbnail%s' % (base, ext)))
Ejemplo n.º 2
0
    def create_size(self, photosize):
        if self.size_exists(photosize):
            return
        if not os.path.isdir(self.cache_path()):
            os.makedirs(self.cache_path())

        should_convert = lambda s : s.name not in self.dont_convert

        # some images may have CMYK color encoding, so convert to RGB
        # we use tifficc from littlecms utils because pil color space conversion
        # does not give pretty results

        try:
            # get the image format
            im = PILImage.open(self.image.path)
            format = im.format

            # convert to rgb
            retcode = -1
            if should_convert(photosize) and format == 'TIFF':
                input_profile = '%s/iccprofiles/CoatedFOGRA27.icc' % settings.PROJECT_ROOT
                output_profile = '%s/iccprofiles/AdobeRGB1998.icc' % settings.PROJECT_ROOT
                outputfile = tempfile.NamedTemporaryFile()

                retcode = subprocess.call(
                    ['tifficc', "-i", input_profile, "-o", output_profile, self.image.path, outputfile.name])
            im = PILImage.open(outputfile.name if retcode == 0 else self.image.path)

        except IOError:
            return

        # Apply effect if found
        if self.effect is not None:
            im = self.effect.pre_process(im)
        elif photosize.effect is not None:
            im = photosize.effect.pre_process(im)
        # Resize/crop image
        if im.size != photosize.size and photosize.size != (0, 0):
            im = self.resize_image(im, photosize)
        # Apply watermark if found
        if photosize.watermark is not None:
            im = photosize.watermark.post_process(im)
        # Apply effect if found
        if self.effect is not None:
            im = self.effect.post_process(im)
        elif photosize.effect is not None:
            im = photosize.effect.post_process(im)
        # round corners on thumbnails
        if photosize.name == 'thumbnail':
            im = round_image(im, {}, 10)

        # Save file
        im_filename = getattr(self, "get_%s_filename" % photosize.name)()
        try:
            if im.format != 'JPEG' and should_convert(photosize):

                # save as jpeg
                base, ext = os.path.splitext(im_filename)
                im_filename = ''.join([base, '.jpg'])

                im.save(im_filename, 'JPEG', quality=int(photosize.quality), optimize=True)
                return
            else:
                im.save(im_filename)

        except IOError, e:
            if os.path.isfile(im_filename):
                os.unlink(im_filename)
            raise e