1

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 :

Image for Image.fromarray

2 Answers 2

1

You can try to use Image.fromarray:

Image.fromarray(matrice, mode=couleur)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot for your answer, I have tried out this solution but another problem appears.
0

Sorry for my longtime answer. The problem is in the type being used and converting it to the image. If you use a single-byte type for an image, then the matrix type must also be single-byte. Example:

from PIL import Image
import numpy as np

size_x = 50
size_y = 8
m = "L"

matrix = np.array([[255] * 50 for _ in range(size_y)], dtype="uint8")
im = Image.fromarray(matrix, mode=m)

im.save('Degrade.jpg')
im.show()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.