Skip to content
Advertisement

Change RGB of an Image using python

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:

import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os
import numpy as np

root = Tk()
root.geometry("550x300+300+150")
root.resizable(width=True, height=True)

#Find image
def openfn():
    filename = filedialog.askopenfilename(title='Open')
    return filename

#Here's where we load the image
def open_img():
    x = openfn()
    img = Image.open(x)
    img = img.resize((350, 350), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)
    panel = Label(root, image=img)
    panel.image = img
    panel.grid()


def gray():
    imagen = openfn()
    img = Image.open(imagen)
    img = img.convert("RGB")

    datas = img.getdata()

    new_image_data = []

    for item in datas:
        if item[0] in list(range(0, 255)):
            new_image_data.append((20, 40, 60))
        else:
            new_image_data.append(item)
    img.putdata(new_image_data)

    img.save("gray_image.png")

    img = img.resize((250, 250), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)

    panel = Label(root, image=img)
    panel.image = img

    panel.grid()
    
#buttons
btn = tk.Button(root, text='Select an image', command=open_img).grid(column=0,row=0)
gray = tk.Button(root, text='Gray filter', command=gray).grid(column=1,row=0)
root.mainloop()

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:

import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import os
import numpy as np

root = Tk()
root.geometry("550x300+300+150")
root.resizable(width=True, height=True)

#Find image
def openfn():
    filename = filedialog.askopenfilename(title="Open")
    return filename

#Here's where we load the image
def open_img():
    global filename, img, tk_img, panel
    filename = openfn()
    img = Image.open(filename)
    img = img.resize((350, 350), Image.ANTIALIAS)
    tk_img = ImageTk.PhotoImage(img)
    panel = Label(root, image=tk_img)
    panel.grid()

def apply_gray_filter():
    global tk_img
    draw = img.load()
    width, height = img.size
    # For each pixel
    for x in range(width):
        for y in range(height):
            # Get the value as (r, g, b) using draw[x, y] and then average it
            gray = sum(draw[x, y])/3
            # turn it into an int
            gray = int(gray)
            # Change the pixel's value
            draw[x, y] = (gray, gray, gray)
    # Display the new image
    tk_img = ImageTk.PhotoImage(img)
    panel.config(image=tk_img)

    
#buttons
select_img_button = tk.Button(root, text="Select an image", command=open_img)
select_img_button.grid(column=0, row=0)

gray_filter_button = tk.Button(root, text="Gray filter",
                               command=apply_gray_filter)
gray_filter_button.grid(column=1, row=0)

root.mainloop()

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement