Pytoch之torchvision.transforms图像变换实例

yipeiwu_com5年前Python基础

transforms.CenterCrop(size)

将给定的PIL.Image进行中心切割,得到给定的size,size可以是tuple,(target_height, target_width)。size也可以是一个Integer,在这种情况下,切出来的图片的形状是正方形。

size可以为int,也可以为float

#定义中心切割
centerCrop = transforms.CenterCrop((img.size[0]/2,img.size[1]/2))
imgccrop = centerCrop(img)
 
transforms.RandomCrop(size,padding=0)

切割中心点的位置随机选取。size可以是tuple也可以是Integer。

但是如果是Tuple,只能是int型的不能是float

#要求目标size必须为整数
randomCrop1 = transforms.RandomCrop((int(img.size[0]/2),int(img.size[1]/2))) #padding默认为False
randomCrop2 = transforms.RandomCrop((int(img.size[0]/2),int(img.size[1]/2)),padding=10)
 
 
imgrcrop1 = randomCrop1(img)
imgrcrop2 = randomCrop2(img)
 

transforms.RandomHorizontalFlip

随机水平翻转给定的PIL.Image,概率为0.5。即:一半的概率翻转,一半的概率不翻转。

#随机将图片旋转180°
randomFlip = transforms.RandomHorizontalFlip()
 
imgf = randomFlip(img)

transforms.RandomSizedCrop(size, interpolation=2)

先将给定的PIL.Image随机切,然后再resize成给定的size大小。

size只能是Int,不能是float或tuple

#参数需为一个整数,不能是float或者tuple
randomcut = transforms.RandomResizedCrop(100)
imgc = randomcut(img)

transforms.Pad(padding, fill=0)

将给定的PIL.Image的所有边用给定的pad value填充。 padding:要填充多少像素fill

可以用于给图片加边框2333

#加边界框
pad = transforms.Pad(padding=10,fill=0)
pad_img = pad(img)
pad_img.save("block.jpg")

以上这篇Pytoch之torchvision.transforms图像变换实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

在Qt中正确的设置窗体的背景图片的几种方法总结

Qt中正确的设置窗体的背景图片的方法大致有两种,下面将逐个讲解: 一. 利用styleSheet设置窗体的背景图片 使用stylesheet设置窗体的背景图片的时候,可以直接按照下图的操...

对Python使用mfcc的两种方式详解

对Python使用mfcc的两种方式详解

1、Librosa import librosa filepath = "/Users/birenjianmo/Desktop/learn/librosa/mp3/in.wav"...

Python编程pygal绘图实例之XY线

Python编程pygal绘图实例之XY线

安装pygal,可参阅:pip和pygal的安装实例教程 基本XY线: import pygal from math import cos """ XY线是将各个点用直线连接起来的...

Python 识别12306图片验证码物品的实现示例

Python 识别12306图片验证码物品的实现示例

1、PIL介绍以及图片分割 Python 3 安装:  pip3 install Pillow 1.1 image 模块 Image模块是在Python PIL图像处理中常...

基于python log取对数详解

log()方法返回x的自然对数,对于x>0。 语法 以下是log()方法的语法: import math math.log( x ) 注意:此函数是无法直接访问的,所以我...