陕西网站建设品牌公司推荐,重庆大渡口营销型网站建设价格,设计企业网络方案的五个步骤,南京建设网站报价一、概述
1.1pillow简介
Python Imaging Library (PIL)是python 下的图像处理模块,支持多种格式#xff0c;并提供强大的图像处理功能#xff0c;可以通过pip进行安装后使用。
1.2pillow具体应用 Pillow 库是 Python3 最常用的图像处理库#xff0c;它支持多种图像格式并提供强大的图像处理功能可以通过pip进行安装后使用。
1.2pillow具体应用 Pillow 库是 Python3 最常用的图像处理库它支持多种图像格式可以用于图像处理、图像增强、图像合成等。下面是 Pillow 库中的一些常用函数open()打开一张图片new()创建一张新的图片save()保存一张图片show()显示一张图片resize()改变图片的大小crop()裁剪图片rotate()旋转图片transpose()翻转图片convert()转换图片的格式或色彩模式filter()使用滤镜处理图片getpixel()获取图片中指定位置的像素值putpixel()设置图片中指定位置的像素值paste()将一张图片粘贴到另一张图片上。 1.3快速入门
通过文件创建 Image 对象
通过文件创建 Image 图像对象是最常用的方法
示例通过文件创建 Image 图像对象
from PIL import Imageimage Image.open(D://桌面//1.jpeg) # 创建图像实例
# 查看图像实例的属性
print(image.format, image.size, image.mode)image.show() # 显示图像 格式转换并保存图像
pillow Image 模块中的 save 函数可以保存图片除非你指定文件格式否则文件的扩展名就是文件格式。
from PIL import Image
import os# 打开图片
image_path D://桌面//1.png
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 指定输出文件格式
output_format jpeg # 要保存的文件格式比如 PNG、JPEG 等# 创建输出文件名
outfile f . output_format.lower()# 保存图片并指定文件格式
image.save(outfile, formatoutput_format)print(f图片已保存为 {outfile}格式为 {output_format})
image.show()
save() 函数有两个参数如果文件名没有指定图片格式那么第二个参数是必须的他指定图片的格式。
二、课堂实操
2.1 创建缩略图 创建缩略图 使用 Image.thumbnail( size ), size 为缩略图宽长元组。
示例 创建缩略图
from PIL import Image
import os# 打开图片
image_path D://桌面//1.png
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 指定输出文件格式
output_format PNG # 要保存的文件格式比如 PNG、JPEG 等# 创建输出文件名
outfile f _thumbnail. output_format.lower()# 创建缩略图
thumbnail_size (100, 100) # 缩略图尺寸
image.thumbnail(thumbnail_size)# 保存缩略图并指定文件格式
image.save(outfile, formatoutput_format)print(f缩略图已保存为 {outfile}格式为 {output_format})2.2剪贴粘贴、合并图像
2.2.1Image类包含允许您操作图像中的区域的方法。如要从图像中复制子矩形图像使用 crop() 方法。
2.2.2注意将子图region 粘贴paste回原图时粘贴位置 box 的像素与宽高必须吻合。而原图和子图的 mode 不需要匹配Pillow会自动处理。
粘贴其它图片
from PIL import Image
import os# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 剪切区域
crop_box (100, 100, 300, 300) # 左上角坐标为 (100, 100)右下角坐标为 (300, 300)
cropped_image image.crop(crop_box)# 打开另一张图片作为粘贴对象
paste_image_path D://桌面//2.jpeg
paste_image Image.open(paste_image_path)# 调整粘贴对象的尺寸和模式以匹配剪切区域
paste_image paste_image.resize((crop_box[2] - crop_box[0], crop_box[3] - crop_box[1]))# 在原图上粘贴剪切区域
image.paste(paste_image, crop_box)# 保存合并后的图像
merged_outfile f _merged. e
image.save(merged_outfile)print(f合并后的图像已保存为 {merged_outfile})当你从一个图像中剪切出一个区域然后希望将这个剪切出的区域粘贴到另一个图像上时剪切出的区域和粘贴目标的尺寸和模式也就是图像的大小和颜色通道等信息需要匹配以确保粘贴操作能够成功。 在你的代码中你已经定义了一个剪切区域 crop_box这个区域的左上角坐标为 (100, 100)右下角坐标为 (300, 300)。剪切区域的宽度是 300 - 100 200高度是 300 - 100 200。 粘贴自身图片
from PIL import Image
import os# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 剪切区域
crop_box (100, 100, 300, 300) # 左上角坐标为 (100, 100)右下角坐标为 (300, 300)
cropped_image image.crop(crop_box)# 在原图上粘贴剪切区域
image.paste(cropped_image, crop_box)# 对粘贴的图像进行颠倒180度
region image.crop(crop_box)
region region.transpose(Image.ROTATE_180)# 将颠倒后的图像再次粘贴回原图上
image.paste(region, crop_box)# 保存合并后的图像
merged_outfile f _merged1. e
image.save(merged_outfile)print(f合并后的图像已保存为 {merged_outfile})2.3几何变换
当使用PillowPIL库的Image模块进行几何变换时你可以使用rotate()方法或transpose()方法来实现不同类型的旋转和翻转。
使用rotate()方法进行图像旋转
from PIL import Image# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 定义旋转角度以度为单位逆时针方向为正
angle 45# 旋转图像
rotated_image image.rotate(angle)# 保存旋转后的图像
rotated_outfile f _rotated. e
rotated_image.save(rotated_outfile)print(f旋转后的图像已保存为 {rotated_outfile})使用transpose()方法进行图像翻转
from PIL import Image# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 对图像进行水平翻转
flipped_image_horizontal image.transpose(Image.FLIP_LEFT_RIGHT)# 对图像进行垂直翻转
flipped_image_vertical image.transpose(Image.FLIP_TOP_BOTTOM)# 保存翻转后的图像
flipped_horizontal_outfile f _flipped_horizontal. e
flipped_image_horizontal.save(flipped_horizontal_outfile)flipped_vertical_outfile f _flipped_vertical. e
flipped_image_vertical.save(flipped_vertical_outfile)print(f水平翻转后的图像已保存为 {flipped_horizontal_outfile})
print(f垂直翻转后的图像已保存为 {flipped_vertical_outfile})out im.transpose(Image.FLIP_LEFT_RIGHT) # 水平左右翻转
out im.transpose(Image.FLIP_TOP_BOTTOM) # 垂直上下翻转
out im.transpose(Image.ROTATE_90) # 逆时针90度
out im.transpose(Image.ROTATE_180) # 逆时针180度
out im.transpose(Image.ROTATE_270) # 逆时针270度 2.4颜色变换
from PIL import Image
import numpy as np# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 改变图像亮度
brightness_factor 1.5
brightened_image Image.eval(image, lambda x: min(255, x * brightness_factor))# 修改图像通道颜色
r, g, b image.split()
new_image Image.merge(RGB, (b, r, g))# 保存修改后的图像
brightened_outfile f _brightened. e
new_image.save(brightened_outfile)print(f修改后的图像已保存为 {brightened_outfile})Image.eval() 方法允许我们在像素级别对图像进行操作。在这个示例中我们使用了一个lambda函数将每个像素的值乘以亮度因子来改变图像的亮度。 使用 split() 和 merge() 方法来重新排列图像的颜色通道。在这个示例中我们将原始图像的红色和蓝色通道进行交换以改变颜色分布。 灰度图像
from PIL import Image# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 将彩色图像转换为灰度图像
gray_image image.convert(L)# 保存灰度图像
gray_outfile f _gray. e
gray_image.save(gray_outfile)print(f灰度图像已保存为 {gray_outfile})PillowPIL库支持多种图像模式每种模式具有不同的颜色通道和像素表示方式。以下是一些常见的图像模式 RGBRGB每个像素由红色、绿色和蓝色通道组成是彩色图像的常见模式。灰度L每个像素只有一个通道表示像素的亮度值用于灰度图像。RGBARGBA类似于RGB还包括一个透明度通道用于带有透明背景的彩色图像。CMYKCMYK一种印刷颜色模式使用青色、洋红色、黄色和黑色通道表示颜色。HSVHSV使用色相、饱和度和值通道来表示颜色。二值化1每个像素只有一个二进制值用于表示黑白图像。 可以根据需要使用convert()方法将图像从一种模式转换为另一种模式 2.5图像增强
from PIL import Image, ImageEnhance# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 增强图像亮度
enhancer ImageEnhance.Brightness(image)
brightened_image enhancer.enhance(1.5) # 1.5表示增强的倍数# 增强图像对比度
enhancer ImageEnhance.Contrast(image)
high_contrast_image enhancer.enhance(1.5) # 1.5表示增强的倍数# 增强图像色彩
enhancer ImageEnhance.Color(image)
colorful_image enhancer.enhance(1.5) # 1.5表示增强的倍数# 增强图像锐度
enhancer ImageEnhance.Sharpness(image)
sharp_image enhancer.enhance(2.0) # 2.0表示增强的倍数# 保存增强后的图像
brightened_outfile f _brightened. e
high_contrast_outfile f _high_contrast. e
colorful_outfile f _colorful. e
sharp_outfile f _sharp. ebrightened_image.save(brightened_outfile)
high_contrast_image.save(high_contrast_outfile)
colorful_image.save(colorful_outfile)
sharp_image.save(sharp_outfile)print(f亮度增强后的图像已保存为 {brightened_outfile})
print(f对比度增强后的图像已保存为 {high_contrast_outfile})
print(f色彩增强后的图像已保存为 {colorful_outfile})
print(f锐度增强后的图像已保存为 {sharp_outfile})其他图像增强功能可以使用 ImageEnhance 模块中的类。从图像创建后可以使用 ImageEnhance 快速调整图片的对比度、亮度、饱和度和清晰度。 ImageEnhance 方法类型 ImageEnhance.Contrast(im) 对比度
ImageEnhance.Color(im) 色彩饱和度
ImageEnhance.Brightness(im) 亮度
ImageEnhance.Sharpness(im) 清晰度 处理单独通道
处理图像的单独通道意味着你可以选择性地对图像的红色、绿色、蓝色或灰度通道进行操作。PillowPIL库允许你通过拆分图像的通道对每个通道进行处理然后重新合并通道来生成处理后的图像。
from PIL import Image# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 拆分图像的通道
r, g, b image.split()# 处理单独的通道
r r.point(lambda p: p * 0.8) # 处理红色通道降低亮度
g g.point(lambda p: p * 1.2) # 处理绿色通道增加亮度
b b.point(lambda p: p * 0.9) # 处理蓝色通道降低亮度# 合并通道
processed_image Image.merge(RGB, (r, g, b))# 保存处理后的图像
processed_outfile f _processed. e
processed_image.save(processed_outfile)print(f处理后的图像已保存为 {processed_outfile})处理灰度通道
from PIL import Image# 打开图片
image_path D://桌面//1.jpeg
image Image.open(image_path)# 获取文件名与后缀
f, e os.path.splitext(image_path)# 将图像转换为灰度模式
gray_image image.convert(L)# 处理灰度图像的单独通道
processed_gray_image gray_image.point(lambda p: p * 0.8) # 降低亮度# 保存处理后的灰度图像
processed_gray_outfile f _processed_gray. e
processed_gray_image.save(processed_gray_outfile)print(f处理后的灰度图像已保存为 {processed_gray_outfile})2.6动态图像
Pillow 支持一些动态图像处理如FLI/FLCGIF等格式。TIFF文件同样可以包含数帧图像。
打开动态图像时PIL 会自动加载序列中的第一帧。你可以使用 seek 和 tell 方法在不同的帧之间移动。
2.7水印
PillowPIL库允许你在图像上添加图像、文字或水印。
图像水印
from PIL import Image# 打开主图像和水印图像
main_image_path D://桌面//1.jpeg
watermark_image_path D://桌面//2.jpegmain_image Image.open(main_image_path)
watermark_image Image.open(watermark_image_path)# 将水印图像缩放到合适的大小
watermark_size (100, 100) # 替换为合适的尺寸
watermark_image watermark_image.resize(watermark_size, Image.ANTIALIAS)# 创建一个透明度通道
alpha_channel Image.new(L, watermark_image.size, 128) # 128表示透明度0为完全透明255为不透明# 将透明度通道与水印图像合并
watermark_image.putalpha(alpha_channel)# 计算水印位置右下角
main_width, main_height main_image.size
watermark_width, watermark_height watermark_image.size
x main_width - watermark_width
y main_height - watermark_height# 在主图像上添加透明水印
main_image.paste(watermark_image, (x, y), watermark_image)# 保存带有透明水印的图像
watermarked_outfile f _watermarked. e
main_image.save(watermarked_outfile)print(f带有透明水印的图像已保存为 {watermarked_outfile})文本水印
from PIL import Image, ImageDraw, ImageFont# 打开图片
main_image_path D://桌面//1.jpeg
main_image Image.open(main_image_path)# 获取文件名与后缀
f, e os.path.splitext(main_image_path)# 创建一个ImageDraw对象
draw ImageDraw.Draw(main_image)# 定义水印文本和字体
watermark_text Watermark
font ImageFont.truetype(arial.ttf, 36) # 替换为合适的字体路径和大小# 计算水印文本的尺寸
text_width, text_height draw.textsize(watermark_text, font)# 创建一个透明度通道
alpha_channel Image.new(L, (text_width, text_height), 128) # 128表示透明度0为完全透明255为不透明# 创建一个带有透明度的文本图像
watermark_text_image Image.new(RGBA, (text_width, text_height))
watermark_draw ImageDraw.Draw(watermark_text_image)
watermark_draw.text((0, 0), watermark_text, fill(255, 255, 255, 128), fontfont) # 填充颜色和透明度# 计算水印位置居中
main_width, main_height main_image.size
x (main_width - text_width) // 2
y (main_height - text_height) // 2# 在主图像上添加文本透明水印
main_image.paste(watermark_text_image, (x, y), watermark_text_image)# 保存带有文本透明水印的图像
watermarked_outfile f _watermarked. e
main_image.save(watermarked_outfile)print(f带有文本透明水印的图像已保存为 {watermarked_outfile})2.8生成验证码图片
import random
from PIL import Image, ImageDraw, ImageFont# 创建验证码图像
width, height 200, 100
background_color (255, 255, 255) # 白色背景
image Image.new(RGB, (width, height), background_color)# 创建一个ImageDraw对象
draw ImageDraw.Draw(image)# 创建一个字体对象
font ImageFont.truetype(arial.ttf, 48) # 替换为合适的字体路径和大小# 随机生成4个字符作为验证码
characters ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
code .join(random.choice(characters) for _ in range(4))# 计算每个字符的位置居中
text_width, text_height draw.textsize(code, font)
x (width - text_width) // 2
y (height - text_height) // 2# 在图像上绘制验证码
draw.text((x, y), code, fill(0, 0, 0), fontfont) # 使用黑色填充# 添加干扰线
for _ in range(5):x1 random.randint(0, width)y1 random.randint(0, height)x2 random.randint(0, width)y2 random.randint(0, height)draw.line((x1, y1, x2, y2), fill(0, 0, 0), width2)# 保存验证码图片
captcha_outfile captcha.png
image.save(captcha_outfile)print(f验证码图片已保存为 {captcha_outfile}验证码为: {code})