Search code examples
pythonpython-3.xsteganography

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


I'm coding a stenography code, but my problem is the size of the image that I want to Hide on another image. Because if the image container is smaller than the image to hide that's return an error of out of index.

So I find a solution, it's to create a thumbnail of the image to hide if the size is bigger than the image container.

Here is the code (return the error in the title):

    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

So what I'm expecting is to create the thumbnail and to Hide it into the container image (that part already work).


Solution

  • According to the documentation (reference), thumbnail expects the size to be defined in a tuple of (width, height), and not two separate parameters.

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