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