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)

好的,所以我正在创建一个网络爬虫,它应该下载 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")

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

4

2 回答 2

2

此方法可能会为您的目的而简化

例如:

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)
于 2016-04-15T19:02:34.067 回答
2

我的解决方案

通过交换

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

为了

im = Image.open(Image_file)

结果好多了。:)

于 2016-04-15T19:15:13.850 回答