0

I'm trying to create some pixelart using a matrix in pycharm. The problem is that I have never used this program. It's supposed to work just by simply selecting if you're working with the RGB model, but it doesn't.

import cv2
import numpy as np
from matplotlib import pyplot as plt
pixels = ([0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0])
    ([0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0])
([0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0])
([0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0])
print (pixels[2][4])
cv2.waitKey()
Nick
  • 4,820
  • 18
  • 31
  • 47
  • 2
    Does this answer your question? [Saving a Numpy array as an image](https://stackoverflow.com/questions/902761/saving-a-numpy-array-as-an-image) – Karthik Sep 11 '20 at 04:25

2 Answers2

0

You need to save pixels as a numpy array with type uint8 and then let cv2 display it. If you pass 0 to waitKey the window will stay open until you close it manually.

import cv2
import numpy as np

pixels = np.array([[0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0],[0,0,0,1,1,1,1,0,0,1,1,1,1,0,0,0],[0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0],[0,1,1,0,1,1,0,0,0,0,1,1,0,1,1,0]], np.uint8)

cv2.imshow("My Image", pixels)
cv2.waitKey(0)
Merve Sahin
  • 1,008
  • 2
  • 14
  • 26
0

You can use the Pillow library.

from PIL import Image
import numpy as np

pixels = np.array(pixels).astype(np.uint8) # converts pixels to a numpy array

image = Image.fromarray(pixels)

# now you can save your image using

image.save('newimage.png')
Riptide
  • 472
  • 3
  • 14