pillow.Image常用操作-图像预处理

pillow.Image常用操作如图片裁剪,旋转,缩放,翻转等
技术标签: pillow 图像预处理

在Python中import Image包格式如下:

from PIL import Image
width_rate=random.randint(1,width_rate)/100
height_rate=random.randint(1,height_rate)/100
if type(figure) is np.ndarray:
   figure=Image.fromarray(figure)
figure=figure.crop((32*width_rate/2,32*height_rate/2,32-32*width_rate/2,32- 
                    32*height_rate/2))
figure=figure.resize((32,32))
 return figure

图片裁剪

figure.crop((左,上,右,下)) 分别对应希望裁剪的尺寸的最左上右下边。

调整图像大小

figure.resize((width,height))分别对应缩放后的长宽

figure=Image.open('save_picture\cat.jpg')
figure_resize=figure.resize((200,200))
plt.figure()
plt.subplot(211)
plt.imshow(figure_resize)
plt.subplot(212)
plt.imshow(figure)
plt.show()

结果如下所示:
在这里插入图片描述

查看图片格式

figure.format

figure=Image.open('save_picture\picture.jpg')
print(figure.format)

输出结果如下:

在这里插入图片描述

图片粘贴

figure.paste(粘贴的图片,(位置))

figure=Image.open('\save_picture\cat.jpg')
print(figure.size)
figure_crop=figure.crop((60,60,260,340))
figure.paste(figure_crop,(100,100))   #图片粘贴位置的左上角坐标

效果如下:
在这里插入图片描述

图像旋转

figure_rotate=figure.rotate(90)  #旋转代码
plt.figure()
plt.subplot(211)
plt.imshow(figure_rotate)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:
在这里插入图片描述

图像水平翻转

figure_flip=figure.transpose(Image.FLIP_LEFT_RIGHT)       #翻转代码
plt.figure()
plt.subplot(211)
plt.imshow(figure_flip)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:

在这里插入图片描述

图像垂直翻转

figure_flip=figure.transpose(Image.FLIP_TOP_BOTTOM)
plt.figure()
plt.subplot(211)
plt.imshow(figure_flip)
plt.subplot(212)
plt.imshow(figure)
plt.show()

在这里插入图片描述

高斯模糊

figure_gaussianblur=figure.filter(ImageFilter.GaussianBlur)
plt.figure()
plt.subplot(211)
plt.imshow(figure_gaussianblur)
plt.subplot(212)
plt.imshow(figure)
plt.show()

在这里插入图片描述

边缘增强

from PIL import Image,ImageFilter
figure_edge_enhance=figure.filter(ImageFilter.EDGE_ENHANCE)
plt.figure()
plt.subplot(211)
plt.imshow(figure_edge_enhance)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:
在这里插入图片描述

找到边缘

figure_find_edges=figure.filter(ImageFilter.FIND_EDGES)
plt.figure()
plt.subplot(211)
plt.imshow(figure_find_edges)
plt.subplot(212)
plt.imshow(figure)
plt.show()

在这里插入图片描述

锐化

figure_sharpen=figure.filter(ImageFilter.SHARPEN)
plt.figure()
plt.subplot(211)
plt.imshow(figure_sharpen)
plt.subplot(212)
plt.imshow(figure)
plt.show()

在这里插入图片描述

平滑处理

figure_smooth=figure.filter(ImageFilter.SMOOTH)
plt.figure()
plt.subplot(211)
plt.imshow(figure_smooth)
plt.subplot(212)
plt.imshow(figure)
plt.show()

效果如下:
在这里插入图片描述

细节

figure_detail=figure.filter(ImageFilter.DETAIL)
plt.figure()
plt.subplot(211)
plt.imshow(figure_detail)
plt.subplot(212)
plt.imshow(figure)
plt.show()

在这里插入图片描述


版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42074867/article/details/90440294

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
COCO数据集的图像预处理可以分为以下几个步骤: 1. 载入图像:使用Python中的OpenCVPillow等库载入图像。 2. 调整大小:将图像调整为相同的大小,可以使用OpenCVPillow库中的resize函数。 3. 数据增强:对图像进行数据增强,如随机裁剪、旋转、翻转等操作,以增加模型的泛化能力。 4. 标准化:对图像进行标准化处理,将图像的像素值缩放到0-1范围内,以便模型更好地训练。 5. 转换为张量:将图像转换为张量,以便在深度学习模型中使用。 下面是一个使用PythonOpenCV库进行COCO数据集图像预处理的示例代码: ```python import cv2 import numpy as np def preprocess_image(image_path, input_shape): # 载入图像 image = cv2.imread(image_path) # 调整大小 image = cv2.resize(image, (input_shape[1], input_shape[0])) # 数据增强 # 随机裁剪 x1 = np.random.randint(0, image.shape[1] - input_shape[1] + 1) y1 = np.random.randint(0, image.shape[0] - input_shape[0] + 1) image = image[y1:y1 + input_shape[0], x1:x1 + input_shape[1], :] # 随机翻转 if np.random.rand() < 0.5: image = cv2.flip(image, 1) # 标准化 image = image.astype(np.float32) / 255. # 转换为张量 image = np.transpose(image, (2, 0, 1)) image = np.expand_dims(image, axis=0) return image ``` 在上面的示例代码中,我们使用了OpenCV库中的imread函数载入图像,并使用resize函数将图像调整为指定大小。然后,我们进行了随机裁剪、随机翻转等数据增强操作,并将图像标准化到0-1范围内。最后,我们将图像转换为张量,并返回处理后的图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值