Python 如何将单个像素位置作为plt.imshow的参数?
这段代码打印紫色,而实际上图像是黑色的,是单个像素。我将默认图像像素值转换为RGB pallete。代码如下所示:Python 如何将单个像素位置作为plt.imshow的参数?,python,matplotlib,image-processing,Python,Matplotlib,Image Processing,这段代码打印紫色,而实际上图像是黑色的,是单个像素。我将默认图像像素值转换为RGB pallete。代码如下所示: from PIL import Image import numpy as np import matplotlib.pyplot as plt image = Image.open(r'C:\Users\oneblackpixel.png') image2 = np.asarray(image) fig = plt.figure() imgplot = plt.imshow(
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image = Image.open(r'C:\Users\oneblackpixel.png')
image2 = np.asarray(image)
fig = plt.figure()
imgplot = plt.imshow([image2[0][0]])
plt.show()
我将默认图像像素值转换为RGB托盘。代码如下所示:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image = Image.open(r'C:\Users\oneblackpixel.png')
image2 = np.asarray(image)
fig = plt.figure()
imgplot = plt.imshow([image2[0][0]])
plt.show()
以下是代码更改: 从PIL导入图像 将numpy作为np导入 将matplotlib.pyplot作为plt导入 image=image.openr'C:\Users\oneblackpixel.png' 图像2=np.asarrayimage 图=plt.figure 这里需要第二个括号: imgplot=plt.imshow[[image2[0][0]] 节目
图像以三维数组的形式读取。第一和第二维是图像的x轴和y轴,第三维是像素的颜色信息。在您的情况下,如果.png文件中为黑色,则为[0,0,0255]。数组中的值为[R,G,B,Alpha]按顺序。如果仅使用1个括号,imshow将从一个像素绘制4个值。以下是代码更改: 从PIL导入图像 将numpy作为np导入 将matplotlib.pyplot作为plt导入 image=image.openr'C:\Users\oneblackpixel.png' 图像2=np.asarrayimage 图=plt.figure 这里需要第二个括号: imgplot=plt.imshow[[image2[0][0]] 节目
图像以三维数组的形式读取。第一和第二维是图像的x轴和y轴,第三维是像素的颜色信息。在您的情况下,如果.png文件中为黑色,则为[0,0,0255]。数组中的值为[R,G,B,Alpha]按顺序。如果仅使用1个括号,imshow将从一个像素打印4个值。是否尝试将图像作为matplot图像读取?您的操作如下:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image = Image.open(r'black_white_image.png')
# image2 = np.asarray(image)
imgRgb = image.convert('RGB')
fig = plt.figure()
plt.imshow(imgRgb)
plt.show()
这通常是推荐的。
此外,这一行:
import matplotlib.image as img
image = img.imread("oneblackpixel.png")
将图像转换为二维数组,这是不好的,您将需要3个
这将解决这个问题:
imgplot = plt.imshow([image2[0][0]])
我也做了一些清理,这个代码应该对你有用
image2 = [[image[0][0]]]
这有帮助吗?您是否尝试将图像作为matplot图像读取?您的操作方式如下:
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
image = Image.open(r'black_white_image.png')
# image2 = np.asarray(image)
imgRgb = image.convert('RGB')
fig = plt.figure()
plt.imshow(imgRgb)
plt.show()
这通常是推荐的。
此外,这一行:
import matplotlib.image as img
image = img.imread("oneblackpixel.png")
将图像转换为二维数组,这是不好的,您将需要3个
这将解决这个问题:
imgplot = plt.imshow([image2[0][0]])
我也做了一些清理,这个代码应该对你有用
image2 = [[image[0][0]]]
那有用吗