简体   繁体   English

如果我在python中具有强度矩阵,则可以打印灰度图像

[英]to print a gray scale image if i have the matrix of intensities in python

a=[[22,10,21,22,15,16],
   [24,21,13,20,14,17],
   [23,17,38,23,17,16],
   [25,25,22,14,15,21],
   [27,22,12,11,21,20],
   [24,21,10,12,22,23]
   ]

Consider this list of lists as a two dimensional array and can anyone help me how to print this matrix in the form of an image in python. 将此列表列表视为二维数组,谁能帮助我如何以python中的图像形式打印此矩阵。

You want to use a library such as the Pillow (a fork of the Python Image Library). 您想使用诸如Pillow (Python图像库的一个分支)之类的库。

Have a look at the Image.frombuffer function which allows you to build a image from raw pixel data. 看一下Image.frombuffer函数,它使您可以从原始像素数据构建图像。

You could try looking at pygame. 您可以尝试查看pygame。 It is a really handy module that comes in use all the time. 这是一个非常方便的模块,一直在使用。

Here is a sample code that works: 这是一个有效的示例代码:

import pygame

a=[[22,10,21,22,15,16],
   [24,21,13,20,14,17],
   [23,17,38,23,17,16],
   [25,25,22,14,15,21],
   [27,22,12,11,21,20],
   [24,21,10,12,22,23]]

BLOCKSIZE = 16
WIDTH = len(a[0]) * BLOCKSIZE
HEIGHT = len(a) * BLOCKSIZE
DISPLAY = (WIDTH, HEIGHT)

WHITE = (255.0,255.0,255.0)

pygame.init()
screen = pygame.display.set_mode(DISPLAY,0,32)

x = y = 0

for row in a:
    for item in row:
        screen.fill((WHITE[0]*item/50,WHITE[1]*item/50,WHITE[2]*item/50),(x,y,BLOCKSIZE,BLOCKSIZE))
        y += BLOCKSIZE
    x += BLOCKSIZE
    y = 0

pygame.display.update()

while 1:
    pass

What this code does is it looks at the value then converts it to a grey color corresponding to its value (0 is black and 50 is white). 该代码的作用是查看该值,然后将其转换为对应于其值的灰色(0为黑色,50为白色)。 It then draws it on the screen. 然后将其绘制在屏幕上。

AGAIN, read up on pygame. 再次,请阅读pygame。 It is REALLY useful 这真的很有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM