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

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

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