首页 > 解决方案 > imshow 不显示从 ndarray 提取的图像。没有错误

问题描述

我需要实现一个生活游戏。我和 Pyzo 一起工作

Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) on Windows (64 bits).
This is the Pyzo interpreter with integrated event loop for PYQT5.

为此,我希望地图成为世界地图而不是正方形。所以我得到一个单色图像。我打开它,我把它转换成数组。但我无法显示它。没有错误,我在代码中设置的日志消息似乎显示一切正常,但是没有显示带有图像的窗口。我也尝试使用 RGBA 图像但没有更多成功我在重新启动 PC 后也尝试过。

该图像是用位图单色绘制保存的 bmp。我加入了它感谢您提供解决此问题的任何想法。

这是我的代码中的消息

format: BMP   mode: 1   palette: None
The image is 993 x 579
ok
[[1 1 1 ... 1 1 1]
 [1 0 0 ... 0 0 1]
 [1 0 0 ... 0 0 1]
 ...
 [1 0 0 ... 0 0 1]
 [1 0 0 ... 0 0 1]
[1 1 1 ... 1 1 1]]
0
(579, 993)
<class 'numpy.ndarray'>
<class 'numpy.uint8'>
should be displayed 

这是代码

def Load_image(filename):
#    try:
    img = Image.open(filename) 
    print ('format: %s   mode: %s   palette: %s' % (img.format,img.mode,img.palette))
    print ('The image is %d x %d' % img.size)
    return img
 #    except:
    print ("Unable to open %s" % filename)
    return -1

def Img2Array(img):
    # BytePerPixel = {'1': 1, 'RGB': 2, 'RGBA' : 4 }# 1: B&W, 3: RGB, 4: RGBA (A=Alpha : transparency)
    # result = numpy.array(img.getdata(), numpy.uint8).reshape(img.size[1], img.size[0], BytePerPixel.get(img.mode))
    result = numpy.array(img,dtype='int8')
    print (result)
    print(result[200,200])
    print(result.shape)
    print (type(result))
    print (type(result[200,200]))

    test = plt.imshow(result)
    print ("should be displayed")
    return result

fichiercarte = "C:\\Users\\mld\\Desktop\\python\\world-map1.bmp"
carte = Load_image(fichiercarte)
if (carte != None): 
   print ("ok")
ndArrayCarteDuMonde = Img2Array (carte)

单色世界地图

标签: pythonimagenumpymatplotlibimshow

解决方案


推荐阅读