image_file.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:file-metadata 作者: pywikibot-catfiles 项目源码 文件源码
def fetch(self, key=''):
        if key == 'filename_raster':
            # A raster filename holds the file in a raster graphic format
            return self.fetch('filename')
        elif key == 'filename_zxing':
            return pathlib2.Path(self.fetch('filename_raster')).as_uri()
        elif key == 'ndarray':
            Image.MAX_IMAGE_PIXELS = self.config('max_decompressed_size')
            try:
                image_array = skimage.io.imread(self.fetch('filename_raster'))
                if image_array.shape == (2,):
                    # Assume this is related to
                    # https://github.com/scikit-image/scikit-image/issues/2154
                    return image_array[0]
                return image_array
            except Image.DecompressionBombWarning:
                logging.warn('The file "{0}" contains a lot of pixels and '
                             'can take a lot of memory when decompressed. '
                             'To allow larger images, modify the '
                             '"max_decompressed_size" config.'
                             .format(self.fetch('filename')))
                # Use empty array as the file cannot be read.
                return numpy.ndarray(0)
        elif key == 'ndarray_grey':
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                return skimage.img_as_ubyte(
                    skimage.color.rgb2grey(self.fetch('ndarray')))
        elif key == 'ndarray_hsv':
            with warnings.catch_warnings():
                warnings.simplefilter("ignore")
                return skimage.img_as_ubyte(
                    skimage.color.rgb2hsv(self.fetch('ndarray_noalpha')))
        elif key == 'ndarray_noalpha':
            if self.is_type('alpha'):
                return self.alpha_blend(self.fetch('ndarray'))
            return self.fetch('ndarray')
        elif key == 'pillow':
            pillow_img = Image.open(self.fetch('filename_raster'))
            self.closables.append(pillow_img)
            return pillow_img
        return super(ImageFile, self).fetch(key)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号