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)
好的,所以我正在创建一个Web爬网程序,该爬网程序应该下载1920 x 1080大小的图像。实际程序可以运行,但是PyCharm和Codacy表示在这里未使用width和height变量:
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)