DuttaA
DuttaA

Reputation: 923

Converting numpy array to picture

So I have got a string of characters and I am representing it by a number between 1-5 in a numpy array. Now I want to convert it to a pictorial form by first repeating the string of numbers downwards so the picture becomes broad enough to be visible (since single string will give a thin line of picture). My main problem is how do I convert the array of numbers to a picture?

Upvotes: 0

Views: 113

Answers (1)

dennlinger
dennlinger

Reputation: 11488

This would be a minimal working example to visualize with matplotlib:

import numpy as np
import matplotlib.pyplot as plt

# generate 256 by 1 vector of values 1-5
img = np.random.randint(1,6, 256)
# transpose for visualization
img = np.expand_dims(img, 1).T

# force aspect ratio
plt.imshow(img, aspect=100)
# or, alternatively use aspect='auto'
plt.show()

You can force the aspect ratio of the plotted figure by simply setting the aspect option of imshow()

Upvotes: 1

Related Questions