Python png olan bir resmin rengini degiştirmek

merhabalar, başlıkta belirtmiş oldugum gibi elimde png bir fotograf var üzerine geldigimde fotografın
renginin kırmızı olmasını istiyorum bunu kodlarla saglamak mümkünmü extra resim kullanmadan bilgilendirirseniz sevinirim.

KOD:


from functools import partial
from tkinter import*


class hover_event():
    def enter_hover(widget, event):
        #Renk degiştirmesini istedigim bölüm!
        print("test")
    def leave_hover(widget, event):
        global res
        print("test")

root = Tk()
root.geometry("850x750+500+200")
res1 = PhotoImage(file="../İmage/chat.png").subsample(2, 2)
label = Label(root,image=res1)
label.pack()
label.bind("<Enter>", partial(hover_event.enter_hover, label))
root.mainloop()

Merhaba,

Şöyle yapabilirsiniz.

import tkinter as tk

from PIL import Image, ImageTk, ImageOps


def on_button_press(label, img, colorize=False):
    if colorize:
        img = img.convert(mode="L")
        img = ImageOps.colorize(img, black="red", white="cyan")
    img = ImageTk.PhotoImage(img)
    label.img = img
    label["image"] = img
    

def main():
    root = tk.Tk()
    img = Image.open("img.png")
    image = ImageTk.PhotoImage(img)
    label = tk.Label(root, image=image)
    label.pack()
    label.bind(
        "<Button-1>", 
        lambda event: on_button_press(label, img, colorize=True)
    )
    label.bind(
        "<Button-3>", 
        lambda event: on_button_press(label, img)
    )
    root.mainloop()
    
    
if __name__ == "__main__":
    main()

Yukarıdaki kodlara göre, farenin sol tuşuna basarsanız renk değişir, sağ tuşuna basarsanız, resmin rengi tekrar eski haline döner. Tabi siz bind metodunun sequence parametresini <Enter> olarak değiştirirsiniz. Bu örnekte fare düğmelerini kullandım ben.

Cevabınız için teşkkür ederim ama tam olarak işe yaramadı

Tam olarak nasıl bir şey istiyordunuz? Resmin orijinal hali nasıl, siz nasıl bir değişiklik yapmak istiyorsunuz?

1 Beğeni

deneme
resim bu ve bunun renginin kırmızı olmasını istiyorum

Fonksiyonu şöyle değiştirin:

def on_button_press(label, img, colorize=False):
    if colorize:
        img = img.convert(mode="RGBA")
        img = img.split()[3]
        img = img.convert(mode="L")
        img = ImageOps.colorize(img, black=label["bg"], white="red")
    img = ImageTk.PhotoImage(img)
    label.img = img
    label["image"] = img

Sol düğmeye tıklamadan önce:
2020-08-07 23-07-47 ekran görüntüsü
Sol düğmeye tıkladıktan sonra:
2020-08-07 23-24-30 ekran görüntüsü
Sağ düğmeye tıklarsanız, resim eski haline geri döner.

ValueError: unknown color specifier: ‘systembuttonface’ şeklinde bir hata alıyorum

Kodlarınızı bir paylaşabilir misiniz?

import tkinter as tk

from PIL import Image, ImageTk, ImageOps


def on_button_press(label, img, colorize=False):
    if colorize:
        img = img.convert(mode="RGBA")
        img = img.split()[3]
        img = img.convert(mode="L")
        img = ImageOps.colorize(img, black=label["bg"], white="red")
    img = ImageTk.PhotoImage(img)
    label.img = img
    label["image"] = img


def main():
    root = tk.Tk()
    img = Image.open("deneme.png")
    image = ImageTk.PhotoImage(img)
    label = tk.Label(root, image=image)
    label.pack()
    label.bind(
        "<Button-1>",
        lambda event: on_button_press(label, img, colorize=True)
    )
    label.bind(
        "<Button-3>",
        lambda event: on_button_press(label, img)
    )
    root.mainloop()


if __name__ == "__main__":
    main()

İlginç, bende çalışıyor. Aşağıdaki belirteçleri kullanmayı deneyin.

img = ImageOps.colorize(img, black=label["bg"], white=(255, 0, 0))

Veya;

img = ImageOps.colorize(img, black=label["bg"], white="rgb(255, 0, 0)")

Veya;

img = ImageOps.colorize(img, black=label["bg"], white="#ff0000")

Veya;

img = ImageOps.colorize(img, black=label["bg"], white="rgb(100%, 0%, 0%)")

Hepsini denedim hata hep bu şekilde

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:/Users/user/Desktop/Python/test2.py", line 26, in <lambda>
    lambda event: on_button_press(label, img, colorize=True)
  File "C:/Users/user/Desktop/Python/test2.py", line 11, in on_button_press
    img = ImageOps.colorize(img, black=label["bg"], white="rgb(100%, 0%, 0%)")
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageOps.py", line 172, in colorize
    black = _color(black, "RGB")
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageOps.py", line 44, in _color
    color = ImageColor.getcolor(color, mode)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageColor.py", line 131, in getcolor
    color, alpha = getrgb(color), 255
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\lib\site-packages\PIL\ImageColor.py", line 116, in getrgb
    raise ValueError("unknown color specifier: %r" % color)
ValueError: unknown color specifier: 'systembuttonface'

Bir dakika, bir Windows’a geçeyim, orada bir deneyeyim.

1 Beğeni

Tamamdır bekliyorum sizide ugraştırdım kusura bakmayın

Evet, Windows’ta ben de aynı hatayı aldım. Bir araştıralım bakalım, niye Windows’ta böyle bir hata veriyor.

Tamam şöyle yapın:

def on_button_press(label, img, colorize=False):
    if colorize:
        bg = [i / 256 for i in label.winfo_rgb("SystemButtonFace")]
        img = img.convert(mode="RGBA")
        img = img.split()[3]
        img = img.convert(mode="L")
        img = ImageOps.colorize(img, black=bg, white="red")
    img = ImageTk.PhotoImage(img)
    label.img = img
    label["image"] = img

Süper çalıştı bir sorum daha olacaktı boyutlandırmayı nasıl ayarlayabilirim eskisinde subsample kullanıyordum şuan nasıl olacak bilgilendirirmisiniz

resize fonksiyonunu kullanabilirsiniz.

Örnek:

def on_button_press(label, img, colorize=False):
    if colorize:
        bg = [i / 256 for i in label.winfo_rgb("SystemButtonFace")]
        img = img.convert(mode="RGBA")
        img = img.split()[3]
        img = img.convert(mode="L")
        img = ImageOps.colorize(img, black=bg, white="red")
        img = img.resize((100, 100))
    img = ImageTk.PhotoImage(img)
    label.img = img
    label["image"] = img
1 Beğeni

Herşey için teşekkürler dostum

Rica ederim, kolay gelsin.