当前位置:首页 >> 编程语言 >> 【Python学习】 - PIL - 各种图像操作,热门手机

【Python学习】 - PIL - 各种图像操作,热门手机

0evadmin 编程语言 2
文件名:【Python学习】 - PIL - 各种图像操作,热门手机 【Python学习】 - PIL - 各种图像操作

官方文档:http://effbot.org/imagingbook/image.htm

1.打开图像并显示(注意这样show的话,会生成本地文件的): img=Image.open('1.jpg')img.show()

 

2.转8位灰度图 greyimg=img.convert('L')greyimg.show()

 

3.获取图片某一像素点的 (R,G,B)值 from PIL import Image  imagepath='1.jpg'  img = Image.open(imagepath)  if img.mode not in ('L', 'RGB'):  img = img.convert('RGB')  r, g, b = img.getpixel((10, 10))   4.从图像中随机截取一部分

crop() : 从图像中提取出某个矩形大小的图像。它接收一个四元素的元组作为参数,各元素为(left, upper, right, lower),坐标系统的原点(0, 0)是左上角。

leftupx=random.randint(0,img.size[0]-64-1)leftupy= random.randint(0,img.size[1]-128-1)img1=img.crop((leftupx,leftupy,leftupx+64,leftupy+128)) 5.保存图像 im.save("2.jpg")   6.PIL中的Image和numpy中的数组array相互转换

 

image转换成array img=np.array(image) array转换成image

注意img必须强转成uint8类型!如果是uint16的矩阵而不转为uint8的话,Image.fromarray这句会报错

from PIL import Imageim_arr = Image.fromarray(np.uint8(img)) 7.修改图像大小

im.resize(size, filter) ⇒ image

返回图像的调整大小的副本。size参数以像素为单位给出请求的大小,作为2元组:(宽、高)。

The filter argument can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), BICUBIC (cubic spline interpolation in a 4x4 environment), or ANTIALIAS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.

滤波器参数可以是最接近的(使用最近邻)、双线性(在2x2环境中进行线性插值)、双三次样条(在4x4环境中进行三次样条插值)或反锯齿(高质量的下采样滤波器)。如果省略,或者图像模式为“1”或“P”,则将其设置为最接近的模式。

Note that the bilinear and bicubic filters in the current version of PIL are not well-suited for large downsampling ratios (e.g. when creating thumbnails). You should use ANTIALIAS unless speed is much more important than quality.

请注意,当前版本的公益诉讼的双线性和双三次滤波器不适用于大的下采样率(例如,创建缩略图时)。除非速度比质量更重要,否则应该使用反锯齿。

(总之一般情况下第二个参数使用默认就好)

比如要将图片修改成64*64的im = im.resize(64,64) 8.复制图片 im = Image.open('1.jpg')im2 = im.copy() 9.添加水印,叠加函数,拼接图像

基于keras中的PIL中的paste()函数可以给图片添加水印,也可以叠加函数,拼接图像,做到多张图片结果的可视化。

https://blog.csdn.net/s411081123/article/details/86255688

10.更多操作

https://www.cnblogs.com/meitian/p/3699223.html

 

部分内容选自:传送门

 

https://www.sohu.com/a/328328613_571478

协助本站SEO优化一下,谢谢!
关键词不能为空
同类推荐
«    2025年12月    »
1234567
891011121314
15161718192021
22232425262728
293031
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接