我想从URL读取图像以调整大小并将其转换为灰度。我已经从stackoverflow看到了许多示例,并尝试了它们。但是,就我而言,它从未成功将图像转换为灰度。我不确定这里出了什么问题。这就是我尝试过的。
import matplotlib.pyplot as plt
from skimage.transform import resize
import numpy as np
from skimage import io, color
# try 1
img1 = io.imread("https://prasadpamidi.github.io/images/image2.jpg", as_grey=True)
img1 = (img1 - 255.0) / 255
img1 = resize(img1, (32, 32))
# try 2
img1 = io.imread("https://prasadpamidi.github.io/images/image2.jpg")
img1 = img1.dot([0.07, 0.72, 0.21])
img1 = (img1 - 255.0) / 255
img1 = resize(img1, (32, 32))
# try 3
img1 = color.rgb2gray(io.imread("https://prasadpamidi.github.io/images/image2.jpg"))
img1 = (img1 - 255.0) / 255
img1 = resize(img1, (32, 32))
# print images
plt.figure(figsize=(5,5))
plt.imshow(img1)
plt.show()
结果或多或少与下面类似。所以,我想知道我错过了什么。任何建议将不胜感激。
答案 0 :(得分:2)
尝试一下此代码:
from skimage.io import imread, imshow
from skimage.transform import resize
from skimage.util import img_as_ubyte
url = "https://prasadpamidi.github.io/images/image2.jpg"
img1 = imread(url, as_gray=True)
img2 = resize(img1, (32, 32))
img3 = img_as_ubyte(img2)
imshow(img3)
输出:
我正在附上变量浏览器的屏幕快照,以向您展示变量具有正确的形状和类型。