0
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.

user3320033
  • 245
  • 3
  • 6
  • 16
  • This must be related to what you need http://stackoverflow.com/questions/3823752/display-image-as-grayscale-using-matplotlib – kon psych Jun 21 '14 at 04:53

2 Answers2

1

You want to use a library such as the Pillow (a fork of the Python Image Library).

Have a look at the Image.frombuffer function which allows you to build a image from raw pixel data.

Andrew Wilkinson
  • 10,682
  • 3
  • 35
  • 38
0

You could try looking at 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). It then draws it on the screen.

AGAIN, read up on pygame. It is REALLY useful

sshashank124
  • 31,495
  • 9
  • 67
  • 76