简体   繁体   中英

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

This method may simplify for your purpose

For instance:

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM