opencv-python 网络图片获取 并 编辑

Python3

import cv2
import requests
import time
from PIL import Image
from io import BytesIO

# set None proxy
import os
os.environ['no_proxy'] = '*' 

# Internet picture
url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1565165195&di=0ae43289971ee5b3cdc36eb9c9612a0a&imgtype=jpg&er=1&src=http%3A%2F%2Fvpic.video.qq.com%2F3388556%2Fx0540ujyh6i_ori_3.jpg"

# get file(of Internet picture)
for i in range(10):
    start=time.time()
    file = requests.get(url)
    img = cv2.imdecode(np.fromstring(file.content, np.uint8), 1)    #file.content 是读取的远程文件的字节流
    print('time',time.time()-start)

#using plt draw picture
plt.figure() # 设置画布
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()

    
# using PIL.Image to read Internet file of picture
## image = Image.open(BytesIO(file.content))
## image.show()

# using cv2 to read Internet file of picture
color_img = img
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 获取信息:行,列,通道,像素数目,图像数据类型
print(color_img.shape, color_img.size, color_img.dtype)
print(gray_img.shape, gray_img.size, gray_img.dtype)

取消全局代理 os方法

import os
os.environ['no_proxy'] = '*' 
print(requests.get("https://www.baidu.com"))  
# session method
session = requests.Session()
session.trust_env = False
response = session.get('http://www.stackoverflow.com')

你可能感兴趣的:(opencv-python 网络图片获取 并 编辑)