参数说明:
mode:图像的模式,定义了图像中像素的数据类型和排列方式。常见的模式包括 ‘L’(灰度模式,每个像素为8位)、‘RGB’(红绿蓝模式,每个像素为24位)、‘RGBA’(红绿蓝加透明度模式,每个像素为32位)等。
size:图像的大小,以像素为单位,通常是一个二元组 (width, height)。
color:图像的颜色。如果不指定颜色,图像将填充为零(通常为黑色),参数值支持(R,G,B)三元组数字格式、颜色的十六进制值以及颜色英文单词。
from PIL import Image
# 创建一个 300x300 像素的红色 RGB 图像
# color="#ff0000" 表示红色
img = Image.new('RGB', (300, 300), 'red')
# 显示图像
img.show()
2.查看img对象属性:
# @Author : 小红牛
# 微信公众号:WdPython
from PIL import Image
# 打开图像文件
img = Image.open('example.png')
# 1.获取图像的格式
format = img.format
print(f'Format: {format}')
# Format: PNG
# 2.获取图像的尺寸(宽度和高度)
width, height = img.size
print(f'Width: {width}, Height: {height}')
# 输出 Width: 142, Height: 160