OCR-PIL.Image与Base64 String的互相转换

2023-05-16

1. 基本环境

  • py2: python2.7.13
  • py3: python3.6.2
  • PIL: pip(2/3) install pillow, PIL库已不再维护,而pillow是PIL的一个分支,如今已超越PIL

2. Convert PIL.Image to Base64 String

  • py2 :先使用CStringIO.StringIO把图片内容转为二进制流,再进行base64编码
# -*- coding: utf-8 -*-

import base64
from cStringIO import StringIO

# pip2 install pillow
from PIL import Image


def image_to_base64(image_path):
    img = Image.open(image_path)
    output_buffer = StringIO()
    img.save(output_buffer, format='JPEG')
    binary_data = output_buffer.getvalue()
    base64_data = base64.b64encode(binary_data)
    return base64_data
  • py3:python3中没有cStringIO,对应的是io,但却不能使用io.StringIO来处理图片,它用来处理文本的IO操作,处理图片的应该是io.BytesIO
import base64
from io import BytesIO

# pip3 install pillow
from PIL import Image

# 若img.save()报错 cannot write mode RGBA as JPEG
# 则img = Image.open(image_path).convert('RGB')
def image_to_base64(image_path):
    img = Image.open(image_path)
    output_buffer = BytesIO()
    img.save(output_buffer, format='JPEG')
    byte_data = output_buffer.getvalue()
    base64_str = base64.b64encode(byte_data)
    return base64_str

3. Convert Base64 String to PIL.Image

  • py2:
# -*- coding: utf-8 -*-

import re
import base64
from cStringIO import StringIO

from PIL import Image


def base64_to_image(base64_str, image_path=None):
    base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
    binary_data = base64.b64decode(base64_data)
    img_data = StringIO(binary_data)
    img = Image.open(img_data)
    if image_path:
        img.save(image_path)
    return img
  • py3:
import re
import base64
from io import BytesIO

from PIL import Image


def base64_to_image(base64_str, image_path=None):
    base64_data = re.sub('^data:image/.+;base64,', '', base64_str)
    byte_data = base64.b64decode(base64_data)
    image_data = BytesIO(byte_data)
    img = Image.open(image_data)
    if image_path:
        img.save(image_path)
    return img

4. 参考网址

[1] https://stackoverflow.com/questions/16065694/is-it-possible-to-create-encodeb64-from-image-object

[2] https://stackoverflow.com/questions/31826335/how-to-convert-pil-image-image-object-to-base64-string

