#!/usr/bin/env python # -*- coding: utf-8 -*- import requests from io import BytesIO import cv2 import numpy as np from PIL import Image from fake_useragent import UserAgent def imread_from_url(url, seek_index=0, debug=False): image = None # URLから画像を取得 ua = UserAgent() header = {'User-Agent': str(ua.chrome)} response = requests.get(url, headers=header) # PILでURLの画像を読み込み try: image = Image.open(BytesIO(response.content)) except Exception as e: print(e) return None if debug: print(image) print(image.size) print(dir(image)) # アニメーションGIF等の複数枚ある画像はシーク if 'n_frames' in dir(image): if debug: print('n_frames', image.n_frames) print('seek_index', seek_index) if 0 <= seek_index < image.n_frames: image.seek(seek_index) elif seek_index < 0: print(&
从指定 URL 读取图像并以 OpenCV 格式返回的函数(从指定 URL 读取图像并使其可由 OpenCV 处理。)
最新推荐文章于 2024-04-07 08:44:34 发布
该Python脚本定义了一个函数`imread_from_url`,它从给定的URL下载图像,利用PIL库处理,然后转换为OpenCV可以处理的BGR格式。函数还包括对动画GIF的处理,支持指定帧数。示例代码中展示了如何显示从URL获取的图像。
摘要由CSDN通过智能技术生成