如何使用Python检查存储为URL的图像的透明度?
我有一个csv文件,其中包含指向我的图像的URL。我想检查哪个图像有透明的徽标。对于某些行,没有提供URL,对于某些行,URL不打开(这就是我添加请求例外的原因) 下面的代码适用于我的csv文件的前3行。但是,对于没有url且为空的第四行,它抛出以下错误:如何使用Python检查存储为URL的图像的透明度?,python,matplotlib,python-requests,Python,Matplotlib,Python Requests,我有一个csv文件,其中包含指向我的图像的URL。我想检查哪个图像有透明的徽标。对于某些行,没有提供URL,对于某些行,URL不打开(这就是我添加请求例外的原因) 下面的代码适用于我的csv文件的前3行。但是,对于没有url且为空的第四行,它抛出以下错误: NoTransparent! Transparent NoTransparent! Traceback (most recent call last): File "/path/to/transparency.py",
NoTransparent!
Transparent
NoTransparent!
Traceback (most recent call last):
File "/path/to/transparency.py", line 13, in <module>
pic = plt.imread(x)
File "/usr/local/lib/python3.7/site-packages/matplotlib/pyplot.py", line 2230, in imread
return matplotlib.image.imread(fname, format)
File "/usr/local/lib/python3.7/site-packages/matplotlib/image.py", line 1486, in imread
with img_open(fname) as image:
File "/usr/local/lib/python3.7/site-packages/PIL/ImageFile.py", line 107, in __init__
self._open()
File "/usr/local/lib/python3.7/site-packages/PIL/PngImagePlugin.py", line 636, in _open
if self.fp.read(8) != _MAGIC:
AttributeError: 'float' object has no attribute 'read'
有人能帮我解决这个问题吗?这可能与请求异常有关。提前谢谢你 你就不能加一张简单的支票吗
for x in urls:
if x == '': # or some other option to check for the blank urls
continue
try:
pic = plt.imread(x)
... etc. with your code
谢谢,我添加了'if x='':'步骤(见下文)。但它仍然抛出相同的错误,表明该位有问题
pic=plt.imread(x)
。你知道我做错了什么吗?如果x
实际上是空的,那么该行永远不会执行pic=plt.imread(x)
,而是继续执行下一行数据。行可能不是空的,而是空白或其他内容(如空格或制表符)?
for x in urls:
if x == '': # or some other option to check for the blank urls
continue
try:
pic = plt.imread(x)
... etc. with your code