[3] https://stackoverflow.com/questions/26070547/decoding-base64-from-post-to-use-in-pil

 

 

 

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OCR-PIL.Image与Base64 String的互相转换 的相关文章

  • 直方图均衡结果

    I am trying to code histogram equalization by my self but the results are different from the built in function in matlab
  • minAreaRect OpenCV 返回的裁剪矩形 [Python]

    minAreaRectOpenCV 中返回一个旋转的矩形 如何裁剪矩形内图像的这部分 boxPoints返回旋转矩形的角点的坐标 以便可以通过循环框内的点来访问像素 但是在 Python 中是否有更快的裁剪方法 EDIT See code在
  • 在 C++ 中查找精确的字符串匹配

    这是我用来检测 txt 文件中一行中的字符串的代码 int main std ifstream file C log txt std string line while file eof while std getline file lin
  • libxml2 xmlChar * 到 std::wstring

    libxml2似乎将所有字符串存储在 UTF 8 中 如xmlChar xmlChar This is a basic byte in an UTF 8 encoded string It s unsigned allowing to pi
  • 将字符串转换为双精度 - VB

    VB中有没有一种有效的方法来检查字符串是否可以转换为双精度型 我目前正在尝试将字符串转换为双精度型 然后查看它是否引发异常 但这似乎减慢了我的申请速度 Try if number then format it current CDbl x
  • 在Python中从字节串创建zip文件对象?

    我有一个字节串 保证它是 zip 文件的字节表示形式 知道这个字节串后 如何在 Python 中创建 zip 文件对象 Use io BytesIO https docs python org 3 library io html io By
  • 左对齐图像和居中文本在 div 内的同一级别?

    HTML br div class UpperTitle img src align left CableSolve Web Dashboard Version 0 1 1 div br CSS UpperTitle text align
  • Matlab 图像数据的 hist 函数

    我是 Matlab 新手 我想制作自己的函数 与 imhist 显示图像数据的直方图 完成相同的工作 但我对此完全是新手 我不知道如何做开发这样的功能 我开始做一些东西 但它非常不完整 function output args myhist
  • 在Android内存中存储gif图像

    我对安卓还很陌生 我想将图像保存到内存中 然后从内存中检索图像并将其加载到图像视图中 我已使用以下代码成功将图像存储在内存中 void saveImage String fileName img cnt jpg File file new
  • 测量两个字符串之间相似性的有效方法是什么? (编辑距离使堆栈太深)

    所以 我从这个开始 http en wikibooks org wiki Algorithm Implementation Strings Levenshtein distance Ruby http en wikibooks org wi
  • 生成逗号分隔值

    假设我有一个字符串集合 foo bar xyz 我想从列表中生成一个逗号分隔的值 如下所示 foo bar xyz 请注意末尾缺少 我知道有多种方法可以生成此内容 使用 for 循环和 string Format 或 StringBuild
  • 拆分/标记化/扫描字符串并注意引号

    Java中是否有默认 简单的方法来分割字符串 但要注意引号或其他符号 例如 给定以下文本 There s a man that live next door in my neighborhood and he gets me down Ob
  • WinForms - 加载表单时如何使用 PaintEventArgs 运行函数?

    我试图理解图形 在 Graphics FromImage 文档中 它有这样的示例 private void FromImageImage PaintEventArgs e Create image Image imageFile Image
  • 奇怪的跨线程 UI 错误

    我正在编写一个 WinForms 应用程序 它有两种模式 控制台或 GUI 同一解决方案中的三个项目 一个用于控制台应用程序 一个用于 UI 表单 第三个用于保存两个界面也将连接的逻辑 控制台应用程序运行绝对流畅 保存用户选择的模型 它有一
  • C 支持原始字符串吗?

    C 11 添加了对原始字符串文字的支持 例如 R foo A weird string foo C有这样的东西吗 如果有 标准是什么版本 C11 如果没有 有谁知道它是否正在计划中以及是否有编译器支持它 C有这样的东西吗 如果有 标准是什么
  • 将 std::string 从 C++ DLL 返回到 c# 程序 -> 指定给 RtlFreeHeap 的地址无效

    在我的 C DLL 中的函数中 我将 std string 返回到我的 C 应用程序 它几乎看起来像这样 std string g DllName MyDLL extern C THUNDER API const char stdcall
  • 如何在 R 中将字符串解析为层次结构或树

    有没有办法将表示组的字符串解析为 R 中的层次结构 假设我的小组结构如下 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 3 1 1 3 1 1 1 3 2 1 1 3 3 1 2 1 2 1 1 2 1 1 1 2 1 2 1
  • 获取两个字符串之间的公共部分c# [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我需要的是获取两个单词之间的共同部分并获取差异 例子 场景1 word1 感言 word2 Test 将返回 公共部分Test 不同之
  • 如何检测 Java 字符串中的 unicode 字符?

    假设我有一个包含 的字符串 我如何找到所有这些 un icode 字符 我应该测试他们的代码吗 我该怎么做呢 例如 给定字符串 A X 我想将其转换为 AYXY 我想对其他 unicode 字符做同样的事情 并且我不想将它们存储在某种翻译映
  • 如何在Python中按AaB而不是ABa顺序对字符串进行排序

    我正在尝试对字符串进行排序 为 punnetsquare 制作基因型 我目前的实现是 unsorted genotype ABaB sorted genotype sorted list unsorted genotype sorted s

随机推荐