コード例 #1
0
ファイル: pil.py プロジェクト: sean-abbott/phatch
    def save(self, filename, format=None, save_metadata=True, **options):
        """Saves a flattened image"""
        #todo: flatten layers
        if format is None:
            format = imtools.get_format_filename(filename)
        image = self.get_flattened_image()
        image_copy = imtools.convert_save_mode_by_format(image, format)
        if image_copy.mode == 'P' and 'transparency' in image_copy.info:
            options['transparency'] = image_copy.info['transparency']

        if image_copy.mode != image.mode:
            self.log(CONVERTED_MODE % {
                'mode': image.mode,
                'mode_copy': image_copy.mode,
                'format': format
            })

        #reverse exif previously applied exif orientation
        #exif thumbnails are usually within 160x160
        #desktop thumbnails size is defined by thumbnail.py and is
        #probably 128x128
        save_metadata = save_metadata and exif \
            and exif.is_writable_format(format)
        if save_metadata:
            # Exif thumbnails are stored in their own format (eg JPG)
            thumb = thumbnail.thumbnail(image_copy, (160, 160))
            thumbdata = imtools.get_format_data(thumb, format)
            image_copy = imtools.transpose(image_copy,
                                           self._exif_transposition_reverse)
            #thumb = thumbnail.thumbnail(thumb, copy=False)
        else:
            thumbdata = None
            #postpone thumbnail production to see later if it is needed
            thumb = None

        if 'compression.tif' in options:
            compression = options['compression.tif']
            del options['compression.tif']
        else:
            compression = 'none'

        try:
            if compression.lower() in ['raw', 'none']:
                #save image with pil
                file_mode = imtools.save_check_mode(image_copy, filename,
                                                    **options)
                #did PIL silently change the image mode?
                if file_mode:
                    #PIL did change the image mode without throwing
                    # an exception.
                    #Do not save thumbnails in this case
                    # as they won't be reliable.
                    if image_copy.mode.endswith('A') and \
                            not file_mode.endswith('A'):
                        #force RGBA when transparency gets lost
                        #eg saving TIFF format with LA mode
                        mode = image_copy.mode
                        image_copy = image_copy.convert('RGBA')
                        file_mode = imtools.save_check_mode(
                            image_copy, filename, **options)
                        if file_mode:
                            # RGBA failed
                            self.log(
                                CONVERTED_MODE % {
                                    'mode': mode,
                                    'mode_copy': file_mode,
                                    'format': format
                                })
                        else:
                            # RGBA succeeded
                            self.log(
                                CONVERTED_MODE % {
                                    'mode': mode,
                                    'mode_copy': 'RGBA',
                                    'format': format
                                })
                    else:
                        self.log(
                            CONVERTED_MODE % {
                                'mode': image_copy.mode,
                                'mode_copy': file_mode,
                                'format': format
                            })
                elif thumbnail.is_needed(image_copy, format):
                    # save thumbnail in system cache if needed
                    if thumb is None:
                        thumb = image_copy
                    thumb_info = {
                        'width': image.size[0],
                        'height': image.size[1]
                    }
                    thumbnail.save_to_cache(filename,
                                            thumb,
                                            thumb_info=thumb_info,
                                            **options)
                # copy metadata if needed (problematic for tiff)
                # FIXME: if metdata corrupts the image, there should be
                # no thumbnail
                if save_metadata:
                    self.info.save(filename, thumbdata=thumbdata)
            else:
                # save with pil>libtiff
                openImage.check_libtiff(compression)
                self.log(
                    openImage.save_libtiff(image_copy,
                                           filename,
                                           compression=compression,
                                           **options))
            if self.modify_date:
                # Update file access and modification date
                os.utime(filename, (self.modify_date, self.modify_date))
            self.append_to_report(filename, image_copy)
        except IOError, message:
            # clean up corrupted drawing
            if os.path.exists(filename):
                os.remove(filename)
            raise IOError(message)
