I want to write a script to create an image from a connection matrix. Basically, wherever there is a ‘1’ in the matrix, I want that area to be shaded in the image. For eg –
I created this image using Photoshop. But I have a large dataset so I will have to automate the process. It would be really helpful if anyone could point me in the right direction.
EDIT
The image that I am getting after using the script is this. This is due to the fact that the matrix is large (19 x 19). Is there any way I can increase the visibility of this image so the black and white boxes appear more clear?
3
Answers
I would suggest usage of
opencv
combined withnumpy
in this case.Create two-dimensional
numpy.array
ofdtype='uint8'
with0
for black and255
for white. For example, to get 2×2 array with white left upper, white right lower, black left lower and black right upper, you could use code:myarray = numpy.array([[255,0],[0,255]],dtype='uint8')
Then you could save that array as image with opencv2 in this way:
cv2.imwrite('image.bmp',myarray)
In which every cell of array is represented by single pixel, however if you want to upscale (so for example every cell is represented by 5×5 square) then you might use
numpy.kron
function, with following one line:myarray = numpy.kron(myarray, numpy.ones((5,5)))
before writing image
May be you can try this!
With a Seaborn heatmap:
Note the reverse colormap
gray_r
to have black for 1’s and white for 0’s.