I’m trying to change the color of an image using RGB values in python, for example, if I want to change my image to grayscale I want to be able to manipulate the RGB values of the image. I’ve read that if I want my image in grayscale I would have to do something like Gray = (RedValue + GreenValue + BlueValue) / 3.
Here’s my attempt code:
JavaScript
x
58
58
1
import tkinter as tk
2
from tkinter import *
3
from PIL import ImageTk, Image
4
from tkinter import filedialog
5
import os
6
import numpy as np
7
8
root = Tk()
9
root.geometry("550x300+300+150")
10
root.resizable(width=True, height=True)
11
12
#Find image
13
def openfn():
14
filename = filedialog.askopenfilename(title='Open')
15
return filename
16
17
#Here's where we load the image
18
def open_img():
19
x = openfn()
20
img = Image.open(x)
21
img = img.resize((350, 350), Image.ANTIALIAS)
22
img = ImageTk.PhotoImage(img)
23
panel = Label(root, image=img)
24
panel.image = img
25
panel.grid()
26
27
28
def gray():
29
imagen = openfn()
30
img = Image.open(imagen)
31
img = img.convert("RGB")
32
33
datas = img.getdata()
34
35
new_image_data = []
36
37
for item in datas:
38
if item[0] in list(range(0, 255)):
39
new_image_data.append((20, 40, 60))
40
else:
41
new_image_data.append(item)
42
img.putdata(new_image_data)
43
44
img.save("gray_image.png")
45
46
img = img.resize((250, 250), Image.ANTIALIAS)
47
img = ImageTk.PhotoImage(img)
48
49
panel = Label(root, image=img)
50
panel.image = img
51
52
panel.grid()
53
54
#buttons
55
btn = tk.Button(root, text='Select an image', command=open_img).grid(column=0,row=0)
56
gray = tk.Button(root, text='Gray filter', command=gray).grid(column=1,row=0)
57
root.mainloop()
58
I made a function called gray where I reloaded the image and change it’s colors, but I don’t want that, I want to apply changes to the image I loaded.
Hope you can help me out.
Advertisement
Answer
Try this:
JavaScript
1
54
54
1
import tkinter as tk
2
from tkinter import *
3
from PIL import ImageTk, Image
4
from tkinter import filedialog
5
import os
6
import numpy as np
7
8
root = Tk()
9
root.geometry("550x300+300+150")
10
root.resizable(width=True, height=True)
11
12
#Find image
13
def openfn():
14
filename = filedialog.askopenfilename(title="Open")
15
return filename
16
17
#Here's where we load the image
18
def open_img():
19
global filename, img, tk_img, panel
20
filename = openfn()
21
img = Image.open(filename)
22
img = img.resize((350, 350), Image.ANTIALIAS)
23
tk_img = ImageTk.PhotoImage(img)
24
panel = Label(root, image=tk_img)
25
panel.grid()
26
27
def apply_gray_filter():
28
global tk_img
29
draw = img.load()
30
width, height = img.size
31
# For each pixel
32
for x in range(width):
33
for y in range(height):
34
# Get the value as (r, g, b) using draw[x, y] and then average it
35
gray = sum(draw[x, y])/3
36
# turn it into an int
37
gray = int(gray)
38
# Change the pixel's value
39
draw[x, y] = (gray, gray, gray)
40
# Display the new image
41
tk_img = ImageTk.PhotoImage(img)
42
panel.config(image=tk_img)
43
44
45
#buttons
46
select_img_button = tk.Button(root, text="Select an image", command=open_img)
47
select_img_button.grid(column=0, row=0)
48
49
gray_filter_button = tk.Button(root, text="Gray filter",
50
command=apply_gray_filter)
51
gray_filter_button.grid(column=1, row=0)
52
53
root.mainloop()
54
It loops through all of the pixels and converts each one to grey scale. For some reason, if you press it multiple times it will turn the image into white. I have no idea why.