PIL creating thumbnail error (TypeError: 'int' object is not subscriptable)

PIL creating thumbnail error (TypeError: 'int' object is not subscriptable)

我正在编写速记代码,但我的问题是我想隐藏在另一张图片上的图片大小。 因为如果图像容器小于要隐藏的图像,那就是 return 超出索引的错误。

所以我找到了一个解决方案,如果尺寸大于图像容器,则创建图像的缩略图以隐藏。

代码如下(return题目中的错误):

    global filepath,filepath2
    im_pass = PIL.Image.open(filepath)
    im_cont = PIL.Image.open(filepath2)
    width_x, height_y = im_pass.size #(x,y)
    width_x2, height_y2 = im_cont.size #(x,y)
    if width_x > width_x2 or height_y > height_y2:
        if width_x2 > height_y2:
            max_size = width_x2
            min_size = height_y2
        elif height_y2 > width_x2:
            max_size = height_y2
            min_size = width_x2
        width_x, height_y = int(min_size//1.5), int(min_size//1.5)
        im_pass.thumbnail(width_x,height_y)
  File ".\Projet_final.py", line 21, in Stega
    im_pass.thumbnail(width_x,height_y)
  File "C:\Users\Naylor\AppData\Local\Programs\Python\Python37-32\lib\site-packages\PIL\Image.py", line 2119, in thumbnail
    if x > size[0]:

TypeError: 'int' object is not subscriptable

所以我期望的是创建缩略图并将其隐藏到容器图像中(该部分已经工作)。

根据 documentation (reference),thumbnail 期望大小在 (width, height) 的元组中定义,而不是两个单独的参数。

global filepath,filepath2
    im_pass = PIL.Image.open(filepath)
    im_cont = PIL.Image.open(filepath2)
    width_x, height_y = im_pass.size #(x,y)
    width_x2, height_y2 = im_cont.size #(x,y)
    if width_x > width_x2 or height_y > height_y2:
        if width_x2 > height_y2:
            max_size = width_x2
            min_size = height_y2
        elif height_y2 > width_x2:
            max_size = height_y2
            min_size = width_x2
        width_x, height_y = int(min_size//1.5), int(min_size//1.5)
        im_pass.thumbnail((width_x,height_y))