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

yipeiwu_com6年前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设计】。

相关文章

PyCharm搭建Spark开发环境的实现步骤

PyCharm搭建Spark开发环境的实现步骤

1.安装好JDK 下载并安装好jdk-12.0.1_windows-x64_bin.exe,配置环境变量: 新建系统变量JAVA_HOME,值为Java安装路径 新建系统变量...

浅谈Python中的闭包

Python中的闭包的概念, 在我看来, 就相当于在某个函数中又定义了一个或多个函数, 内层函数定义了具体的实现方式, 而外层返回的就是这个实现方式, 但并没有执行, 除非外层函数调用的...

Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作示例

本文实例讲述了Python实现输入二叉树的先序和中序遍历,再输出后序遍历操作。分享给大家供大家参考,具体如下: 实现一个功能:     输入:一颗二叉树的先...

浅谈pyhton学习中出现的各种问题(新手必看)

目前比较杂乱无章,后续还会有一些添加补充 1、标识符 (1)标识符是区分大小写的。 (2)标示符以字母或下划线开头,可包括字母,下划线和数字。 (3)以下划线开头的标识符是有特殊意义的。...

python中将zip压缩包转为gz.tar的方法

由于同事电脑上没有直接可以压缩gz.tar格式的压缩软件,而工作中这个又时常需要将zip文件转换为gz.tar格式,所以常常将压缩为zip格式的文件发给我来重新压缩成gz.tar格式发给...