コード例 #2
0
    def save(self, filename, format=None, save_metadata=True, **options):
        """Saves a flattened image"""
        #todo: flatten layers
        if format is None:
            format = imtools.get_format_filename(filename)
        image = self.get_flattened_image()
        image_copy = imtools.convert_save_mode_by_format(image, format)
        if image_copy.mode == 'P' and 'transparency' in image_copy.info:
            options['transparency'] = image_copy.info['transparency']

        if image_copy.mode != image.mode:
            self.log(CONVERTED_MODE % {'mode': image.mode,
                'mode_copy': image_copy.mode, 'format': format} + '\n')

        #reverse exif previously applied exif orientation
        #exif thumbnails are usually within 160x160
        #desktop thumbnails size is defined by thumbnail.py and is
        #probably 128x128
        save_metadata = save_metadata and exif \
            and exif.is_writable_format(format)
        if save_metadata:
            # Exif thumbnails are stored in their own format (eg JPG)
            thumb = thumbnail.thumbnail(image_copy, (160, 160))
            thumbdata = imtools.get_format_data(thumb, format)
            image_copy = imtools.transpose(image_copy,
                self._exif_transposition_reverse)
            #thumb = thumbnail.thumbnail(thumb, copy=False)
        else:
            thumbdata = None
            #postpone thumbnail production to see later if it is needed
            thumb = None

        if 'compression.tif' in options:
            compression = options['compression.tif']
            del options['compression.tif']
        else:
            compression = 'none'

        try:
            if compression.lower() in ['raw', 'none']:
                #save image with pil
                file_mode = imtools.save_check_mode(image_copy, filename,
                    **options)
                #did PIL silently change the image mode?
                if file_mode:
                    #PIL did change the image mode without throwing
                    # an exception.
                    #Do not save thumbnails in this case
                    # as they won't be reliable.
                    if image_copy.mode.endswith('A') and \
                            not file_mode.endswith('A'):
                        #force RGBA when transparency gets lost
                        #eg saving TIFF format with LA mode
                        mode = image_copy.mode
                        image_copy = image_copy.convert('RGBA')
                        file_mode = imtools.save_check_mode(image_copy,
                            filename, **options)
                        if file_mode:
                            # RGBA failed
                            self.log(CONVERTED_MODE % {'mode': mode,
                                'mode_copy': file_mode, 'format': format} \
                                + '\n')
                        else:
                            # RGBA succeeded
                            self.log(CONVERTED_MODE % {'mode': mode,
                                'mode_copy': 'RGBA', 'format': format} + '\n')
                    else:
                        self.log(CONVERTED_MODE % {'mode': image_copy.mode,
                            'mode_copy': file_mode, 'format': format} + '\n')
                elif thumbnail.is_needed(image_copy, format):
                    # save thumbnail in system cache if needed
                    if thumb is None:
                        thumb = image_copy
                    thumb_info = {
                        'width': image.size[0],
                        'height': image.size[1]}
                    thumbnail.save_to_cache(filename, thumb,
                        thumb_info=thumb_info, **options)
                # copy metadata if needed (problematic for tiff)
                # FIXME: if metdata corrupts the image, there should be
                # no thumbnail
                if save_metadata:
                    self.info.save(filename, thumbdata=thumbdata)
            else:
                # save with pil>libtiff
                openImage.check_libtiff(compression)
                self.log(openImage.save_libtiff(image_copy, filename,
                    compression=compression, **options))
            if self.modify_date:
                # Update file access and modification date
                os.utime(filename, (self.modify_date, self.modify_date))
            self.append_to_report(filename, image_copy)
        except IOError, message:
            # clean up corrupted drawing
            if os.path.exists(filename):
                os.remove(filename)
            raise IOError(message)
コード例 #3
0
ファイル: pil.py プロジェクト: sean-abbott/phatch
 def get_thumb(self, size=thumbnail.SIZE):
     return thumbnail.thumbnail(self.get_flattened_image(),
                                size=size,
                                checkboard=True)
コード例 #4
0
 def get_thumb(self, size=thumbnail.SIZE):
     return thumbnail.thumbnail(self.get_flattened_image(),
         size=size, checkboard=True)