如下所示:
# coding=utf-8import globimport osfrom PIL import Imagedef rotate_270(imgae):"""將圖片旋轉270度"""# 讀取圖像im = Image.open(imgae)# im.show()# 指定逆時針旋轉的角度im_rotate = im.rotate(270)# im_rotate.show()return im_rotatedef flip_horizontal(image):"""將圖片水平翻轉"""im = Image.open(image)# im.show()im_fh = im.transpose(Image.FLIP_LEFT_RIGHT)# im_fh.show()return im_fhdef createFile(path):isExists = os.path.exists(path)# 判斷結果if not isExists:# 如果不存在則創建目錄# 創建目錄操作函數os.makedirs(path)return Trueelse:# 如果目錄存在則不創建,并提示目錄已存在print('%s 目錄已存在' % path)return Falsedef 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("http://"):]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實現旋轉和水平翻轉的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答