简体   繁体   English

使用 python 更改图像的 RGB

[英]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.我正在尝试使用 python 中的 RGB 值更改图像的颜色,例如,如果我想将图像更改为灰度,我希望能够操纵图像的 RGB 值。 I've read that if I want my image in grayscale I would have to do something like Gray = (RedValue + GreenValue + BlueValue) / 3 .我已经读过,如果我想要灰度图像,我将不得不做类似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.我制作了一个名为 gray 的 function,我在其中重新加载了图像并将其更改为 colors,但我不希望这样,我想对加载的图像应用更改。

Hope you can help me out.希望你能帮助我。

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.我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM