博客
关于我
python PIL模框使用
阅读量:804 次
发布时间:2023-03-06

本文共 579 字,大约阅读时间需要 1 分钟。

1、打开并展示图片

在Python中,我们可以使用PIL库来打开并显示图片文件。以下是实现方法:

from PIL import Imageimg = Image.open('/Users/jjw/Desktop/后端项目/py-torch-yolov3/assets/giraffe.png')img.show()

效果

运行上述代码后,会打开指定路径的图片文件并在屏幕上展示图片内容。

2、将图片格式转换为RGB

有时我们需要将图片格式从一种格式转换为另一种格式。PIL库提供了多种转换方法,其中`convert`是最常用的。以下是转换RGB格式的实现:

from PIL import Imageimg = Image.open('/Users/jjw/Desktop/后端项目/py-torch-yolov3/assets/giraffe.png')print('转换前:', img)  print('转换后:', img.convert('RGB'))  img.show()  newImg = img.convert('RGB')  newImg.show()  print('新图片:', newImg)

注意事项

需要注意的是,`convert`方法会返回一个新的图片对象,而不会修改原图片对象。因此,在操作完成后,建议将新图片单独显示。

转载地址:http://imafk.baihongyu.com/

你可能感兴趣的文章
python | tiler,一个不可思议的 图像切片重组 Python 库!
查看>>
python | tinydb,一个非常厉害的 关于数据库的 Python 库!
查看>>
python | tox,一个超强的 自动化测试工具 Python 库!
查看>>
python | ttkbootstrap,一个神奇的 Python 库!
查看>>
python | unoconv,一个超厉害的 Python 库!
查看>>
python | urllib3,一个超强的 Python 库!
查看>>
python | webassets,一个超强的 Python 库!
查看>>
python | werkzeug,一个不可思议的 Python 库!
查看>>
python | xlsxwriter,一个实用的 Python 库!
查看>>
python | xlwings,一个非常实用的 Excel 相关的 Python 库!
查看>>
python | xmltodict,一个非常厉害的 关于XML数据 Python 库!
查看>>
python | xonsh,一个超酷的 Python 库!
查看>>
python | yagmail,一个实用的 Python 库!
查看>>
python | 一文掌握Python的上下文管理器和with语句
查看>>
python | 一文看懂Python闭包机制与变量作用域规则
查看>>
python读取含中文的json
查看>>
python | 如何用Python锁避免并发错误?
查看>>
python | 提升代码迭代速度的Python重载方法
查看>>
python | 深入理解Python并发编程中的GIL限制与解决方案
查看>>
Python | 爬虫实战——亚马逊搜索页监控(附详细源码)
查看>>