Python 2.7 为什么';由PIL draw.text()图像生成的数组是否在Matplotlib中正确显示?
我想理解为什么当我将PIL图像Python 2.7 为什么';由PIL draw.text()图像生成的数组是否在Matplotlib中正确显示?,python-2.7,matplotlib,python-imaging-library,Python 2.7,Matplotlib,Python Imaging Library,我想理解为什么当我将PIL图像imageRGB转换为浮点数组arrayRGB\u f并使用matplotlib的imshow()而不使用cmap时,它看起来要么是黑色的,要么是奇怪的和不可读的,即使PIL的imageRGB.show()看起来很好,而且每个单独的r,g,用cmap='gray'显示的b通道看起来也不错 我有解决办法,但我不明白为什么会这样 matplotlib.\uu版本\uuu返回'2.0.2',我正在使用MacOS和Anaconda安装 有关将ttf渲染转换为1bit的更多信
imageRGB
转换为浮点数组arrayRGB\u f
并使用matplotlib的imshow()
而不使用cmap时,它看起来要么是黑色的,要么是奇怪的和不可读的,即使PIL的imageRGB.show()
看起来很好,而且每个单独的r,g,用cmap='gray'
显示的b通道看起来也不错
我有解决办法,但我不明白为什么会这样
matplotlib.\uu版本\uuu
返回'2.0.2'
,我正在使用MacOS和Anaconda安装
有关将ttf渲染转换为1bit的更多信息,请参见此
供参考打印报表的输出为:
float64 (41, 101, 3)
int64 (41, 101, 3)
int64 (41, 101)
int64 (41, 101)
imageRGB.show()
plt.imshow()
imageRGB.show()
plt.imshow()
imageRGB.show()
plt.imshow()
从PIL导入图像、ImageDraw、ImageFont
将numpy作为np导入
将matplotlib.pyplot作为plt导入
#fontname='Arial Unicode.ttf'
fontname='默认'
如果fontname==“默认值”:
font=ImageFont.load\u default()
其他:
font=ImageFont.truetype(fontname,12)
string=“Hello”+fontname[:6]
ww,hh=101,41
阈值=80#https://stackoverflow.com/a/47546095/3904031
imageRGB=Image.new('RGB',(ww,hh))
draw=ImageDraw.draw(imageRGB)
image8bit=draw.text((10,12),string,font=font,
填充=(255、255、255、255))#R、G、Bα
image8bit=imageRGB.convert(“L”)
image1bit=image8bit.point(如果x
我找不到理想的副本,所以我会发布答案
作为@ImportanceOfBeingErnest,当.imshow()
给定nxmx3
或nxmx4
数组时,它需要一个介于0.0和1.0之间的规范化数组
最好的方法是:
arrayRGB_f = arrayRGB.astype(float)/255.
尽管这似乎也能奏效:
arrayRGB_f = arrayRGB.astype(float)
arrayRGB_f = arrayRGB_f / arrayRGB_f.max()
有关详细讨论,请参阅和。使用
a=arrayRGB\u f/255。
因为浮点数组应在0到0之间的范围内。和1。使用建议的解决方案。尝试a=arrayRGB\u f/255时是否得到完全相同的输出。?(请注意,我在这里谈论的是图像,它们是3D rgb阵列,非标准化的2D数据阵列当然可以正常工作)@ImportanceOfBeingErnest噢,天哪,没错!当它是nxmx3
Matplotlib时,它需要标准化的值。我通常在看nxm
的彩虹图。考虑张贴答案吗?如果没有人的话,我以后会的。这里看起来很棒!不,这个问题已经被问过一百次了。如果您愿意,您可以找到相应的副本并将其关闭。@ImportanceOfBeingErnest现在是05:40 AM
在这里,我可以删除它然后睡觉。@ImportanceOfBeingErnest这看起来怎么样(我找不到一个好的副本)呢?或者?@ImportanceOfBeingErnest在这种情况下,这个答案实际上是最好的,从字面上看是正确的“解决了我的问题“所以我要把它留在这里。这两个问题的答案都包含解决方案,但需要一段时间才能找到,因为它们包含了更多的信息。这正是我需要看到的,而你的评论现在引导人们进行更深入的解释。我也会把这些加回到答案中。非常感谢您的时间和帮助!
font = ImageFont.truetype(fontname, 20)
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import matplotlib.pyplot as plt
# fontname = 'Arial Unicode.ttf'
fontname = 'default'
if fontname == 'default':
font = ImageFont.load_default()
else:
font = ImageFont.truetype(fontname, 12)
string = "Hello " + fontname[:6]
ww, hh = 101, 41
threshold = 80 # https://stackoverflow.com/a/47546095/3904031
imageRGB = Image.new('RGB', (ww, hh))
draw = ImageDraw.Draw(imageRGB)
image8bit = draw.text((10, 12), string, font=font,
fill=(255, 255, 255, 255)) # R, G, B alpha
image8bit = imageRGB.convert("L")
image1bit = image8bit.point(lambda x: 0 if x < threshold else 1, mode='1') # https://stackoverflow.com/a/47546095/3904031
arrayRGB = np.array(list(imageRGB.getdata())).reshape(hh, ww, 3)
arrayRGB_f = arrayRGB.astype(float)
array8bit = np.array(list(image8bit.getdata())).reshape(hh, ww)
array1bit = np.array(list(image1bit.getdata())).reshape(hh, ww)
for a in (arrayRGB_f, arrayRGB, array8bit, array1bit):
print a.dtype, a.shape
imageRGB.show()
if True:
plt.figure()
a = arrayRGB_f
plt.subplot(2, 2, 1)
plt.imshow(a) # , interpolation='nearest', cmap='gray',
for i in range(3):
plt.subplot(2, 2, 2+i)
plt.imshow(a[:, :, i], cmap='gray')
plt.suptitle('arrayRGB_f, fontname = ' + fontname)
plt.show()
arrayRGB_f = arrayRGB.astype(float)/255.
arrayRGB_f = arrayRGB.astype(float)
arrayRGB_f = arrayRGB_f / arrayRGB_f.max()