PyCharm 中未使用的变量

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)

好的,所以我正在创建一个网络爬虫,它应该下载大小为 1920 x 1080 的图像。实际程序有效,但 PyCharm 和 Codacy 说宽度和高度变量在这里未使用:

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

我想这是对的,因为我稍后不会在代码中调用它们,但我想知道是否有任何其他方法可以做到这一点,所以在查看代码时我没有看到未使用的代码错误.

另一个例子:

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

这可能是一个愚蠢的问题,但我感谢所有答案。 :)

此方法可能会简化您的用途

例如:

from StringIO import StringIO

imageURL = img.get('src')
fd = urllib.request.urlopen(link)
image_name = imageName + '.bmp'

i = Image.open(StringIO(fd.content))

# i.save(dirName + '/' + image_name)

我的解决方案

通过交换

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

为了

im = Image.open(Image_file)

结果好多了。 :)