0

I have a list 'L'. I want to use this list of numbers to create a map for an image. So I first create a matrix "n" similar to the size of the input Image - 2084, 2084

L = [234, 78, 56, 98, 555, 876, 998 ...] 
n = np.zeros((2084, 2084), dtype = "uint8")

This gives me an matrix of size - 2084 x 2084. Now I flatten it to create an array out of it.

j = n.flatten()

As I want to create a gray scale image of this and replace the positions in this Image which are given in the List "L" to white. I replace the 0's in the array to 255

for i in L:
  j[i] = 255

After this I reshape the array to the matrix form.

 o  = np.reshape(j, (2084, 2084))

But when I try to open this o using matplotlib i only get the black image with no white pixel which should have been there because of the value 255 in the matrix.

plt.imshow(o, cmap="gray", vmin=0, vmax=255)

I would like to use the numbers in the List to map their values as locations in the image and change the colour of that location to white.

I can be wrong in understanding some fundamentals here with regards to image, but some help here would be appreciated

Sahil
  • 439
  • 2
  • 6
  • 17
  • Possible duplicate of [Saving a Numpy array as an image](https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image) – Jurgen Strydom Mar 26 '19 at 07:41

2 Answers2

2

I assume that this is a screen resolution problem. Using your code (slightly simplified) but reducing the resolution clearly works:

import matplotlib.pyplot as plt
import numpy as np

L = [234, 78, 56, 98, 555, 876, 998]
n = np.zeros((32, 32), dtype="uint8")
n.ravel()[L] = 255

plt.figure(figsize=(2.5, 2.5))
plt.imshow(n, cmap="gray", vmin=0, vmax=255)

enter image description here

2048x2048 is more than my screen resolution, so in that case I don't see anything: each image pixel is smaller than a screen pixel. Also, all the white dots would lie in the first row of pixels, which would make it even harder to see.

cheersmate
  • 2,385
  • 4
  • 19
  • 32
1

There is nothing wrong with your code but maybe your white points in the image is too disperse to see them on the screen. If you want to see the white points in the image, you can set plt make a large resolution before show it by adding plt.figure(figsize=(M, M)) where M is a large integer number such as 100. Example code as following:

N = 1000
L = [8000, 8001, 8002, 8003, 9000, 9001, 9002, 9003, 10000, 10001, 10002, 10003, 11000, 11001, 11002, 11003]
a = np.zeros((N,N), dtype=np.uint8)
a.ravel()[L] = 255
np.reshape(a, (N,N))
plt.figure(figsize=(100, 100))
_ = plt.imshow(a, cmap="gray", vmin=0, vmax=255)
yann
  • 652
  • 7
  • 14