/usr/lib/python3/dist-packages/willow/image.py is in python3-willow 1.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import imghdr
import warnings
from .registry import registry
from .utils.deprecation import RemovedInWillow05Warning
class UnrecognisedImageFormatError(IOError):
pass
class Image(object):
@classmethod
def check(cls):
pass
@staticmethod
def operation(func):
func._willow_operation = True
return func
@staticmethod
def converter_to(to_class, cost=None):
def wrapper(func):
func._willow_converter_to = (to_class, cost)
return func
return wrapper
@staticmethod
def converter_from(from_class, cost=None):
def wrapper(func):
if not hasattr(func, '_willow_converter_from'):
func._willow_converter_from = []
if isinstance(from_class, list):
func._willow_converter_from.extend([
(sc, cost) for sc in from_class]
)
else:
func._willow_converter_from.append((from_class, cost))
return func
return wrapper
def __getattr__(self, attr):
try:
operation, _, conversion_path, _ = registry.find_operation(type(self), attr)
except LookupError:
# Operation doesn't exist
raise AttributeError("%r object has no attribute %r" % (
self.__class__.__name__, attr
))
def wrapper(*args, **kwargs):
image = self
for converter, _ in conversion_path:
image = converter(image)
return operation(image, *args, **kwargs)
return wrapper
# A couple of helpful methods
@classmethod
def open(cls, f):
# Detect image format
image_format = imghdr.what(f)
# Find initial class
initial_class = INITIAL_IMAGE_CLASSES.get(image_format)
if not initial_class:
if image_format:
raise UnrecognisedImageFormatError("Cannot load %s images" % image_format)
else:
raise UnrecognisedImageFormatError("Unknown image format")
return initial_class(f)
def save(self, image_format, output):
# Get operation name
if image_format not in ['jpeg', 'png', 'gif', 'bmp', 'tiff']:
raise ValueError("Unknown image format: %s" % image_format)
operation_name = 'save_as_' + image_format
return getattr(self, operation_name)(output)
class ImageBuffer(Image):
def __init__(self, size, data):
self.size = size
self.data = data
@Image.operation
def get_size(self):
return self.size
class RGBImageBuffer(ImageBuffer):
mode = 'RGB'
@Image.operation
def has_alpha(self):
return False
@Image.operation
def has_animation(self):
return False
class RGBAImageBuffer(ImageBuffer):
mode = 'RGBA'
@Image.operation
def has_alpha(self):
return True
@Image.operation
def has_animation(self):
return False
class ImageFile(Image):
format_name = None
@property
def original_format(self):
warnings.warn(
"Image.original_format has been renamed to Image.format_name.",
RemovedInWillow05Warning)
return self.format_name
def __init__(self, f):
self.f = f
class JPEGImageFile(ImageFile):
format_name = 'jpeg'
class PNGImageFile(ImageFile):
format_name = 'png'
class GIFImageFile(ImageFile):
format_name = 'gif'
class BMPImageFile(ImageFile):
format_name = 'bmp'
class TIFFImageFile(ImageFile):
format_name = 'tiff'
INITIAL_IMAGE_CLASSES = {
# A mapping of image formats to their initial class
'jpeg': JPEGImageFile,
'png': PNGImageFile,
'gif': GIFImageFile,
'bmp': BMPImageFile,
'tiff': TIFFImageFile,
}
# 12 - Make imghdr detect JPEGs based on first two bytes
def test_jpeg(h, f):
if h[0:1] == b'\377':
return 'jpeg'
imghdr.tests.append(test_jpeg)
|