4つの画像の正しいバージョンを表示するはずのこのコードがあります。Tkinterプロジェクトで画像をグレースケールに変換する
これを行うには、グレースケールビットマップにする必要があります。グレースケールビットマップは、すべてのピクセルが同じ赤、緑、青の値(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枚の異なる画像をプリントアウトすることになっています。たとえば、次のように
しかし、私はちょうど画像が表示されることなく、無地の青の背景を取得します。私は間違いもない。私が間違っている場所を知るだけでいいです。
私の間違いを見つけて、私がそれらを修正するのを助ける人に感謝します。
'print()'を使って変数の値をチェックしたり、デバッガの使い方を学んでください。 – furas
'set_a_pixel_of_bitmap'は値を返さないので(' None'を返します)、戻り値を '正しいグレースケール'として使用します。他の変数をチェックするために 'print()'を使うのが良いでしょう。 – furas
これを指摘していただきありがとうございます。代わりにビットマップイメージを返すように変更しました。私は今これを入手しています:http://imgur.com/a/O4ANx。 –