I am trying to create an image on python 3.7 (on Thonny). I want to set a matrix with values in terms of grey so 0 would be black and 255 would be white. This matrix would represent the pixel of the image I want to create. Basically, I want to create an image in black and white based on a matrix. Here is my code :
from PIL import Image
import numpy as np
largeur = 2
hauteur = 2
couleur = 'L'
im = Image.new(couleur, (largeur, hauteur))
matrice = np.array([[0,255],[255,0]])
for x in range(largeur):
for y in range(hauteur):
a = int()
a = matrice[x,y]
print(a)
im.putpixel((x, y), (a)) # Composante R en fonction de la hauteur
im.save('Degrade.jpg')
im.show()
When I run the program, it says that a is the problem : "color must be int or tuple".
Thank you for your help and have a nice day !
I tried with Image.fromarray :
from PIL import Image
import numpy as np
largeur = 50
hauteur = 8
couleur = 'L'
#im = Image.new(couleur, (largeur, hauteur))
matrice = np.array([[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255],
[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255]])
#for x in range(largeur):
#for y in range(hauteur):
#a = int()
#a = matrice[x,y]
#print(a)
#im.putpixel((x, y), (255)) # Composante R en fonction de la hauteur
im = Image.fromarray(matrice, mode=couleur)
im.save('Degrade.jpg')
im.show()
With this, I should have have an image 50 x 8 pixels with only white pixels but I get white and black pixels (look at picture below), I know we don't see much sorry :) also thank you a lot for your answer :
