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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

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