python將圖片旋轉,顛倒,修改尺寸


直接上代碼,根據需求注釋選擇相應修改 

from PIL import Image
import os
import os.path

rootdir = r'G:\jianfeng\project\rubblish_det\faster_rcnn\rubbish_voc_xml\rubbish_pic_forTest\4396'  # 指明被遍歷的文件夾
for parent, dirnames, filenames in os.walk(rootdir):
    for filename in filenames:
        print('parent is :' + parent)
        print('filename is :' + filename)
        currentPath = os.path.join(parent, filename)
        print('the fulll name of the file is :' + currentPath)

        im = Image.open(currentPath)
        #進行上下顛倒
        out = im.transpose(Image.FLIP_TOP_BOTTOM)
        #進行左右顛倒
        out =out.transpose(Image.FLIP_LEFT_RIGHT)
        # 進行旋轉90
        out = im.transpose(Image.ROTATE_90)
        # 進行旋轉180
        out = im.transpose(Image.ROTATE_180)
        # 進行旋轉270
        out = im.transpose(Image.ROTATE_270)
        #將圖片重新設置尺寸
        out= out.resize((1280,720))
        newname = r"G:\jianfeng\project\rubblish_det\faster_rcnn\rubbish_voc_xml\rubbish_pic_forTest\4396_720" + '\\' +"10t"+ filename
        out.save(newname)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM