Reputation: 573
I want to modify the following code so that the image is sufficiently magnified to see individual pixels (python 3x).
import numpy as np
from PIL import Image
from IPython.display import display
width = int(input('Enter width: '))
height = int(input('Enter height: '))
iMat = np.random.rand(width*height).reshape((width,height))
im=Image.fromarray(iMat, mode='L')
display(im)
Upvotes: 12
Views: 8728
Reputation: 26211
Once you have your image, you could resize it by some ratio large enough to see individual pixels.
Example:
width = 10
height = 5
# (side note: you had width and height the wrong way around)
iMat = np.random.rand(height * width).reshape((height, width))
im = Image.fromarray(iMat, mode='L')
display(im)
10x larger:
display(im.resize((40 * width, 40 * height), Image.NEAREST))
Note: it's important to use Image.NEAREST
for the resampling filter; the default (Image.BICUBIC
) will blur your image.
Also, if you plan to actually display numerical data (not some image read from a file or generated as an example), then I would recommend doing away with PIL or other image processing libraries, and instead use proper data plotting libraries. For example, Seaborn's heatmap (or Matplotlib's). Here is an example:
sns.heatmap(iMat, cmap='binary')
Upvotes: 5