Image
最重要的类.
>>> from PIL import Image
>>> im = Image.open("hopper.ppm")
检查属性:
>>> print(im.format, im.size, im.mode)
PPM (512, 512) RGB
>>> im.show()
//显示
import os, sys
from PIL import Image
for infile in sys.argv[1:]:
f, e = os.path.splitext(infile)
outfile = f + ".jpg"
if infile != outfile:
try:
with Image.open(infile) as im:
im.save(outfile)
//保存文件.
except OSError:
print("不能转换", infile)
import os, sys
from PIL import Image
size = (128, 128)
for infile in sys.argv[1:]:
outfile = os.path.splitext(infile)[0] + ".thumbnail"
if infile != outfile:
try:
with Image.open(infile) as im:
im.thumbnail(size)
im.save(outfile, "JPEG")
except OSError:
print("失败", infile)
import sys
from PIL import Image
for infile in sys.argv[1:]:
try:
with Image.open(infile) as im:
print(infile, im.format, f"{im.size}x{im.mode}")
except OSError:
pass
box = (100, 100, 400, 400)
region = im.crop(box)
//
region = region.transpose(Image.Transpose.ROTATE_180)
im.paste(region, box)
def roll(im, delta):
"""滚动"""
xsize, ysize = im.size
delta = delta % xsize
if delta == 0:
return im
part1 = im.crop((0, 0, delta, ysize))
part2 = im.crop((delta, 0, xsize, ysize))
im.paste(part1, (xsize - delta, 0, xsize, ysize))
im.paste(part2, (0, 0, xsize - delta, ysize))
return im
def merge(im1, im2):
w = im1.size[0] + im2.size[0]
h = max(im1.size[1], im2.size[1])
im = Image.new("RGBA", (w, h))
im.paste(im1)
im.paste(im2, (im1.size[0], 0))
return im
r, g, b = im.split()
im = Image.merge("RGB", (b, g, r))
out = im.resize((128, 128))
out = im.rotate(45)
//旋转
out = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
out = im.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
out = im.transpose(Image.Transpose.ROTATE_90)
out = im.transpose(Image.Transpose.ROTATE_180)
out = im.transpose(Image.Transpose.ROTATE_270)
from PIL import Image
with Image.open("hopper.ppm") as im:
im = im.convert("L")
from PIL import ImageFilter
out = im.filter(ImageFilter.DETAIL)
# 都乘1.2
out = im.point(lambda i: i * 1.2)
source = im.split()
R, G, B = 0, 1, 2
mask = source[R].point(lambda i: i < 100 and 255)
out = source[G].point(lambda i: i * 0.7)
source[G].paste(out, None, mask)
im = Image.merge(im.mode, source)
//掩码
imout = im.point(lambda i: expression and 255)
from PIL import ImageEnhance
enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30%对比")
from PIL import Image
with Image.open("animation.gif") as im:
im.seek(1)
// 跳过第2个
try:
while 1:
im.seek(im.tell() + 1)
//干活
except EOFError:
pass //序列外
from PIL import ImageSequence
for frame in ImageSequence.Iterator(im):
//干活.
from PIL import Image
from PIL import PSDraw
with Image.open("hopper.ppm") as im:
title = "hopper"
box = (1 * 72, 2 * 72, 7 * 72, 10 * 72) # in points
ps = PSDraw.PSDraw()
ps.begin_document(title)
//#画
ps.image(box, im, 75)
ps.rectangle(box)
//画标题
ps.setfont("HelveticaNarrow-Bold", 36)
ps.text((3 * 72, 4 * 72), title)
ps.end_document()
from PIL import Image
with open("hopper.ppm", "rb") as fp:
im = Image.open(fp)
from PIL import Image
import io
im = Image.open(io.BytesIO(buffer))
from PIL import Image
from urllib.request import urlopen
url = "https://python-pillow.org/images/pillow-logo.png"
img = Image.open(urlopen(url))
from PIL import Image, TarIO
fp = TarIO.TarIO("Tests/images/hopper.tar", "hopper.jpg")
im = Image.open(fp)
import glob
from PIL import Image
def compress_image(source_path, dest_path):
with Image.open(source_path) as img:
if img.mode != "RGB":
img = img.convert("RGB")
img.save(dest_path, "JPEG", optimize=True, quality=80)
paths = glob.glob("*.png")
for path in paths:
compress_image(path, path[:-4] + ".jpg")
//
from pathlib import Path
paths = Path(".").glob("*.png")
for path in paths:
compress_image(path, path.stem + ".jpg")