博客
关于我
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 rdflib可传递查询
查看>>
python redis 集群_python 搭建redis集群
查看>>
python redis连接,在Python中使用Redis连接池的正确方法
查看>>
python regex_Python RegEx
查看>>
python requests post 中文结果请求得到unicode
查看>>
Python Requests接口自动化测试实战
查看>>
Python requests模块
查看>>
python request与grequests该如何选择
查看>>
python request模块
查看>>
Python requirements.txt的使用方法
查看>>
Python REST(Web 服务)框架的推荐?
查看>>
Python rsa 加密
查看>>
Python RSA操作
查看>>
Python轻松实现统计学中重要的相关性分析
查看>>
Python scrapy 常见问题及解决 【遇到的坑】
查看>>
Python Seborn热图数据的动态更新
查看>>
Python Seborn绘制空白直方图
查看>>
Python Selenium - 获取href值
查看>>
Python Selenium实现自动化测试及Chrome驱动使用!
查看>>
Python Selenium搭建UI自动化测试框架
查看>>