Matheo99995
Matheo99995

Reputation: 11

Sepia filter in Python

I have a code to write and I need to create a sepia filter for my image. I came up with this but it is not the result that I need to have my image made with the color (159,85,30) that is not exactly the right sepia filter.

#FILTRE SÉPIA
from PIL import Image
Chateau = Image.open("Chateau.png")
Taille = Chateau.size
Chateau_Sepia = Image.new("RGB", (Taille))
for x in range (0, Taille[0]):
    for y in range (0, Taille[1]):
        Pixel = Chateau.getpixel((x, y))
        R = Pixel[0]
        G = Pixel[1]
        B = Pixel[2]
        taux_rouge = int(0.393 * R + 0.769 * G + 0.189 * B)
        taux_vert = int(0.349 * R + 0.686 * G + 0.168 * B)
        taux_bleu = int(0.272 * R + 0.534 * G + 0.131 * B)
        if taux_rouge > 255:
            taux_rouge = 255
        if taux_vert > 255:
            taux_vert = 255
        if taux_bleu > 255:
            taux_bleu = 255
        Chateau_Sepia.putpixel((x, y), (taux_rouge, taux_vert, taux_bleu))
Chateau_Sepia.save("Chateau Sépia.png")
Chateau_Sepia.show()

Upvotes: 1

Views: 1760

Answers (1)

Raphaël Maguet
Raphaël Maguet

Reputation: 111

for your problem try add the alpha channel and play with this value to get the tone you want.

Chateau_Sepia.putpixel((x, y), (taux_rouge, taux_vert, taux_bleu, 255))

Upvotes: 1

Related Questions