python 文件夹中所有图像旋转5度

1, 提供的代码分别为对文件夹中所有代码旋转5度,和对单张图像旋转5度的操作. 

缺点:有可能处理的图像不是原对应的图像.造成标签错乱. 更加科学的做法如2

#coding=utf-8
#对文件夹中的所有图片进行5度的旋转操作
import PIL.Image as img
from PIL import ImageEnhance
import os

def rotationImage(filepath,destpath):
    filelist=os.listdir(filepath)  #所有文件的文件名
    total_num=len(filelist) #所有文件的个数
    print(total_num)
    for i in range(total_num): #对每张图像进行操作
        im=img.open(filepath+str(i+21)+str("_training")+".gif")
        for j in range(72):
            im_rotate=im.rotate(j*5) #每张图像都5°旋转一次
            im_rotate.save(destpath+str((j+1)*5)+str("_")+str(i+21)+str("_")+str("manual1")+'.gif')
            #im_rotate.save(destpath+str(i+21)+str("_mannual_")+str((j+1)*5)+'.gif')
        j=0

if __name__== '__main__':
    filepath='/home/qxq/Desktop/eyedata/train/label_gif/gif/'
    destpath='/home/qxq/Desktop/eyedata/train/label_gif/rotation/'
    rotationImage(filepath,destpath)

# #coding=utf-8
# import cv2
# import numpy as np
# from numpy import *
# #**************旋转操作*****逐个文件**********
# img=cv2.imread('27_training.tif')
# rows,cols,channel=img.shape
# for j in range(0, 71):
#     M=cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),((j+1)*5),1)
#     #cv2.getRotationMarix2D(point2f center,double angle,double scale)
#     #参数分别为:旋转中心,旋转角度,图像缩放因子
#     dst=cv2.warpAffine(img,M,(cols,rows))
#     #仿射变换其三个参数分别为:输入图像,变换矩阵,输出图像大小
# 
#     #print(j,(j+1)*5)
#     #保存图像
#     cv2.imwrite(str((j+1)*5)+'_27_training.tif',dst)
# 
# #**************旋转操作***************

2 更加靠谱的做法:

#coding=utf-8
import os
from PIL import Image

rootdir = r'/home/qxq/Desktop/eyedata_final/mask/original/'  # 指明被遍历的文件夹
for parent, dirnames, filenames in os.walk(rootdir):
    for filename in filenames:
        currentPath = os.path.join(parent, filename)
        im = Image.open(currentPath)
        for j in range(72):
            im_rotate = im.rotate(j * 5)  # 每张图像都10°旋转一次

            newname = r"/home/qxq/Desktop/eyedata_final/mask/r/" + str((j+1)*5) +str("_")+ filename
            im_rotate.save(newname)
        j = 0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值