1

I see lot of questions ask how to save 2D array to an image, and most of the answer is about saving image as gray scale image. But I'm trying to figure out a way to save image that can actually display each value in each cell of array. Is there a way save image that display value of array in python?

enter image description here

5
  • 1
    You could construct an image like that by using the Pillow fork of the Python Imaging Library (PIL). It contains an ImageDraw module that can draw simple 2D graphics such as lines, and also includes a function named text that can render character strings. Commented Aug 23, 2021 at 16:10
  • 1
    So what you really want to do is read the pixel values, output these values as a table, and probably saving the output table back as a new image file? Commented Aug 23, 2021 at 17:57
  • @Guang I'm not going to read pixel values of image. But I'm trying to save small 2d array or list generated from my project, into an image. The maximum size of Array or list will be 5*8 and each cell values will be integers. And hoping that saved image of array or list will contain integer values of each cells Commented Aug 24, 2021 at 0:21
  • 1
    What's the range of possible values of the integers please? Commented Aug 24, 2021 at 0:23
  • @MarkSetchell Range of integers will be [0, 32] or [-1, 32], thanks : ) Commented Aug 24, 2021 at 2:34

1 Answer 1

2

I has a quick try at this. You can play around with colours and sizes.

#!/usr/local/bin/python3

from PIL import Image, ImageFont, ImageDraw
import numpy as np

# Variables that can be edited
w, h = 8, 5     # width and height of Numpy array
cs = 100        # cell side length in pixels

# Make random array but repeatable
np.random.seed(39)
arr = np.random.randint(-1,33, (h,w), np.int)

# Generate a piece of canvas and draw text on it
canvas = Image.new('RGB', (w*cs,h*cs), color='magenta')

# Get a drawing context
draw = ImageDraw.Draw(canvas)
monospace = ImageFont.truetype("/Library/Fonts/Andale Mono.ttf", 40)

# Now write numbers onto canvas at appropriate points
for r in range(h):
   draw.line([(0,r*cs),(w*cs,r*cs)], fill='white', width=1)        # horizontal gridline
   for c in range(w):
      draw.line([(c*cs,0),(c*cs,h*cs)], fill='white', width=1)     # vertical gridline
      cx = cs // 2 + (c * cs)     # centre of cell in x-direction
      cy = cs // 2 + (r * cs)     # centre of cell in y-direction
      draw.text((cx, cy), f'{arr[r,c]}', anchor='mm', fill='white', font=monospace)

# Save
canvas.save('result.png')

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Mark: There was no need to use numpy here, so IMO you should have left it out — otherwise a very good answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.