【问题标题】:How to show image in grayscale如何以灰度显示图像
【发布时间】:2022-11-18 16:54:28
【问题描述】:

由于某种原因,这是行不通的。

我可能在某个地方犯了一个愚蠢的错误。 请帮忙

# importing modules
import urllib.request
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from PIL import Image

#dowload 蒙娜丽莎图片

urllib.request.urlretrieve(
  'https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/1024px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg',
   "Mona_Lisa.png")

#打开文件

img = Image.open("/content/Mona_Lisa.png")

#convert 从 rgba 到 rgb

rgb_image = img.convert('RGB')
rgb_image_rgb = np.array(rgb_image)

#显示图像

plt.imshow(rgb_image_rgb, cmap = cm.Greys_r)

【问题讨论】:

  • 你检查过 ImageOps.grayscale() 了吗?这也来自枕头图书馆

标签: python image matplotlib


【解决方案1】:

你试过这个答案吗?

How can I convert an RGB image into grayscale in Python?

from PIL import Image
img = Image.open('image.png').convert('L')
img.save('greyscale.png')

【讨论】:

    【解决方案2】:

    您可以使用 PIL.Image.convert 将图像转换为灰度:

    img = Image.open("/content/Mona_Lisa.png").convert("L")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      相关资源
      最近更新 更多