這篇文章主要介紹了利用python怎么實現一個旋轉和水平翻轉功能,此處通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考價值,需要的朋友可以參考下:
Python是一種編程語言,內置了許多有效的工具,Python幾乎無所不能,該語言通俗易懂、容易入門、功能強大,在許多領域中都有廣泛的應用,例如最熱門的大數據分析,人工智能,Web開發等。
如下所示:
# 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怎么實現一個旋轉和水平翻轉功能的內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。