簡體   English   中英

我如何使用python將圖像轉換為灰度?

[英]how do i convert image into gray scale using python?

我在文件夾中有 5 張圖片。 我想將所有圖像轉換為灰度。

import glob
colorIm = []
for filename in glob.glob('/content/drive/My Drive/Colab Notebooks/Asplab/Cifar/*.png'):
  print(filename)
  img = Image.open(filename)
  colorIm.append(img)
  greyIm=colorIm.convert('L')

AttributeError: 'list' 對象沒有屬性 'convert'

您需要轉換列表中的每個圖像:

import glob

colorIm = []
for filename in glob.glob('/content/drive/My Drive/Colab Notebooks/Asplab/Cifar/*.png'):
  print(filename)
  img = Image.open(filename)
  colorIm.append(img)

greyIm = [img.convert('L') for img in colorIm]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM