惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

Lan小站-嗯,不错! - 转载文章

Chrome:此扩展程序不再受支持,因此已停用,Mac解决方案 - Lan小站-嗯,不错! 哈夫曼树(最优二叉树)的概念以及构造 - Lan小站-嗯,不错! 利用js去除无限debugger - Lan小站-嗯,不错! 在pycharm中为函数或方法以及参数添加注释 - Lan小站-嗯,不错! 关于Python的面试题 - Lan小站-嗯,不错! Python面试宝典-基础篇-2020 - Lan小站-嗯,不错! 数据库事务的概念及其实现原理 - Lan小站-嗯,不错! vite2.1 最新alias别名设置方式 - Lan小站-嗯,不错! 论文格式 - Lan小站-嗯,不错!
python PIL/cv2/base64相互转换 - Lan小站-嗯,不错!
Lan · 2021-05-28 · via Lan小站-嗯,不错! - 转载文章

Lan

本文最后更新于2021年07月01日,已超过1809天没有更新,若内容或图片失效,请留言反馈。

PIL和cv2是python中两个常用的图像处理库,PIL一般是anaconda自带的,cv2是opencv的python版本。base64在网络传输图片的时候经常用到。

PIL读取、保存图片方法

from PIL import Image
    img = Image.open(img_path)
    img.save(img_path2)

cv2读取、保存图片方法

import cv2
img = cv2.imread(img_path)
cv2.imwrite(img_path2, img)

图片文件打开为base64

import base64

def img_base64(img_path):
    with open(img_path,"rb") as f:
        base64_str = base64.b64encode(f.read())
    return base64_str

1、PIL和cv2转换

PIL转cv2

import cv2
from PIL import Image
import numpy as np

def pil_cv2(img_path):
    image = Image.open(img_path)
    img = cv2.cvtColor(np.asarray(image),cv2.COLOR_RGB2BGR)
    return img

cv2转PIL

import cv2
from PIL import Image

def cv2_pil(img_path):
    image = cv2.imread(img_path)
    image = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB))
    return image

2、PIL和base64转换

##PIL转base64

import base64
from io import BytesIO

def pil_base64(image):
    img_buffer = BytesIO()
    image.save(img_buffer, format='JPEG')
    byte_data = img_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    return base64_str

base64转PIL

import base64
from io import BytesIO
from PIL import Image

def base64_pil(base64_str):
    image = base64.b64decode(base64_str)
    image = BytesIO(image)
    image = Image.open(image)
    return image

cv2和base64转换

import cv2def 
cv2_base64(image):
    base64_str = cv2.imencode('.jpg',image)[1].tostring()
    base64_str = base64.b64encode(base64_str)return base64_str 

base64转cv2

import base64
import numpy as np
import cv2def 
base64_cv2(base64_str):
    imgString = base64.b64decode(base64_str)
    nparr = np.fromstring(imgString,np.uint8) 
    image = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
    return image