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 用lambda函数替换for循环的方法

场景如下: 现在有一个dataframe,其中一列为score,值从0-100, df: score 98 88 37 68 86 33 现在需要增加一列level,给这些分数分类,90...

Python实现正整数分解质因数操作示例

本文实例讲述了Python实现正整数分解质因数操作。分享给大家供大家参考,具体如下: 遇到一个Python编程练习题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5...

Python+tkinter使用40行代码实现计算器功能

Python+tkinter使用40行代码实现计算器功能

本文实例为大家分享了40行Python代码实现计算器功能,供大家参考,具体内容如下 偶尔用脚本写点东西也是不错的。 效果图 代码 from tkinter import *...

Python基于socket模块实现UDP通信功能示例

Python基于socket模块实现UDP通信功能示例

本文实例讲述了Python基于socket模块实现UDP通信功能。分享给大家供大家参考,具体如下: 一 代码 1、接收端 import socket #使用IPV4协议,使用UDP协...

python中如何实现将数据分成训练集与测试集的方法

接下来,直接给出大家响应的代码,并对每一行进行标注,希望能够帮到大家。 需要用到的是库是。numpy 、sklearn。 #导入相应的库(对数据库进行切分需要用到的库是sklearn...