当前位置:首页>python>Python零基础入门(九):Pillow图片处理进阶

Python零基础入门(九):Pillow图片处理进阶

  • 2026-10-11 05:50:57
Python零基础入门(九):Pillow图片处理进阶

Python零基础入门(九):Pillow图片处理进阶

上篇搞定了基础操作——打开、保存、缩放、裁剪、旋转、调色。

这篇来点实用的:画文字图形、批量处理图片、做一个证件照生成器。

👉 没看过上篇?点这里

一、绘制文字和图形

画基本图形

from PIL import Image, ImageDrawimg = Image.new("RGB", (400, 300), "white")draw = ImageDraw.Draw(img)# 画线:起点、终点、颜色、粗细draw.line([(0, 0), (400, 300)], fill="red", width=3)# 画矩形:左上角、右下角draw.rectangle([(50, 50), (150, 100)], outline="green", width=2)draw.rectangle([(200, 50), (300, 100)], fill="yellow")  # 填充色# 画椭圆draw.ellipse([(50, 150), (150, 200)], outline="purple", width=2)# 画多边形draw.polygon([(200, 220), (250, 280), (300, 220)], fill="pink")img.save("shapes.png")

写文字

from PIL import Image, ImageDraw, ImageFontimg = Image.new("RGB", (400, 200), "white")draw = ImageDraw.Draw(img)# 默认字体(很小,只适合测试)draw.text((50, 50), "Hello, Pillow!", fill="black")# 用自定义字体(中文必须用中文字体文件)try:    font = ImageFont.truetype("C:/Windows/Fonts/msyh.ttc", 36)  # 微软雅黑except:    font = ImageFont.load_default()draw.text((50, 100), "你好,世界!", fill="red", font=font)img.save("text.png")

字体路径参考:

  • • Windows:C:/Windows/Fonts/msyh.ttc(微软雅黑)、simhei.ttf(黑体)
  • • Mac:/System/Library/Fonts/PingFang.ttc
  • • Linux:/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf

添加水印

这是实际项目里最常见的需求:

from PIL import Image, ImageDraw, ImageFontdef add_watermark(image_path, text, output_path,                   position="bottom-right", opacity=128):    """    给图片加文字水印    position: top-left, top-right, bottom-left, bottom-right, center    opacity: 透明度 0-255    """    img = Image.open(image_path).convert("RGBA")    # 新建透明图层画水印    watermark = Image.new("RGBA", img.size, (0, 0, 0, 0))    draw = ImageDraw.Draw(watermark)    try:        font = ImageFont.truetype("C:/Windows/Fonts/msyh.ttc", 36)    except:        font = ImageFont.load_default()    # 算文字尺寸    bbox = draw.textbbox((0, 0), text, font=font)    text_w = bbox[2] - bbox[0]    text_h = bbox[3] - bbox[1]    # 算位置    margin = 20    positions = {        "top-left": (margin, margin),        "top-right": (img.width - text_w - margin, margin),        "bottom-left": (margin, img.height - text_h - margin),        "bottom-right": (img.width - text_w - margin, img.height - text_h - margin),        "center": ((img.width - text_w) // 2, (img.height - text_h) // 2),    }    x, y = positions.get(position, positions["bottom-right"])    # 画水印    draw.text((x, y), text, fill=(255, 255, 255, opacity), font=font)    # 合并图层,转回RGB保存    result = Image.alpha_composite(img, watermark)    result.convert("RGB").save(output_path, quality=95)    print(f"水印已添加:{output_path}")# 用法add_watermark("photo.jpg", "© 2024 My Photo", "output.jpg")

二、批量处理

一张一张手动处理?不存在的。写个循环批量搞定。

批量调整尺寸

import osfrom PIL import Imagedef batch_resize(input_dir, output_dir, size=(800, 600)):    os.makedirs(output_dir, exist_ok=True)    for f in os.listdir(input_dir):        if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.webp')):            try:                with Image.open(os.path.join(input_dir, f)) as img:                    img.thumbnail(size, Image.Resampling.LANCZOS)                    img.save(os.path.join(output_dir, f), quality=95)                    print(f"✓ {f}")            except Exception as e:                print(f"✗ {f}: {e}")# batch_resize("photos", "photos_resized", (1024, 768))

批量转格式

def batch_convert(input_dir, output_dir, target="PNG"):    os.makedirs(output_dir, exist_ok=True)    for f in os.listdir(input_dir):        if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.webp')):            name = os.path.splitext(f)[0]            new_f = f"{name}.{target.lower()}"            try:                img = Image.open(os.path.join(input_dir, f))                if target == "JPEG" and img.mode == "RGBA":                    img = img.convert("RGB")  # JPEG不支持透明                img.save(os.path.join(output_dir, new_f), target)                print(f"✓ {f} → {new_f}")            except Exception as e:                print(f"✗ {f}: {e}")# batch_convert("photos_png", "photos_jpg", "JPEG")

生成缩略图网格

把一堆图片拼成一张预览图,方便查看:

def create_thumbnail_grid(input_dir, output_path, thumb_size=(150, 150), cols=4, padding=10):    files = [f for f in os.listdir(input_dir)             if f.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.webp'))]    if not files:        print("没找到图片")        return    rows = (len(files) + cols - 1) // cols    w = cols * (thumb_size[0] + padding) + padding    h = rows * (thumb_size[1] + padding) + padding    grid = Image.new("RGB", (w, h), (255, 255, 255))    for i, f in enumerate(files):        img = Image.open(os.path.join(input_dir, f))        img.thumbnail(thumb_size, Image.Resampling.LANCZOS)        row, col = i // cols, i % cols        x = padding + col * (thumb_size[0] + padding) + (thumb_size[0] - img.width) // 2        y = padding + row * (thumb_size[1] + padding) + (thumb_size[1] - img.height) // 2        grid.paste(img, (x, y))    grid.save(output_path, quality=95)    print(f"网格已保存:{output_path}")# create_thumbnail_grid("photos", "grid.jpg", cols=5)

三、综合实战:证件照处理器

把前面学的全串起来,做一个实用的证件照工具:

from PIL import Imageimport osclass IDPhotoProcessor:    """证件照处理器"""    # 常用尺寸(300dpi下的像素值)    SIZES = {        "一寸": (295, 413),        "二寸": (413, 579),        "小一寸": (260, 378),        "小二寸": (413, 531),    }    def __init__(self, image_path):        self.original = Image.open(image_path)        self.processed = self.original.copy()    def resize_to_id(self, size_name="一寸"):        """调整为证件照尺寸(等比缩放,居中填充白边)"""        if size_name not in self.SIZES:            raise ValueError(f"不支持:{size_name},可选:{list(self.SIZES.keys())}")        target = self.SIZES[size_name]        # 等比缩放        ratio = min(target[0] / self.processed.width, target[1] / self.processed.height)        new_size = (int(self.processed.width * ratio), int(self.processed.height * ratio))        self.processed = self.processed.resize(new_size, Image.Resampling.LANCZOS)        # 居中放到白色画布上        canvas = Image.new("RGB", target, (255, 255, 255))        x = (target[0] - new_size[0]) // 2        y = (target[1] - new_size[1]) // 2        canvas.paste(self.processed, (x, y))        self.processed = canvas        return self    def add_border(self, width=2, color=(0, 0, 0)):        """加边框"""        w, h = self.processed.size        canvas = Image.new("RGB", (w + 2 * width, h + 2 * width), color)        canvas.paste(self.processed, (width, width))        self.processed = canvas        return self    def create_print_sheet(self, count=8, size_name="一寸"):        """生成A4打印排版"""        self.resize_to_id(size_name)        photo = self.processed.copy()        # A4 300dpi: 2480x3508        a4_w, a4_h = 2480, 3508        pad = 50        cols = (a4_w - pad) // (photo.width + pad)        rows = (a4_h - pad) // (photo.height + pad)        sheet = Image.new("RGB", (a4_w, a4_h), (255, 255, 255))        total_w = cols * (photo.width + pad) - pad        total_h = rows * (photo.height + pad) - pad        x_off = (a4_w - total_w) // 2        y_off = (a4_h - total_h) // 2        placed = 0        for row in range(rows):            for col in range(cols):                if placed >= count:                    break                x = x_off + col * (photo.width + pad)                y = y_off + row * (photo.height + pad)                sheet.paste(photo, (x, y))                placed += 1        return sheet    def save(self, output_path, quality=95):        os.makedirs(os.path.dirname(output_path) or ".", exist_ok=True)        if self.processed.mode == "RGBA":            self.processed = self.processed.convert("RGB")        self.processed.save(output_path, quality=quality)        print(f"已保存:{output_path}")

用法:

# 生成一寸照processor = IDPhotoProcessor("my_photo.jpg")processor.resize_to_id("一寸")processor.save("output/一寸.jpg")# 生成带边框的二寸照processor = IDPhotoProcessor("my_photo.jpg")processor.resize_to_id("二寸").add_border().save("output/二寸_边框.jpg")# 生成A4打印排版processor = IDPhotoProcessor("my_photo.jpg")sheet = processor.create_print_sheet(count=8, size_name="一寸")sheet.save("output/一寸_排版.jpg")

小结

上下两篇加起来,Pillow的核心用法就这些:

场景
关键方法
打开/保存
Image.open()
 / .save()
缩放
resize()
强制尺寸,thumbnail()等比
裁剪
crop((左, 上, 右, 下))
旋转翻转
rotate()
 / transpose()
调色
ImageEnhance
系列
滤镜
ImageFilter
系列
绘制
ImageDraw
画图写字
批量
循环 + 函数封装

日常图片处理需求,Pillow基本都能覆盖。更高级的(人像抠图、目标检测)可以看看OpenCV或深度学习方案。

最新文章

随机文章