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

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
lnus
  • 31
  • 7
  • That's because you don't do anything with them...add a `print(width, height)`....and it should gone (don't know why you even `print(im.size)`) – danidee Apr 15 '16 at 18:57
  • If you don't call them, then why do you use them? – Prof Apr 15 '16 at 18:57
  • It's the only way to get the correct im.size, they aren't actually storing anything inside. – lnus Apr 15 '16 at 18:58
  • if `im.size` is a list or tuple that contains exactly two elements. `width` and `height` should unpack it – danidee Apr 15 '16 at 19:00
  • The reason I print it is because it was easier to use as an example. They need to be saved in a tuple. – lnus Apr 15 '16 at 19:01
  • They're already saved in a tuple: **im.size** *is* that tuple. Since you don't use them, why not just delete that line of code. BTW, that's a warning, not an error. – Prune Apr 15 '16 at 19:05
  • @Prune I managed to get it working, but I still need the "with Image.open(Image_file) as im: But I put "pass" instead of "width, height = im.size". Thanks for your help :) – lnus Apr 15 '16 at 19:10
  • Actually I just now swapped it for "im = Image.open(Image_file)" and that worked alot better. You really helped me understand this. @Prune – lnus Apr 15 '16 at 19:13
  • The key is removing the **width, height** assignment, not in swapping out the **with** statement. Glad to be of help. – Prune Apr 15 '16 at 19:22

2 Answers2

2

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)
ballade4op52
  • 2,142
  • 5
  • 27
  • 42
  • This worked as well, but I had to use from io import StringIO, since Python 3.x doesn't have the StringIO module. Thanks for your help! :) – lnus Apr 15 '16 at 19:11
2

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

lnus
  • 31
  • 7
  • I know this is an old thread but this is a dangerous change as it doesn't close the file. Instead just change `width, height = im.size` to `image_size = im.size` and use `image_size` where you would use `im.size`. This way the file is properly closed and you have the tuple available for use when needed and no unused variables. – Labrys Knossos Nov 22 '17 at 01:45