PIL Image.Open导致RecursionError

时间:2020-10-17 12:28:31

标签: python image tkinter python-imaging-library

我编写了一个程序,并且在程序的一部分中,我将输出的图像显示为一系列图像,可以使用“下一个/上一个”按钮在它们之间进行切换。 每次单击next / prev时,都会使用指向下一张图片的正确路由来更新click_dict,然后运行此代码:

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi

app = FastAPI()


@app.get("/items/")
async def read_items():
    return [{"name": "Foo"}]


def custom_openapi():
    if app.openapi_schema:
        return app.openapi_schema
    openapi_schema = get_openapi(
        title="Custom title",
        version="2.5.0",
        description="This is a very custom OpenAPI schema",
        routes=app.routes,
    )
    openapi_schema["paths"]["/api/auth"] = {
        "post": {
            "requestBody": {"content": {"application/json": {}}, "required": True}, "tags": ["Auth"]
        }
    }
    app.openapi_schema = openapi_schema
    return app.openapi_schema


app.openapi = custom_openapi

一切正常,并且在图像之间移动并更新StringVars,但是最终在80-100张图像之间移动后,我遇到了此错误:

        def set_image():
            wordvar.set("Word: "+str(click_dict['curr_word']))
            word_found_var.set("Word Count: " + str(click_dict['cnt_appearances']))
            tot_word_found_var.set("Total Found Count: 
                        "+str(click_dict['curr_index']+1)+"/"+str(click_dict['tot_cnt_appearances']))
            filelbl_var.set("File: " + click_dict['curr_file'] + " Page: " + 
                        str(int(click_dict['curr_page'])+1))
            im = Image.open(click_dict['curr_directory'] + "//" + click_dict['curr_filename'])
            zoom = 0.9
            while im.size[0] >= 700:
                x = im.size[0]
                y = im.size[1]
                im = im.resize((round(x * 0.98), round(y * 0.98)))
            photo = ImageTk.PhotoImage(im)
            im.close()
            image.config(image=photo)
            output_window.mainloop()

我正在尝试使用im.close(),如您在代码中所见,但这仍然无济于事,最终一切都崩溃了 有什么想法可以防止这种情况发生吗? 谢谢!

0 个答案:

没有答案