[英]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)
好的,所以我正在創建一個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)
我的解決方案
通過交換
with Image.open(Image_file) as im:
width, height = im.size
對於
im = Image.open(Image_file)
它的效果更好。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.