#!/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(&