# # The Python Imaging Library # $Id$ # # a simple math add-on for the Python Imaging Library # # History: # 1999-02-15 fl Original PIL Plus release # 2005-05-05 fl Simplified and cleaned up for PIL 1.1.6 # 2005-09-12 fl Fixed int() and float() for Python 2.4.1 # # Copyright (c) 1999-2005 by Secret Labs AB # Copyright (c) 2005 by Fredrik Lundh # # See the README file for information on usage and redistribution. # import builtins from . import Image, _imagingmath VERBOSE = 0 def _isconstant(v): return isinstance(v, (int, float)) class _Operand: """Wraps an image operand, providing standard operators""" def __init__(self, im): self.im = im def __fixup(self, im1): # convert image to suitable mode if isinstance(im1, _Operand): # argument was an image. if im1.im.mode in ("1", "L"): return im1.im.convert("I") elif im1.im.mode in ("I", "F"): return im1.im else: raise ValueError(f"unsupported mode: {im1.im.mode}") else: # argument was a constant if _isconstant(im1) and self.im.mode in ("1", "L", "I"): return Image.new("I", self.im.size, im1) else: return Image.new("F", self.im.size, im1) def apply(self, op, im1, im2=None, mode=None): im1 = self.__fixup(im1) if im2 is None: # unary operation out = Image.new(mode or im1.mode, im1.size, None) im1.load() try: op = getattr(_imagingmath, op + "_" + im1.mode) except AttributeError as e: raise TypeError(f"bad operand type for '{op}'") from e _imagingmath.unop(op, out.im.id, im1.im.id) else: # binary operation im2 = self.__fixup(im2) if im1.mode != im2.mode: # convert both arguments to floating point if im1.mode != "F": im1 = im1.convert("F") if im2.mode != "F": im2 = im2.convert("F") if im1.mode != im2.mode: raise ValueError("mode mismatch") if im1.size != im2.size: # crop both arguments to a common size size = (min(im1.size[0], im2.size[0]), min(im1.size[1], im2.size[1])) if im1.size != size: im1 = im1.crop((0, 0) + size) if im2.size != size: im2 = im2.crop((0, 0) + size) out = Image.new(mode or im1.mode, size, None) else: out = Image.new(mode or im1.mode, im1.size, None) im1.load() im2.load() try: op = getattr(_imagingmath, op + "_" + im1.mode) except AttributeError as e: raise TypeError(f"bad operand type for '{op}'") from e _imagingmath.binop(op, out.im.id, im1.im.id, im2.im.id) return _Operand(out) # unary operators def __bool__(self): # an image is "true" if it contains at least one non-zero pixel return self.im.getbbox() is not None def __abs__(self): return self.apply("abs", self) def __pos__(self): return self def __neg__(self): return self.apply("neg", self) # binary operators def __add__(self, other): return self.apply("add", self, other) def __radd__(self, other): return self.apply("add", other, self) def __sub__(self, other): return self.apply("sub", self, other) def __rsub__(self, other): return self.apply("sub", other, self) def __mul__(self, other): return self.apply("mul", self, other) def __rmul__(self, other): return self.apply("mul", other, self) def __truediv__(self, other): return self.apply("div", self, other) def __rtruediv__(self, other): return self.apply("div", other, self) def __mod__(self, other): return self.apply("mod", self, other) def __rmod__(self, other): return self.apply("mod", other, self) def __pow__(self, other): return self.apply("pow", self, other) def __rpow__(self, other): return self.apply("pow", other, self) # bitwise def __invert__(self): return self.apply("invert", self) def __and__(self, other): return self.apply("and", self, other) def __rand__(self, other): return self.apply("and", other, self) def __or__(self, other): return self.apply("or", self, other) def __ror__(self, other): return self.apply("or", other, self) def __xor__(self, other): return self.apply("xor", self, other) def __rxor__(self, other): return self.apply("xor", other, self) def __lshift__(self, other): return self.apply("lshift", self, other) def __rshift__(self, other): return self.apply("rshift", self, other) # logical def __eq__(self, other): return self.apply("eq", self, other) def __ne__(self, other): return self.apply("ne", self, other) def __lt__(self, other): return self.apply("lt", self, other) def __le__(self, other): return self.apply("le", self, other) def __gt__(self, other): return self.apply("gt", self, other) def __ge__(self, other): return self.apply("ge", self, other) # conversions def imagemath_int(self): return _Operand(self.im.convert("I")) def imagemath_float(self): return _Operand(self.im.convert("F")) # logical def imagemath_equal(self, other): return self.apply("eq", self, other, mode="I") def imagemath_notequal(self, other): return self.apply("ne", self, other, mode="I") def imagemath_min(self, other): return self.apply("min", self, other) def imagemath_max(self, other): return self.apply("max", self, other) def imagemath_convert(self, mode): return _Operand(self.im.convert(mode)) ops = {} for k, v in list(globals().items()): if k[:10] == "imagemath_": ops[k[10:]] = v def eval(expression, _dict={}, **kw): """ Evaluates an image expression. :param expression: A string containing a Python-style expression. :param options: Values to add to the evaluation context. You can either use a dictionary, or one or more keyword arguments. :return: The evaluated expression. This is usually an image object, but can also be an integer, a floating point value, or a pixel tuple, depending on the expression. """ # build execution namespace args = ops.copy() args.update(_dict) args.update(kw) for k, v in list(args.items()): if hasattr(v, "im"): args[k] = _Operand(v) out = builtins.eval(expression, args) try: return out.im except AttributeError: return out
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 2755 |
|
|
BdfFontFile.py | File | 2.75 KB | 0644 |
|
BlpImagePlugin.py | File | 14 KB | 0644 |
|
BmpImagePlugin.py | File | 13.92 KB | 0644 |
|
BufrStubImagePlugin.py | File | 1.48 KB | 0644 |
|
ContainerIO.py | File | 2.82 KB | 0644 |
|
CurImagePlugin.py | File | 1.68 KB | 0644 |
|
DcxImagePlugin.py | File | 2.09 KB | 0644 |
|
DdsImagePlugin.py | File | 5.34 KB | 0644 |
|
EpsImagePlugin.py | File | 11.82 KB | 0644 |
|
ExifTags.py | File | 8.8 KB | 0644 |
|
FitsStubImagePlugin.py | File | 1.59 KB | 0644 |
|
FliImagePlugin.py | File | 4.23 KB | 0644 |
|
FontFile.py | File | 2.7 KB | 0644 |
|
FpxImagePlugin.py | File | 6.53 KB | 0644 |
|
FtexImagePlugin.py | File | 3.23 KB | 0644 |
|
GbrImagePlugin.py | File | 2.73 KB | 0644 |
|
GdImageFile.py | File | 2.47 KB | 0644 |
|
GifImagePlugin.py | File | 28.25 KB | 0644 |
|
GimpGradientFile.py | File | 3.27 KB | 0644 |
|
GimpPaletteFile.py | File | 1.24 KB | 0644 |
|
GribStubImagePlugin.py | File | 1.51 KB | 0644 |
|
Hdf5StubImagePlugin.py | File | 1.48 KB | 0644 |
|
IcnsImagePlugin.py | File | 11.44 KB | 0644 |
|
IcoImagePlugin.py | File | 9.94 KB | 0644 |
|
ImImagePlugin.py | File | 10.53 KB | 0644 |
|
Image.py | File | 113.4 KB | 0644 |
|
ImageChops.py | File | 7.13 KB | 0644 |
|
ImageCms.py | File | 36.22 KB | 0644 |
|
ImageColor.py | File | 8.44 KB | 0644 |
|
ImageDraw.py | File | 29.94 KB | 0644 |
|
ImageDraw2.py | File | 4.9 KB | 0644 |
|
ImageEnhance.py | File | 3.12 KB | 0644 |
|
ImageFile.py | File | 20.74 KB | 0644 |
|
ImageFilter.py | File | 15.46 KB | 0644 |
|
ImageFont.py | File | 43.49 KB | 0644 |
|
ImageGrab.py | File | 3.54 KB | 0644 |
|
ImageMath.py | File | 6.88 KB | 0644 |
|
ImageMode.py | File | 1.6 KB | 0644 |
|
ImageMorph.py | File | 7.67 KB | 0644 |
|
ImageOps.py | File | 18.03 KB | 0644 |
|
ImagePalette.py | File | 6.2 KB | 0644 |
|
ImagePath.py | File | 336 B | 0644 |
|
ImageQt.py | File | 5.67 KB | 0644 |
|
ImageSequence.py | File | 1.81 KB | 0644 |
|
ImageShow.py | File | 6.15 KB | 0644 |
|
ImageStat.py | File | 3.81 KB | 0644 |
|
ImageTk.py | File | 9.11 KB | 0644 |
|
ImageTransform.py | File | 2.78 KB | 0644 |
|
ImageWin.py | File | 7.02 KB | 0644 |
|
ImtImagePlugin.py | File | 2.15 KB | 0644 |
|
IptcImagePlugin.py | File | 5.6 KB | 0644 |
|
Jpeg2KImagePlugin.py | File | 8.52 KB | 0644 |
|
JpegImagePlugin.py | File | 27.16 KB | 0644 |
|
JpegPresets.py | File | 12.41 KB | 0644 |
|
McIdasImagePlugin.py | File | 1.71 KB | 0644 |
|
MicImagePlugin.py | File | 2.54 KB | 0644 |
|
MpegImagePlugin.py | File | 1.76 KB | 0644 |
|
MpoImagePlugin.py | File | 4.14 KB | 0644 |
|
MspImagePlugin.py | File | 5.43 KB | 0644 |
|
PSDraw.py | File | 6.51 KB | 0644 |
|
PaletteFile.py | File | 1.08 KB | 0644 |
|
PalmImagePlugin.py | File | 8.89 KB | 0644 |
|
PcdImagePlugin.py | File | 1.47 KB | 0644 |
|
PcfFontFile.py | File | 6.2 KB | 0644 |
|
PcxImagePlugin.py | File | 5.41 KB | 0644 |
|
PdfImagePlugin.py | File | 7.49 KB | 0644 |
|
PdfParser.py | File | 33.58 KB | 0644 |
|
PixarImagePlugin.py | File | 1.61 KB | 0644 |
|
PngImagePlugin.py | File | 42.79 KB | 0644 |
|
PpmImagePlugin.py | File | 4.34 KB | 0644 |
|
PsdImagePlugin.py | File | 7.56 KB | 0644 |
|
PyAccess.py | File | 9.37 KB | 0644 |
|
SgiImagePlugin.py | File | 5.96 KB | 0644 |
|
SpiderImagePlugin.py | File | 9.31 KB | 0644 |
|
SunImagePlugin.py | File | 4.2 KB | 0644 |
|
TarIO.py | File | 1.41 KB | 0644 |
|
TgaImagePlugin.py | File | 6.18 KB | 0644 |
|
TiffImagePlugin.py | File | 66.86 KB | 0644 |
|
TiffTags.py | File | 14.22 KB | 0644 |
|
WalImageFile.py | File | 5.4 KB | 0644 |
|
WebPImagePlugin.py | File | 10.54 KB | 0644 |
|
WmfImagePlugin.py | File | 4.56 KB | 0644 |
|
XVThumbImagePlugin.py | File | 1.9 KB | 0644 |
|
XbmImagePlugin.py | File | 2.37 KB | 0644 |
|
XpmImagePlugin.py | File | 3 KB | 0644 |
|
__init__.py | File | 3.18 KB | 0644 |
|
__main__.py | File | 41 B | 0644 |
|
_binary.py | File | 1.75 KB | 0644 |
|
_imaging.cpython-36m-x86_64-linux-gnu.so | File | 650.13 KB | 0755 |
|
_imagingcms.cpython-36m-x86_64-linux-gnu.so | File | 38.05 KB | 0755 |
|
_imagingft.cpython-36m-x86_64-linux-gnu.so | File | 41.98 KB | 0755 |
|
_imagingmath.cpython-36m-x86_64-linux-gnu.so | File | 24.43 KB | 0755 |
|
_imagingmorph.cpython-36m-x86_64-linux-gnu.so | File | 8.12 KB | 0755 |
|
_imagingtk.cpython-36m-x86_64-linux-gnu.so | File | 9.37 KB | 0755 |
|
_tkinter_finder.py | File | 224 B | 0644 |
|
_util.py | File | 359 B | 0644 |
|
_version.py | File | 50 B | 0644 |
|
_webp.cpython-36m-x86_64-linux-gnu.so | File | 40.82 KB | 0755 |
|
features.py | File | 8.8 KB | 0644 |
|