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')
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,691评论 1 32
  • 我想讲一个小Z的故事。 小Z年过三十,是单位里的一个普通职工,姿色平平,不甘平庸。和单位里有背景的同事比较,她后台...
    王律师阅读 7,883评论 0 0
  • 方小暗阅读 523评论 0 51
  • 默契。
    阿友阅读 146评论 0 0
  • 伍章【离歌的前奏】 一·不舍 云卷云舒,星月交替,反复在这片天空上演着。被夜晚黑暗,被霞光染红,这片天空一天天的重...
    诗琢阅读 298评论 1 1

友情链接更多精彩内容