加色剂Tkin

2024-10-01 00:21:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用Tkinter复制加色剂。

我的职能:

def synthese(red,green,blue):
    win2 = Tk()
    win2.title("ADDITIVE COLOR")
    win2.geometry("500x500")
    win2.resizable(0,0)

    hred = "#%02x%02x%02x" % (red, 0, 0) #RGB to Hexadecimal
    hgreen = "#%02x%02x%02x" % (0, green, 0)
    hblue = "#%02x%02x%02x" % (0, 0, blue)

    r = 50
    Width = 450
    Height = 450
    win3 = Canvas(win2, width = Width, height = Height, bg = 'white')
    win3.pack(padx=5,pady=5)
    win3.create_oval(10,150,300,440, outline=hred, fill=hred)
    win3.create_oval(150,150,440,440, outline=hblue, fill=hblue)
    win3.create_oval(75,10,375,300, outline=hgreen, fill=hgreen)

    win2.mainloop()

我得到的是:

Additive color

我想要的是: enter image description here

有可能合并颜色还是我需要找到碰撞区域?


Tags: tkintercreategreenblueredwidthfillheight
3条回答

使用PIL,你可以创建三个灰度层,画圆,并使用它们来创建预期的圆,但在黑色背景上。在

enter image description here

如果你使用倒置的图层,你会得到白色的背景,但有错误的圆圈。在

enter image description here

有了PIL,你甚至可以显示它或保存在文件中。在

from PIL import Image, ImageDraw

def synthese(red=255, green=255, blue=255):

    background = 0 # black

    # layers in greyscale
    layer_R = Image.new('L', (450, 450), background)
    layer_G = Image.new('L', (450, 450), background)
    layer_B = Image.new('L', (450, 450), background)

    # draw circle on red layer
    draw_R = ImageDraw.Draw(layer_R)
    draw_R.ellipse((10,150,300,440), red)

    # draw circle on green layer
    draw_G = ImageDraw.Draw(layer_G)
    draw_G.ellipse((150,150,440,440), green)

    # draw circle on blue layer
    draw_B = ImageDraw.Draw(layer_B)
    draw_B.ellipse((75,10,375,300), blue)

    #layer_R.show()               
    #layer_G.show()               
    #layer_B.show()

    #layer_R.save('layer_r.png')
    #layer_G.save('layer_g.png')
    #layer_B.save('layer_b.png')

    # create RGB image using greyscale layers
    image_RGB = Image.merge('RGB', (layer_R, layer_G, layer_B))

    # show it
    image_RGB.show()               
    #image_RGB.save('rgb.png')

synthese(255, 255, 255)

下面是一种使用Numpy绘制附加RGB圆的方法。它使用PIL(Pill)将Numpy数据转换为Tkinter照片对象,并将结果显示在Tkinter标签中。我用黑色背景是因为我们在做加色混合。在

import numpy as np
from PIL import Image, ImageTk
import tkinter as tk

width, height = 400, 360

# Make RGB colors
red, grn, blu = np.eye(3, dtype=np.uint8) * 255

class GUI:
    def __init__(self, width, height):
        self.root = root = tk.Tk()
        root.title('Circles')
        root.geometry('%dx%d' % (width, height))
        self.img_label = tk.Label(self.root)
        self.img_label.pack(fill='both', expand=True)

gui = GUI(width, height)

# Increase the scale for smoother circles
scale = 4
width *= scale
height *= scale
screen = np.zeros((height, width, 3), dtype=np.uint8)

def show(fname=None):
    img = Image.fromarray(screen, 'RGB')
    img = img.resize((width // scale, height // scale), resample=Image.BILINEAR)
    gui.photo = ImageTk.PhotoImage(image=img)
    gui.img_label.config(image=gui.photo)
    gui.root.update()
    if fname is not None:
        img.save(fname)

def disc(radius):
    diameter = 2 * radius
    yy, xx = np.mgrid[:diameter, :diameter] - radius
    c = xx * xx + yy * yy < radius * radius
    return c.reshape(diameter, diameter, 1)

def get_region(cx, cy, radius):
    ylo = cy - radius
    yhi = cy + radius
    xlo = cx - radius
    xhi = cx + radius
    return screen[ylo:yhi, xlo:xhi]

radius = 120 * scale
circle = disc(radius)

cx = width // 2
cy = 130 * scale
region = get_region(cx, cy, radius)
region |= circle * red
show()

cy += 97 * scale
cx -= 56 * scale
region = get_region(cx, cy, radius)
region |= circle * grn
show()

cx += 112 * scale
region = get_region(cx, cy, radius)
region |= circle * blu

show('rgb.png')
gui.root.mainloop()

输出

RGB additive color circles

您可以使用ImageChops添加图像。
所以你可以这样做:

from Tkinter import Tk, Canvas, Label
import ImageDraw, ImageChops, Image, ImageTk

image1 = Image.new("RGBA", (500, 500), color=0)
image2 = Image.new("RGBA", (500, 500), color=0)
image3 = Image.new("RGBA", (500, 500), color=0)

draw1 = ImageDraw.Draw(image1)
draw2 = ImageDraw.Draw(image2)
draw3 = ImageDraw.Draw(image3)

draw1.ellipse([10, 150, 300, 440], (128,0,0))
draw2.ellipse([150, 150, 440, 440], (0,0,128))
draw3.ellipse([75, 10, 375, 300], (0,128,0))

out = ImageChops.add(image1,image2,0.5)
out = ImageChops.add(out,image3,0.5)

win2 = Tk()

photo = ImageTk.PhotoImage(out)

label = Label(win2, image=photo)
label.pack()

win2.mainloop()

输出:

additive

相关问题 更多 >