pillow使用小技巧

pillow比较实用的参考网站

中文参考:https://www.osgeo.cn/pillow/index.html
中文参考:https://pillow-cn.readthedocs.io/zh_CN/latest/
英文参考:https://pillow.readthedocs.io/en/stable/index.html

查看图像模式、尺寸和通道数

img = Image.open("./0001.png")
print("模式:", img.mode)
print("尺寸:", img.size)
print("通道数:", len(img.split()))

常用函数总结

image = Image.open("0024.jpg")
image.resize((100, 100)) # 缩放
image = image.convert("L")  # 转灰度
b, g, r = image.split()  # 通道分离
image = Image.merge("RGB", (g, b, r))  # 通道合并

PIL格式与numpy格式转换

image = Image.open("0024.jpg")
print(type(image))
image_arr = np.array(image)
print("image_arr: ", type(image_arr))
image_pil = Image.fromarray(image_arr)
print("image_pil: ", type(image_pil))

PIL读入图片格式是(宽,高,通道),cv读入图片的格式(高,宽,通道)

img_path = "1.jpg"
img1 = Image.open(img_path)
img2 = cv.imread(img_path , cv.IMREAD_UNCHANGED)
print(img1.size, img2.shape)

图像裁剪

def img_crop(image, location, height=100, width=100):
    box = (location[0], location[1], height, width)
    return image.crop(box)

image = Image.open("0024.jpg")
image = img_crop(image, (20, 10))
image.save("1111.jpg")

在图片中绘制矩形框

def draw_rectangle(image, location, width, height, color="red"):
    draw = ImageDraw.Draw(image)
    draw.rectangle([(location[0], location[1]), (location[0]+width, location[1]+height)], outline=color, width=2)
    return image

img_path = "./JPEGImages/0001.jpg"
image = Image.open(img_path)
image = draw_rectangle(image, [50, 50], 500, 500, color="green")
image.save("./img.jpg")

window下使用PIL在图片上添加汉字

在windows系统下,字体文件位于C:\Windows\Fonts\文件夹下

from PIL import Image, ImageDraw, ImageFont

img = Image.open("0024.jpg")
draw = ImageDraw.Draw(img)
content = "小耳朵音乐!"
font = ImageFont.truetype('C:/Windows/Fonts/kaiu.ttf', 130)
draw.text((100, 300), content, font=font, fill="black")
img.save("imgimg.jpg")

后续更新…

<think>好的,我需要帮助用户了解如何使用Python的Pillow库在图片上添加文字。首先,我需要确认用户的需求:他们想通过Pillow库在图片上添加文字,并需要示例代码。用户可能已经知道Pillow的基本安装,但可能需要具体的步骤和代码示例。 首先,我应该检查Pillow库的安装方法,确保用户已经正确安装。根据引用[3],用户可以从PyPI获取最新版本,特别是Windows用户可能需要二进制文件。所以,在回答中应该提到安装步骤,比如使用pip安装。 接下来,用户需要具体的代码示例。根据引用[2],Pillow可以用于合并图片,比如添加水印或logo,这也包括添加文字。因此,我需要解释如何打开图片,使用ImageDraw模块创建绘图对象,然后使用text()方法添加文字。 我需要考虑设置字体、颜色、位置等参数。字体可能需要指定路径,特别是用户可能需要下载.ttf文件,比如使用默认的Arial字体或者提供其他字体选项。如果用户未指定字体,Pillow可能无法正确渲染文本,所以需要提醒这一点。 此外,文字的位置坐标(xy)可能需要调整,用户可能需要根据图片尺寸来动态计算,比如居中文本。这里可以给出一个示例,计算文本的宽度和高度,然后调整位置。 还需要提到如何保存修改后的图片,使用save()方法,并可能支持不同的格式。 最后,用户可能需要进一步的自定义,比如调整字体大小、颜色、透明度等,所以在相关问题上可以建议这些方向。 现在,整理步骤:安装Pillow,导入模块,打开图片,创建Draw对象,定义字体,添加文本,保存图片。示例代码需要包括这些步骤,并处理可能出现的异常,比如字体文件未找到。 可能的问题点:字体路径在不同操作系统下的处理,比如在Linux下可能使用/usr/share/fonts,而Windows可能在C:\Windows\Fonts。需要提醒用户根据实际情况调整路径。 检查是否需要处理透明度或图层混合,但基础示例可能不需要这些,高级用户可以自行探索。 总结回答结构:先介绍步骤,给出示例代码,然后解释代码各部分,最后提供相关问题和引用。</think>要使用Pillow库在图片上添加文字,可通过以下步骤实现: ### 步骤说明 1. **安装Pillow库** ```bash pip install Pillow ``` 2. **基础代码框架** ```python from PIL import Image, ImageDraw, ImageFont # 打开图片 image = Image.open("input.jpg") # 创建绘图对象 draw = ImageDraw.Draw(image) # 设置字体(需指定.ttf文件路径) font = ImageFont.truetype("arial.ttf", 36) # 添加文字(参数:坐标、内容、颜色、字体) draw.text((50, 50), "Hello Pillow", fill="white", font=font) # 保存结果 image.save("output.jpg") ``` ### 详细说明 - **字体设置**:必须使用`.truetype()`加载字体文件。若未安装字体,可从系统字体目录(如Windows的`C:\Windows\Fonts`)复制.ttf文件到项目目录[^3] - **坐标系统**:原点`(0,0)`在左上角,可通过`image.size`获取图片尺寸动态计算居中位置: ```python text_width, text_height = draw.textsize("Hello Pillow", font=font) x = (image.width - text_width) // 2 y = (image.height - text_height) // 2 ``` ### 完整示例(含居中文字与抗锯齿) ```python from PIL import Image, ImageDraw, ImageFont def add_centered_text(input_path, output_path, text, font_size=40, color=(255,255,255)): img = Image.open(input_path) draw = ImageDraw.Draw(img) try: font = ImageFont.truetype("arial.ttf", font_size) except IOError: font = ImageFont.load_default() # 备选方案 text_width, text_height = draw.textsize(text, font=font) x = (img.width - text_width) // 2 y = (img.height - text_height) // 2 # 添加黑色描边增强可读性 draw.text((x-1, y-1), text, fill="black", font=font) draw.text((x+1, y), text, fill="black", font=font) draw.text((x, y), text, fill=color, font=font) img.save(output_path) # 使用示例 add_centered_text("background.jpg", "result.jpg", "WATERMARK", color=(255,0,0)) ``` ### 效果增强技巧 - 通过多层文字绘制实现描边效果 - 使用`.alpha_composite()`叠加半透明水印[^2] - 调整`font`参数中的`stroke_width`实现文字边框
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值