我有这个代码可以显示四个图像的正确版本。
要做到这一点,我必须将它们变成灰度位图。灰度位图是位图,其中每个像素具有相同的红色,绿色和蓝色值(RGB)。为了将彩色位图转换为灰度,位图中的每个RGB值都设置为相同的值,即红色,绿色和蓝色分量的平均值。
到目前为止我有这个代码:
from tkinter import *
def convert_to_hex(colour_tuple):
hex_colour_string = "#"
for rgb in colour_tuple:
hex_colour_string = hex_colour_string + format(rgb, '02x')
return hex_colour_string
def set_a_pixel_of_bitmap(bitmap_image, colour_tuple, position_tuple):
hex_colour = convert_to_hex(colour_tuple)
bitmap_image.put(hex_colour, position_tuple)
def get_a_pixel_colour_tuple(bitmap_image, across, down):
pixel_colour_values = bitmap_image.get(across, down)
if type(pixel_colour_values) is str:
pixel_colour_values = pixel_colour_values.split()
else:
pixel_colour_values = list(pixel_colour_values)
for i in range(len(pixel_colour_values)):
pixel_colour_values[i] = int(pixel_colour_values[i])
return tuple(pixel_colour_values)
def display_bitmap_inside_canvas(a_canvas, an_image, centre_x, centre_y):
a_canvas.create_image((centre_x, centre_y), image = an_image)
return an_image
def get_revealed_bitmap(bitmap_image):
width = bitmap_image.width()
height = bitmap_image.height()
for across in range(width):
for down in range(height):
a_tuple = get_a_pixel_colour_tuple(bitmap_image, across, down)
new_list = list(a_tuple)
if a_tuple[0] < 10 and a_tuple[1] < 10 and a_tuple[2] < 10:
new_list[0] = ((new_list[0] * 10) + new_list[1] + new_list[2]) / 3
new_list [0] = new_list [1]
new_list[1] = new_list[2]
a_tuple = tuple(new_list)
set_a_pixel_of_bitmap(bitmap_image, a_tuple, (across ,down))
def from_gif_get_bitmap(gif_filename):
my_image = PhotoImage(file=gif_filename)
return my_image
def run_shit(a_canvas):
positions = [(450, 450), (150, 150), (450, 150), (150, 450)]
images = []
for selection in range(1, 5):
current_image_name = "PhotoImage" + str(selection)
current_image = from_gif_get_bitmap(current_image_name)
current_image = get_revealed_bitmap(current_image) #2
centre_tuple = positions[selection % len(positions)]
display_bitmap_inside_canvas(a_canvas, current_image, centre_tuple[0], centre_tuple[1]) #3
images.append(current_image)
return images
def main():
window = Tk()
window.title("Testing stuff")
window.geometry("600x600+10+20")
a_canvas = Canvas(window)
a_canvas.config(background="blue")
a_canvas.pack(fill=BOTH, expand = True)
a_canvas.pack()
image_list = run_shit(a_canvas)
window.mainloop()
main()
这应该以灰度打印4个不同的图像。例如:
但我只是得到一个纯蓝色的背景而没有显示图像。我也没有任何错误。只需要知道我哪里出错了。
我很感激有人发现我的错误并帮助我解决它们。
答案 0 :(得分:1)
我几乎改变了一切 - 从很长的名字开始 - 我得到了这个:
import tkinter as tk
def convert_to_greyscale(image, method=3):
for x in range(image.width()):
for y in range(image.height()):
pixel = image.get(x, y)
if method in (0, 1, 2): # R or G or B
val = pixel[method]
pixel = (val, val, val)
elif method == 3: # average
val = sum(pixel)//3
pixel = (val, val, val)
#else: # without changes
# pixel = pixel
image.put('#%02x%02x%02x' % pixel, (x, y))
def add_images(canvas):
positions = [(450, 450), (150, 150), (450, 150), (150, 450)]
images = []
for index, pos in enumerate(positions, 1):
image = tk.PhotoImage(file="PhotoImage%i.gif" % index)
convert_to_greyscale(image) #(image, index-1)
canvas.create_image(pos, image=image)
images.append(image)
return images
def main():
window = tk.Tk()
window.title("Testing stuff")
window.geometry("600x600+10+20")
canvas = tk.Canvas(window)
canvas.config(background="blue")
canvas.pack(fill=tk.BOTH, expand=True)
images = add_images(canvas)
window.mainloop()
main()
(仅在一个.gif文件上测试,因此请删除代码中的扩展名)
默认情况下,它会获得"average of R,G,B"
,但它也会获得"only R"
或"only G"
或"only B"
(对于方法0,1,2)
答案 1 :(得分:0)
from tkinter import *
def convert_to_hex(colour_tuple):
hex_colour_string = "#"
for rgb in colour_tuple:
hex_colour_string = hex_colour_string + format(rgb, '02x')
return hex_colour_string
def set_a_pixel_of_bitmap(bitmap_image, colour_tuple, position_tuple):
hex_colour = convert_to_hex(colour_tuple)
bitmap_image.put(hex_colour, position_tuple)
def get_a_pixel_colour_tuple(bitmap_image, across, down):
pixel_colour_values = bitmap_image.get(across, down)
if type(pixel_colour_values) is str:
pixel_colour_values = pixel_colour_values.split()
else:
pixel_colour_values = list(pixel_colour_values)
for i in range(len(pixel_colour_values)):
pixel_colour_values[i] = int(pixel_colour_values[i])
return tuple(pixel_colour_values)
def display_bitmap_inside_canvas(a_canvas, an_image, centre_x, centre_y):
a_canvas.create_image((centre_x, centre_y), image = an_image)
return an_image
def get_revealed_bitmap(bitmap_image):
width = bitmap_image.width()
height = bitmap_image.height()
highest_value = 256
for across in range(width):
for down in range(height):
a_tuple = get_a_pixel_colour_tuple(bitmap_image, across, down)
new_list = list(a_tuple)
if a_tuple[0] < highest_value and a_tuple[1] < highest_value and a_tuple[2] < highest_value:
red = new_list[0]
new_red = min(255, red * 10)
new_list[0] = new_red
new_list[1] = new_list[0]
new_list[2] = new_list[0]
a_tuple = tuple(new_list)
set_a_pixel_of_bitmap(bitmap_image, a_tuple, (across ,down))
return bitmap_image
def from_gif_get_bitmap(gif_filename):
my_image = PhotoImage(file=gif_filename)
return my_image
def run_shit(a_canvas):
positions = [(450, 450), (150, 150), (450, 150), (150, 450)]
images = []
for selection in range(1, 5):
current_image_name = "PhotoImage" + str(selection)
current_image = from_gif_get_bitmap(current_image_name)
current_image = get_revealed_bitmap(current_image) #2
centre_tuple = positions[selection % len(positions)]
display_bitmap_inside_canvas(a_canvas, current_image, centre_tuple[0], centre_tuple[1]) #3
images.append(current_image)
return images
def main():
window = Tk()
window.title("Testing stuff")
window.geometry("600x600+10+20")
a_canvas = Canvas(window)
a_canvas.config(background="blue")
a_canvas.pack(fill=BOTH, expand = True)
a_canvas.pack()
image_list = run_shit(a_canvas)
window.mainloop()
main()
我犯了一个小错误,但这似乎有效。