084、Python 使用图像库读取图片文件

1、首先先安装图像库,这里使用Pillow库。

可以使用编辑器的终端安装(我这里使用了镜像):

pip install Pillow --index-url https://pypi.tuna.tsinghua.edu.cn/simple

假如无法安装,更新下pip。

2、通过引入Pillow库,并调用图像库中的Image.open来读取图片,并进行其它处理。如:

"""
example084 - 使用图像库读取图片文件

Author: 不在同一频道上的呆子
Date: 2024/7/20
"""
from PIL import Image

# 打开图片文件
with Image.open('Resources/pic1.png') as img:
    # 显示图片(注意:在某些环境中,如服务器或没有图形界面的环境中,这行代码可能不会工作)
    img.show()

    # 对图片进行一些处理
    # 这里将图片转换为灰度图
    gray_img = img.convert('L')

    # 保存处理后的图片
    gray_img.save('Resources/gray_image.png')

猜你喜欢

转载自blog.csdn.net/mr_five55/article/details/140569824