• 直接用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")        ## 转换完后的保存路径

相关文章:

  • 2022-12-23
  • 2021-12-10
  • 2021-04-21
  • 2022-02-14
  • 2022-01-05
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-05-23
  • 2022-12-23
相关资源
相似解决方案