Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
junkdrawer.imode = lambda image: ImageMode.getmode(image.mode)
ImageMode.getmode('RGB') # one call must be made to getmode()
# to properly initialize ImageMode._modes:
junkdrawer.modes = ImageMode._modes
junkdrawer.types = Image._MODE_CONV
junkdrawer.ismap = Image._MAPMODES
mode_strings = tuple(junkdrawer.modes.keys())
dtypes_for_modes = { k : v[0] for k, v in junkdrawer.types.items() }
junkdrawer.idxmode = lambda idx: ImageMode.getmode(mode_strings[idx])
junkdrawer.is_mapped = lambda mode: mode in junkdrawer.ismap
class ModeAncestor(AliasingEnum):
"""
Valid ImageMode mode strings:
('1', 'L', 'I', 'F', 'P',
'RGB', 'RGBX', 'RGBA', 'CMYK', 'YCbCr',
'LAB', 'HSV', 'RGBa', 'LA', 'La',
'PA', 'I;16', 'I;16L', 'I;16B')
"""
def _generate_next_value_(name,
start,
count,
last_values):
return junkdrawer.idxmode(count)
@classmethod
def _missing_(cls, value):
return self.compose(*processed)
ChannelFork = BandFork
ink_values = (
(255, 255, 255), # White
(0, 250, 250), # Cyan
(250, 0, 250), # Magenta
(250, 250, 0), # Yellow
(0, 0, 0), # Key (blacK)
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
)
class Ink(AliasingEnum):
def rgb(self):
return ink_values[self.value]
def process(self, image):
InkType = type(self)
return ImageOps.colorize(Mode.L.process(image),
InkType(0).rgb(),
InkType(self.value).rgb())
@unique
class CMYKInk(Ink):
WHITE = 0
CYAN = 1
MAGENTA = 2
self.attr_set('image', image)
return self
def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
image = self.attr_or_none('image')
original_mode = self.attr_or_none('original_mode')
if image is not None and original_mode is not None:
if DEBUG:
mode = Mode.of(image)
print(f"ModeContext.__exit__: converting {mode} to {original_mode}")
final_image = original_mode.process(image)
self.attr_set('final_image', final_image)
return exc_type is None
@unique
class Field(AliasingEnum):
RO = auto()
WO = auto()
RW = auto()
ReadOnly = alias(RO)
WriteOnly = alias(WO)
ReadWrite = alias(RW)
class FieldIOError(IOError):
pass
anno_for = lambda cls, name, default=None: getpyattr(cls, 'annotations', default={}).get(name, default)
class ModeField(object):
""" Not *that* ModeDescriptor. THIS ModeDescriptor! """