pythonpython-3.xpycharmunused-variablescodacy

Unused variables in PyCharm


if ".jpg" in link or ".png" in link:
    fd = urllib.request.urlopen(link)
    #Checks the dimensions of said image file
    Image_file = io.BytesIO(fd.read())
    with Image.open(Image_file) as im:
        width, height = im.size
    print(im.size)

Okay, so I'm creating a web crawler that is supposed to download images of the size 1920 by 1080. The actual program works, but PyCharm and Codacy says the width and height variables are unused here:

with Image.open(Image_file) as im:
    width, height = im.size

I guess that's right since I don't call them later in the code, but I'm wondering if there is any other way to do this so I don't see the unused code error when looking through the code.

Another example:

with Image.open(Image_file) as im:
                width, height = im.size
            #If the dimensions are 1920 by 1080, it will download the file in Wallpapers/filename_#WALL_#PAGE
            #This format makes it easy to check which images was downloaded from which page
            if(im.size == (1920, 1080)):
                wall += 1
                print("***FOUND***\t" + str(wall))
                urllib.request.urlretrieve(link, "Wallpapers/filename"+ str(wall) +"_" + str(page) +".png")

Might be a stupid question but I appreciate all answers. :)


Solution

  • My Solution

    By swapping

    with Image.open(Image_file) as im:
                width, height = im.size
    

    for

    im = Image.open(Image_file)
    

    it worked out alot better. :)