最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python中判断本地和网络图片的分辨率等基本信息

图片 crifan 713浏览 0评论
去写爬虫期间,发现有些图片地址,虽然是有的,但是实际上是1×1的空图片:
https://images-na.ssl-images-amazon.com/images/P/141972486X.jpg
而另外一些图片地址,才是正常的:
https://images-na.ssl-images-amazon.com/images/P/0061992275.jpg
所以希望:
用Python代码,能通过图片的url地址,检测出这些图片的基本参数信息,比如分辨率等等
python 检测在线图片属性
Python读取图片属性信息的实现方法_python_脚本之家
Python图像处理(Pillow/PIL)入门 – 小龙在线 – CSDN博客
python 能够实现读取图片的属性信息吗 比如 像素 经纬度-CSDN论坛
Python图像处理库:Pillow 初级教程 – Python – 伯乐在线
wangbinyq/pillow_example
图片EXIF信息获取,在线获取图片EXIF信息
https://www.sojson.com/image/exif.html
Python图像处理库:Pillow 初级教程 – 云+社区 – 腾讯云
去找找PIL
python pil
PIL、Pillow安装使用方法 – pcat – 博客园
Pillow — Pillow (PIL Fork) 5.3.0 documentation
Tutorial — Pillow (PIL Fork) 5.3.0 documentation
PIL – 廖雪峰的官方网站
PIL · Python 3零起点教程 · 看云
➜  crawler_fablexile_book git:(master) ✗ pip install pillow
Collecting pillow
  Downloading https://files.pythonhosted.org/packages/2e/f6/e6d56ec19fae0b76932a343355c7a37198fd8e76c26bc2f2779fc85b4684/Pillow-5.3.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.6MB)
    100% |████████████████████████████████| 3.6MB 2.1MB/s
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
pyopenssl 18.0.0 has requirement six>=1.5.2, but you'll have six 1.4.1 which is incompatible.
Installing collected packages: pillow
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/PIL'
Consider using the `--user` option or check the permissions.

You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
➜  crawler_fablexile_book git:(master) ✗ pip3 install pillow --user
Collecting pillow
  Cache entry deserialization failed, entry ignored
  Downloading https://files.pythonhosted.org/packages/d1/21/bef2816809fac16754e07ed935469fc65f42ced1a94766de7c804179311d/Pillow-5.3.0-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.6MB)
    100% |████████████████████████████████| 3.6MB 725kB/s
Installing collected packages: pillow
Successfully installed pillow-5.3.0
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
➜  crawler_fablexile_book git:(master) ✗ pip3 show pillow
Name: Pillow
Version: 5.3.0
Summary: Python Imaging Library (Fork)
Home-page: http://python-pillow.org
Author: Alex Clark (Fork Author)
Author-email: aclark@aclark.net
License: Standard PIL License
Location: /Users/crifan/Library/Python/3.6/lib/python/site-packages
Requires:
Required-by:
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
➜  crawler_fablexile_book git:(master) ✗ which pip3
/usr/local/bin/pip3
然后去调试代码:
from PIL import Image

TestImgPathList = [
    "https://images-na.ssl-images-amazon.com/images/P/141972486X.jpg", # 1x1
    "https://images-na.ssl-images-amazon.com/images/P/0061992275.jpg", # normal
]

def detectImgageInfo():
    for curImgPath in TestImgPathList:
        print("curImgPath=%s" % curImgPath)
        img = Image.open(curImgPath)
        print("img=%s" % img)
        imgSize = img.size
        print("imgSize=%s" % imgSize)

if __name__ == "__main__":
    detectImgageInfo()
