在Tkinter中查找相同的图像?

时间:2016-03-15 23:28:35

标签: python image tkinter photoimage

我的Tkinter GUI直接从关联的last.fm链接加载某个歌曲/艺术​​家组合的专辑封面(如下所示:http://ift.tt/1Jepy2C,因为它是由ifttt.com提取并重定向到last.fm上的png文件。) 如果last.fm上没有专辑封面,ifttt将重定向到此图片:https://ifttt.com/images/no_image_card.png

问题是这张图片的尺寸与方形相册的尺寸不同,这意味着我制作了一张" N / A"如果我收到那张照片,我会插入的png文件。不幸的是,只是这样:

from tkinter import *
local_copy_of_not_available_image = PhotoImage(file="album_not_found.png")
internet_image = PhotoImage(data=b64_Album_data) # fetched b64 data through urllib, which should contain either an album cover or the n/a picture above
if internet_image == local_copy_of_not_available_image:
    actual_image = PhotoImage(file="my_album_not_found_square_replacement_picture.png")
else:
    actual_image = PhotoImage(data=b64_Album_data)

cover = Label(root, image=actual_image)
cover.pack()

mainloop()

不起作用。显然,即使它们是相同的图像,internet_image中的b64数据也不同于从我的硬盘驱动器加载的文件。 我的问题是,我如何检查两个图像在原始数据方面是否完全相同,以便检测ifttt何时向他发送他们的n / a图片?

1 个答案:

答案 0 :(得分:1)

我完全解决了它,因为来自last.fm的所有专辑封面都是方形的300x300px图片。由于来自ifttt的n / a图像是矩形的,比它更宽,我有几个可能性:

1)检查宽高比。如果它不是1,我没有封面图片。

2)只需检查下载图像的宽度。如果它不是300px,我没有封面图片。

3)将下载的图像宽度与错误图像的本地副本的宽度进行比较。如果它们相等,我就没有封面图片。