T
T
Talyan2018-09-26 22:06:21
Python
Talyan, 2018-09-26 22:06:21

Why does an error occur when loading an image in PIL PhotoImage?

Foreword:
Perhaps I do everything through one place? And is there another option to constantly capture the image of the game with further processing and display the processed image in the window? It seems that there is still PyQT - will it be easier to do? I am not familiar with python, I write code according to the documentation of libraries on the Internet.
******
The task is to take a screenshot of the screen area with the game, put it in a variable for further actions in OpenCV and display this image in the TkInter window.
Includes:

from Tkinter import *
import cv2 as cv
import win32gui
from PIL import ImageOps, Image, ImageGrab, ImageTk
from numpy import *

We get the image (the main function is get_screen, the result of the function is perfectly processed by the OpenCV library without errors:
WINDOW_SUBSTRING="Game Window Name"

def set_window_coordinates(hwnd, window_info):
    if win32gui.IsWindowVisible(hwnd):
        if WINDOW_SUBSTRING in win32gui.GetWindowText(hwnd):
            rect = win32gui.GetWindowRect(hwnd)
            x = rect[0]
            y = rect[1]
            w = rect[2] - x
            h = rect[3] - y
            window_info['x'] = x
            window_info['y'] = y
            window_info['width'] = w
            window_info['height'] = h
            window_info['name'] = win32gui.GetWindowText(hwnd)
            win32gui.SetForegroundWindow(hwnd)


def get_screen(x1, y1, x2, y2):
    box = (x1 + 8, y1 + 30, x2 - 8, y2)
    screen = ImageGrab.grab(box)
    img = array(screen.getdata(), dtype=uint8).reshape((screen.size[1], screen.size[0], 3))
    return img


def get_window_info():
    # set window info
    window_info = {}
    win32gui.EnumWindows(set_window_coordinates, window_info)
    return window_info

We create an interface and load an image into it:
def loop():
    window_info = get_window_info()
    root = Tk()
    root.title("GUI on Python")

    img = get_screen(
        window_info["x"],
        window_info["y"],
        window_info["x"] + window_info["width"],
        window_info["y"] + window_info["height"]
    )
    root.geometry(str(window_info["width"]) + "x" + str(window_info["height"]))

    #print img
    photo = PhotoImage(image=img) # - тут выдается ошибка
    #btn = Button(text="Click Me", background="#555", foreground="#ccc",
    #             padx="20", pady="8", font="16", command=click_button)
    photo.pack()
    #btn.pack()

    root.mainloop()


loop()

An error:

C:\Python27\python.exe D:/games/bot-master/t.py
Traceback (most recent call last):
File "D:/games/bot-master/t.py", line 98, in
loop( )
File "D:/games/bot-master/t.py", line 87, in loop
photo = PhotoImage(image=img)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3371 , in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3325, in __init__
self.tk. call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: unknown option "-image"
Process finished with exit code 1

I don’t understand why the Unknown option, because even in the library file itself this moment is processed - opened, looked.
Python 2.7

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-09-27
@flapflapjack

This is not a named parameter. Just
photo = PhotoImage(img)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question