直接报错:
发生异常: FileNotFoundError
python pillow load image from url
How do I read image data from a URL in Python? – Stack Overflow
Python – how to read an image from a URL? – Stack Overflow
python – Opening Images from URL using Pillow and urllib? – Stack Overflow
python – How do I open an image from the internet in PIL? – Stack Overflow
2.8.0 — Pillow (PIL Fork) 3.0.0 documentation
【已解决】Python的Pillow打开图片出错:OSError cannot identify image file _io.BytesIO object at
再去参考:
Python3学习笔记(urllib模块的使用) – Data&Truth – 博客园
urllib.request — Extensible library for opening URLs — Python 3.7.1 documentation
urllib.error — Exception classes raised by urllib.request — Python 3.7.1 documentation
去完善异常处理
【总结】
最后用如下代码:
from PIL import Image
# import requests
# import cStringIO
# import urllib2 as urllib
import urllib

TestImgUrlList = [
    "https://images-na.ssl-images-amazon.com/images/P/141972486X.jpg", # 1x1
    "https://images-na.ssl-images-amazon.com/images/P/0061992275.jpg", # normal
]

# def getImgRawData(imgUrl):
#     respImgStream = requests.get(imgUrl, stream=True)
#     respImgStream.raw.decode_content = True
#     imgRawData = respImgStream.raw
#     print("imgRawData=%s" % imgRawData)
#     return imgRawData


def getImgFile(imgUrl):
    print("getImgFile: imgUrl=%s" % imgUrl)
    getOk = False
    imgFileOrErrMsg = ""
    try:
        imgFileObj = urllib.request.urlopen(imgUrl)
        print("imgFileObj=%s" % imgFileObj)
        getOk = True
        imgFileOrErrMsg = imgFileObj
    except urllib.error.URLError as urlErr:
        getOk = False
        imgFileOrErrMsg = str(urlErr.reason)

    return getOk, imgFileOrErrMsg


def detectImgageInfoFromUrl():
    for curImgUrl in TestImgUrlList:
        print("curImgUrl=%s" % curImgUrl)
        # imgRawData = getImgRawData(curImgUrl)
        # img = Image.open(imgRawData)

        getOk, imgFileOrErrMsg = getImgFile(curImgUrl)
        print("getOk=%s, imgFileOrErrMsg=%s" % (getOk, imgFileOrErrMsg))
        if getOk:
            imgFile = imgFileOrErrMsg
            print("imgFile=%s" % imgFile)
            img = Image.open(imgFile)
            print("img=%s" % img)
            (imgWidth, imgHeight) = img.size
            print("imgWidth=%s, imgHeight=%s" % (imgWidth, imgHeight)) # 1x1  / 354x500
        else:
            print("Get image url %s failed: %s" %(curImgUrl, imgFileOrErrMsg))

if __name__ == "__main__":
    detectImgageInfoFromUrl()
输出:
curImgUrl=https://images-na.ssl-images-amazon.com/images/P/141972486X.jpg
getImgFile: imgUrl=https://images-na.ssl-images-amazon.com/images/P/141972486X.jpg
imgFileObj=<http.client.HTTPResponse object at 0x10e4042e8>
getOk=True, imgFileOrErrMsg=<http.client.HTTPResponse object at 0x10e4042e8>
imgFile=<http.client.HTTPResponse object at 0x10e4042e8>
img=<PIL.GifImagePlugin.GifImageFile image mode=P size=1x1 at 0x10E176240>
imgWidth=1, imgHeight=1
curImgUrl=https://images-na.ssl-images-amazon.com/images/P/0061992275.jpg
getImgFile: imgUrl=https://images-na.ssl-images-amazon.com/images/P/0061992275.jpg
imgFileObj=<http.client.HTTPResponse object at 0x10e1aa208>
getOk=True, imgFileOrErrMsg=<http.client.HTTPResponse object at 0x10e1aa208>
imgFile=<http.client.HTTPResponse object at 0x10e1aa208>
img=<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=354x500 at 0x10E417978>
imgWidth=354, imgHeight=500
即可得到希望的结果了。
此处1×1不正常的图片的各种属性:
正常的图片的各种属性:

转载请注明:在路上 » 【已解决】Python中判断本地和网络图片的分辨率等基本信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.163 seconds, using 23.33MB memory