2016-10-21 31 views
-1

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枚の異なる画像をプリントアウトすることになっています。たとえば、次のように

http://imgur.com/a/BpMHS

しかし、私はちょうど画像が表示されることなく、無地の青の背景を取得します。私は間違いもない。私が間違っている場所を知るだけでいいです。

私の間違いを見つけて、私がそれらを修正するのを助ける人に感謝します。

+1

'print()'を使って変数の値をチェックしたり、デバッガの使い方を学んでください。 – furas

+1

'set_a_pixel_of_bitmap'は値を返さないので(' None'を返します)、戻り値を '正しいグレースケール'として使用します。他の変数をチェックするために 'print()'を使うのが良いでしょう。 – furas

+0

これを指摘していただきありがとうございます。代わりにビットマップイメージを返すように変更しました。私は今これを入手しています:http://imgur.com/a/O4ANx。 –

答えて

1

私はほとんどすべてのものに変更 - あまりにも長い名前で始まるを - と、私はこれだ:それは"average of R,G,B"を取得し、デフォルトとして

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ファイルでテスト)

をそれはまた、私がなさ"only R"又は"only G"または"only B"(方法0、1、2)

+0

私は実際に持っている機能を変更したくありません。ちょうど私のコードで問題を見つけたい。関係なく、助けてくれてありがとう。 –

+0

申し訳ありませんあなたのコードで問題を見つけようとしましたが、長い名前や 'display_bitmap'のような多くの要素がありましたので、実際の問題を見つけるためにそれらを削除し始めました。このように私は私のバージョンを作成しました。 – furas

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() 

を取得することができ少しの間違いですが、これはうまくいくようです。

関連する問題