< python PIL - 批量圖像處理 - RGB圖像生成灰度圖像 >


< python PIL - 批量圖像處理 - RGB圖像生成灰度圖像 >

  • 直接用python自帶的PIL圖像庫,將一個文件夾下所有jpg/png的RGB圖像轉換成灰度/黑白圖像
from PIL import Image 
import os.path
import glob

def convertjpg(jpgfile,outdir):
    try:
        image_file = Image.open(jpgfile) # open colour image
        image_file = image_file.convert('L') # convert image to black and white
        image_file.save(os.path.join(outdir, os.path.basename(jpgfile)))
    except Exception as e:
        print(e)

for jpgfile in glob.glob("C:/Users/62473/Desktop/RGB/*.png"):    ## 所有圖片存放路徑 png可以改成jpg
    # print(jpgfile)
    convertjpg(jpgfile,"C:/Users/62473/Desktop/gray")        ## 轉換完后的保存路徑


免責聲明!

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



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