mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-06 05:20:09 +03:00
new style class definition
performance improvements through using of list comprehension
This commit is contained in:
parent
050e812214
commit
fe72e26f94
|
@ -232,8 +232,7 @@ def _save(im, fp, filename, check=0):
|
||||||
for i in (0, 255):
|
for i in (0, 255):
|
||||||
fp.write(o8(i) * 4)
|
fp.write(o8(i) * 4)
|
||||||
elif im.mode == "L":
|
elif im.mode == "L":
|
||||||
for i in range(256):
|
fp.write("".join(o8(i) * 4 for i in range(256)))
|
||||||
fp.write(o8(i) * 4)
|
|
||||||
elif im.mode == "P":
|
elif im.mode == "P":
|
||||||
fp.write(im.im.getpalette("RGB", "BGRX"))
|
fp.write(im.im.getpalette("RGB", "BGRX"))
|
||||||
|
|
||||||
|
|
|
@ -233,7 +233,7 @@ _MODE_CONV = {
|
||||||
"CMYK": ('|u1', 4),
|
"CMYK": ('|u1', 4),
|
||||||
"YCbCr": ('|u1', 3),
|
"YCbCr": ('|u1', 3),
|
||||||
"LAB": ('|u1', 3), # UNDONE - unsigned |u1i1i1
|
"LAB": ('|u1', 3), # UNDONE - unsigned |u1i1i1
|
||||||
# I;16 == I;16L, and I;32 == I;32L
|
# I;16 == I;16L, and I;32 == I;32L
|
||||||
"I;16": ('<u2', None),
|
"I;16": ('<u2', None),
|
||||||
"I;16B": ('>u2', None),
|
"I;16B": ('>u2', None),
|
||||||
"I;16L": ('<u2', None),
|
"I;16L": ('<u2', None),
|
||||||
|
@ -920,8 +920,8 @@ class Image:
|
||||||
|
|
||||||
if box is None:
|
if box is None:
|
||||||
return self.copy()
|
return self.copy()
|
||||||
else:
|
else:
|
||||||
self.load()
|
self.load()
|
||||||
# lazy operation
|
# lazy operation
|
||||||
return _ImageCrop(self, box)
|
return _ImageCrop(self, box)
|
||||||
|
|
||||||
|
|
|
@ -86,7 +86,7 @@ def autocontrast(image, cutoff=0, ignore=None):
|
||||||
# get number of pixels
|
# get number of pixels
|
||||||
n = 0
|
n = 0
|
||||||
for ix in range(256):
|
for ix in range(256):
|
||||||
n = n + h[ix]
|
n += h[ix]
|
||||||
# remove cutoff% pixels from the low end
|
# remove cutoff% pixels from the low end
|
||||||
cut = n * cutoff // 100
|
cut = n * cutoff // 100
|
||||||
for lo in range(256):
|
for lo in range(256):
|
||||||
|
@ -147,13 +147,10 @@ def colorize(image, black, white):
|
||||||
assert image.mode == "L"
|
assert image.mode == "L"
|
||||||
black = _color(black, "RGB")
|
black = _color(black, "RGB")
|
||||||
white = _color(white, "RGB")
|
white = _color(white, "RGB")
|
||||||
red = []; green = []; blue = []
|
|
||||||
for i in range(256):
|
|
||||||
red.append(black[0]+i*(white[0]-black[0])//255)
|
|
||||||
green.append(black[1]+i*(white[1]-black[1])//255)
|
|
||||||
blue.append(black[2]+i*(white[2]-black[2])//255)
|
|
||||||
image = image.convert("RGB")
|
image = image.convert("RGB")
|
||||||
return _lut(image, red + green + blue)
|
return _lut(image, ([black[0]+i*(white[0]-black[0])//255 for i in range(256)] +
|
||||||
|
[black[1]+i*(white[1]-black[1])//255 for i in range(256)] +
|
||||||
|
[black[2]+i*(white[2]-black[2])//255 for i in range(256)]))
|
||||||
|
|
||||||
|
|
||||||
def crop(image, border=0):
|
def crop(image, border=0):
|
||||||
|
@ -357,10 +354,7 @@ def invert(image):
|
||||||
:param image: The image to invert.
|
:param image: The image to invert.
|
||||||
:return: An image.
|
:return: An image.
|
||||||
"""
|
"""
|
||||||
lut = []
|
return _lut(image, [255 - i for i in range(256)])
|
||||||
for i in range(256):
|
|
||||||
lut.append(255-i)
|
|
||||||
return _lut(image, lut)
|
|
||||||
|
|
||||||
|
|
||||||
def mirror(image):
|
def mirror(image):
|
||||||
|
@ -381,11 +375,8 @@ def posterize(image, bits):
|
||||||
:param bits: The number of bits to keep for each channel (1-8).
|
:param bits: The number of bits to keep for each channel (1-8).
|
||||||
:return: An image.
|
:return: An image.
|
||||||
"""
|
"""
|
||||||
lut = []
|
|
||||||
mask = ~(2**(8-bits)-1)
|
mask = ~(2**(8-bits)-1)
|
||||||
for i in range(256):
|
return _lut(image, [i & mask for i in range(256)])
|
||||||
lut.append(i & mask)
|
|
||||||
return _lut(image, lut)
|
|
||||||
|
|
||||||
|
|
||||||
def solarize(image, threshold=128):
|
def solarize(image, threshold=128):
|
||||||
|
@ -396,13 +387,7 @@ def solarize(image, threshold=128):
|
||||||
:param threshold: All pixels above this greyscale level are inverted.
|
:param threshold: All pixels above this greyscale level are inverted.
|
||||||
:return: An image.
|
:return: An image.
|
||||||
"""
|
"""
|
||||||
lut = []
|
return _lut(image, [i if i < threshold else 255 - i for i in range(256)])
|
||||||
for i in range(256):
|
|
||||||
if i < threshold:
|
|
||||||
lut.append(i)
|
|
||||||
else:
|
|
||||||
lut.append(255-i)
|
|
||||||
return _lut(image, lut)
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# PIL USM components, from Kevin Cazabon.
|
# PIL USM components, from Kevin Cazabon.
|
||||||
|
|
|
@ -20,7 +20,7 @@ import array
|
||||||
from PIL import Image, ImageColor
|
from PIL import Image, ImageColor
|
||||||
|
|
||||||
|
|
||||||
class ImagePalette:
|
class ImagePalette(object):
|
||||||
"Color palette for palette mapped images"
|
"Color palette for palette mapped images"
|
||||||
|
|
||||||
def __init__(self, mode = "RGB", palette = None, size = 0):
|
def __init__(self, mode = "RGB", palette = None, size = 0):
|
||||||
|
@ -120,19 +120,12 @@ def raw(rawmode, data):
|
||||||
# Factories
|
# Factories
|
||||||
|
|
||||||
def _make_linear_lut(black, white):
|
def _make_linear_lut(black, white):
|
||||||
lut = []
|
if black != 0:
|
||||||
if black == 0:
|
|
||||||
for i in range(256):
|
|
||||||
lut.append(white*i//255)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError # FIXME
|
raise NotImplementedError # FIXME
|
||||||
return lut
|
return [(white*i//255) for i in range(256)]
|
||||||
|
|
||||||
def _make_gamma_lut(exp, mode="RGB"):
|
def _make_gamma_lut(exp, mode="RGB"):
|
||||||
lut = []
|
return [int(((i / 255.0) ** exp) * 255.0 + 0.5) for i in range(256)]
|
||||||
for i in range(256):
|
|
||||||
lut.append(int(((i / 255.0) ** exp) * 255.0 + 0.5))
|
|
||||||
return lut
|
|
||||||
|
|
||||||
def new(mode, data):
|
def new(mode, data):
|
||||||
return Image.core.new_palette(mode, data)
|
return Image.core.new_palette(mode, data)
|
||||||
|
@ -144,10 +137,8 @@ def negative(mode="RGB"):
|
||||||
|
|
||||||
def random(mode="RGB"):
|
def random(mode="RGB"):
|
||||||
from random import randint
|
from random import randint
|
||||||
palette = []
|
|
||||||
for i in range(256*len(mode)):
|
return ImagePalette(mode, [randint(0, 255) for i in range(256 * len(mode))])
|
||||||
palette.append(randint(0, 255))
|
|
||||||
return ImagePalette(mode, palette)
|
|
||||||
|
|
||||||
def sepia(white="#fff0c0"):
|
def sepia(white="#fff0c0"):
|
||||||
r, g, b = ImageColor.getrgb(white)
|
r, g, b = ImageColor.getrgb(white)
|
||||||
|
|
|
@ -20,7 +20,7 @@ from PIL import Image
|
||||||
# the Python class below is overridden by the C implementation.
|
# the Python class below is overridden by the C implementation.
|
||||||
|
|
||||||
|
|
||||||
class Path:
|
class Path(object):
|
||||||
|
|
||||||
def __init__(self, xy):
|
def __init__(self, xy):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -30,7 +30,7 @@ except:
|
||||||
def rgb(r, g, b, a=255):
|
def rgb(r, g, b, a=255):
|
||||||
# use qRgb to pack the colors, and then turn the resulting long
|
# use qRgb to pack the colors, and then turn the resulting long
|
||||||
# into a negative integer with the same bitpattern.
|
# into a negative integer with the same bitpattern.
|
||||||
return (qRgba(r, g, b, a) & 0xffffffff)
|
return qRgba(r, g, b, a) & 0xffffffff
|
||||||
|
|
||||||
##
|
##
|
||||||
# An PIL image wrapper for Qt. This is a subclass of PyQt4's QImage
|
# An PIL image wrapper for Qt. This is a subclass of PyQt4's QImage
|
||||||
|
@ -57,9 +57,7 @@ class ImageQt(QImage):
|
||||||
format = QImage.Format_Mono
|
format = QImage.Format_Mono
|
||||||
elif im.mode == "L":
|
elif im.mode == "L":
|
||||||
format = QImage.Format_Indexed8
|
format = QImage.Format_Indexed8
|
||||||
colortable = []
|
colortable = [rgb(i, i, i) for i in range(256)]
|
||||||
for i in range(256):
|
|
||||||
colortable.append(rgb(i, i, i))
|
|
||||||
elif im.mode == "P":
|
elif im.mode == "P":
|
||||||
format = QImage.Format_Indexed8
|
format = QImage.Format_Indexed8
|
||||||
colortable = []
|
colortable = []
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
||||||
class Iterator:
|
class Iterator(object):
|
||||||
"""
|
"""
|
||||||
This class implements an iterator object that can be used to loop
|
This class implements an iterator object that can be used to loop
|
||||||
over an image sequence.
|
over an image sequence.
|
||||||
|
|
|
@ -17,7 +17,7 @@ from __future__ import print_function
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import os, sys
|
import os, sys
|
||||||
|
|
||||||
if(sys.version_info >= (3, 3)):
|
if sys.version_info >= (3, 3):
|
||||||
from shlex import quote
|
from shlex import quote
|
||||||
else:
|
else:
|
||||||
from pipes import quote
|
from pipes import quote
|
||||||
|
@ -52,7 +52,7 @@ def show(image, title=None, **options):
|
||||||
##
|
##
|
||||||
# Base class for viewers.
|
# Base class for viewers.
|
||||||
|
|
||||||
class Viewer:
|
class Viewer(object):
|
||||||
|
|
||||||
# main api
|
# main api
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ import operator, math
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
|
||||||
|
|
||||||
class Stat:
|
class Stat(object):
|
||||||
|
|
||||||
def __init__(self, image_or_list, mask = None):
|
def __init__(self, image_or_list, mask = None):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -54,7 +54,7 @@ def _pilbitmap_check():
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# PhotoImage
|
# PhotoImage
|
||||||
|
|
||||||
class PhotoImage:
|
class PhotoImage(object):
|
||||||
"""
|
"""
|
||||||
A Tkinter-compatible photo image. This can be used
|
A Tkinter-compatible photo image. This can be used
|
||||||
everywhere Tkinter expects an image object. If the image is an RGBA
|
everywhere Tkinter expects an image object. If the image is an RGBA
|
||||||
|
@ -192,7 +192,7 @@ class PhotoImage:
|
||||||
# BitmapImage
|
# BitmapImage
|
||||||
|
|
||||||
|
|
||||||
class BitmapImage:
|
class BitmapImage(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
A Tkinter-compatible bitmap image. This can be used everywhere Tkinter
|
A Tkinter-compatible bitmap image. This can be used everywhere Tkinter
|
||||||
|
|
|
@ -21,7 +21,7 @@ import warnings
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
class HDC:
|
class HDC(object):
|
||||||
"""
|
"""
|
||||||
Wraps a HDC integer. The resulting object can be passed to the
|
Wraps a HDC integer. The resulting object can be passed to the
|
||||||
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
|
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
|
||||||
|
@ -32,7 +32,7 @@ class HDC:
|
||||||
def __int__(self):
|
def __int__(self):
|
||||||
return self.dc
|
return self.dc
|
||||||
|
|
||||||
class HWND:
|
class HWND(object):
|
||||||
"""
|
"""
|
||||||
Wraps a HWND integer. The resulting object can be passed to the
|
Wraps a HWND integer. The resulting object can be passed to the
|
||||||
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
|
:py:meth:`~PIL.ImageWin.Dib.draw` and :py:meth:`~PIL.ImageWin.Dib.expose`
|
||||||
|
@ -44,7 +44,7 @@ class HWND:
|
||||||
return self.wnd
|
return self.wnd
|
||||||
|
|
||||||
|
|
||||||
class Dib:
|
class Dib(object):
|
||||||
"""
|
"""
|
||||||
A Windows bitmap with the given mode and size. The mode can be one of "1",
|
A Windows bitmap with the given mode and size. The mode can be one of "1",
|
||||||
"L", "P", or "RGB".
|
"L", "P", or "RGB".
|
||||||
|
@ -207,7 +207,7 @@ class Dib:
|
||||||
##
|
##
|
||||||
# Create a Window with the given title size.
|
# Create a Window with the given title size.
|
||||||
|
|
||||||
class Window:
|
class Window(object):
|
||||||
|
|
||||||
def __init__(self, title="PIL", width=None, height=None):
|
def __init__(self, title="PIL", width=None, height=None):
|
||||||
self.hwnd = Image.core.createwindow(
|
self.hwnd = Image.core.createwindow(
|
||||||
|
|
|
@ -173,8 +173,7 @@ def _save(im, fp, filename, check=0):
|
||||||
elif im.mode == "L":
|
elif im.mode == "L":
|
||||||
# greyscale palette
|
# greyscale palette
|
||||||
fp.write(o8(12))
|
fp.write(o8(12))
|
||||||
for i in range(256):
|
fp.write("".join(o8(i)*3 for i in range(256)))
|
||||||
fp.write(o8(i)*3)
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# registry
|
# registry
|
||||||
|
|
Loading…
Reference in New Issue
Block a user