python实现旋转和水平翻转的方法

yipeiwu_com5年前Python基础

如下所示:

# coding=utf-8
import glob
import os

from PIL import Image


def rotate_270(imgae):
"""
将图片旋转270度
"""
# 读取图像
im = Image.open(imgae)
# im.show()
# 指定逆时针旋转的角度
im_rotate = im.rotate(270)
# im_rotate.show()
return im_rotate


def flip_horizontal(image):
"""
将图片水平翻转
"""
im = Image.open(image)
# im.show()
im_fh = im.transpose(Image.FLIP_LEFT_RIGHT)
# im_fh.show()
return im_fh


def createFile(path):
isExists = os.path.exists(path)
# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
os.makedirs(path)
return True
else:
# 如果目录存在则不创建,并提示目录已存在
print('%s 目录已存在' % path)
return False


def main():
path = 'D:/VideoPhotos/hongshi/'
createFile('D:/VideoPhotos/hongshi_rotate')
createFile('D:/VideoPhotos/hongshi_flip_horizontal')

dirs = os.listdir(path)
for dir in dirs:
# print(dir)
createFile('D:/VideoPhotos/hongshi_rotate/' + dir)
createFile('D:/VideoPhotos/hongshi_flip_horizontal/' + dir)

images = glob.glob(path + dir + r"\*.jpg")
for image in images:
image_name = image[image.find("\\"):]
print(image_name)
rotate_270(image).save('D:/VideoPhotos/hongshi_rotate/' + dir +
image_name)
flip_horizontal(image).save(
'D:/VideoPhotos/hongshi_flip_horizontal/' + dir + image_name)


if __name__ == '__main__':
main()

以上这篇python实现旋转和水平翻转的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中append实例用法总结

append()函数 描述:在列表ls最后(末尾)添加一个元素object 语法:ls.append(object) -> None 无返回值 例: a=[1,2,3] a....

Python编程实现粒子群算法(PSO)详解

Python编程实现粒子群算法(PSO)详解

1 原理 粒子群算法是群智能一种,是基于对鸟群觅食行为的研究和模拟而来的。假设在鸟群觅食范围,只在一个地方有食物,所有鸟儿看不到食物(不知道食物的具体位置),但是能闻到食物的味道(能知道...

python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法

python 利用浏览器 Cookie 模拟登录的用户访问知乎的方法

首先在火狐浏览器上登录知乎,然后使用火狐浏览器插件 Httpfox 获取 GET 请求的Cookie,这里注意使用状态值为 200(获取成功)的某次GET. 将 Cookies 复制出...

python字符串查找函数的用法详解

python字符串查找函数的用法详解

python字符串查找函数的使用 打开Python开发工具IDLE,新建‘findstr.py'文件,并写代码如下: s ='/ab/bx,.s' print (s.find('/x...

python处理两种分隔符的数据集方法

python处理两种分隔符的数据集方法

在做机器学习的时候,遇到这样一个数据集... 一共399行10列, 1-9列是用不定长度的空格分割, 第9-10列之间用'\t'分割, 前九列都是数值类型,其中第三列有若干个'?...