简体   繁体   English

如何在不写入磁盘的情况下将 numpy 数组编码为 base64 编码的 PNG?

[英]How do I encode a numpy array to a base64-encoded PNG without writing to disk?

I want to convert an image, represented as a numpy array, into a base64-encoded PNG string, without writing it as a file to disk first.我想将表示为 numpy 数组的图像转换为 base64 编码的 PNG 字符串,而不是先将其作为文件写入磁盘。

The goal is to send this over HTTP, and then display the image in a browser using Javascript.目标是通过 HTTP 发送此图像,然后使用 Javascript 在浏览器中显示图像。 Basically trying to send a numpy image array to a browser without using disk.基本上试图在不使用磁盘的情况下将 numpy 图像数组发送到浏览器。

I'm very comfortable on the python side, and quite unfamiliar on the JS side.我在 python 方面很舒服,在 JS 方面很陌生。

I don't want to use disk, and I don't want to use tensorflow (which has a related helper API).我不想使用磁盘,也不想使用 tensorflow(它有一个相关的帮助 API)。

I have tried researching, but everything that converts a numpy array to a PNG seems to want to save it to disk.我尝试过研究,但是将 numpy 数组转换为 PNG 的所有东西似乎都想将其保存到磁盘。 I don't know how to avoid the disk I/O.我不知道如何避免磁盘 I/O。

Many functions that create files will accept an instance of io.StringIO or io.BytesIO instead of a filename or file object.许多创建文件的函数将接受io.StringIOio.BytesIO的实例,而不是文件名或文件 object。

Here's an example (in an ipython session) where I use numpngw.write_png (from numpngw , a package I wrote for creating PNG and animated PNG files from numpy arrays) to create a base64-encoded PNG file in a Python bytes object. Here's an example (in an ipython session) where I use numpngw.write_png (from numpngw , a package I wrote for creating PNG and animated PNG files from numpy arrays) to create a base64-encoded PNG file in a Python bytes object.

First, the imports:一、进口:

In [39]: import numpy as np                                                              

In [40]: from numpngw import write_png                                                   

In [41]: import io                                                                       

In [42]: import base64                                                                   

Create an image to work with:创建要使用的图像:

In [43]: img = np.random.randint(0, 256, size=(16, 16, 3)).astype(np.uint8)              

Create a BytesIO object, and write the PNG "file" to it.创建一个BytesIO object,并将 PNG“文件”写入其中。

In [44]: f = io.BytesIO()                                                                

In [45]: write_png(f, img)                                                               

f.getvalue() gets the bytes in the PNG "file". f.getvalue()获取 PNG“文件”中的字节。 Take a peek at the first 12 bytes:看看前 12 个字节:

In [46]: f.getvalue()[:12]                                                               
Out[46]: b'\x89PNG\r\n\x1a\n\x00\x00\x00\r'

Finally, use one of the many encodings available in the base64 library to encode the PNG data:最后,使用base64库中可用的多种编码之一对 PNG 数据进行编码:

In [47]: b64 = base64.b64encode(f.getvalue())                                            

In [48]: b64                                                                             
Out[48]: b'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAAInRFWHRDcmVhdGlvbiBUaW1lADIwMTktMTAtMThUMDI6NDY6MTRayfRM7AAAAFF0RVh0U29mdHdhcmUAbnVtcG5ndyAodmVyc2lvbiAwLjAuOS5kZXYwKSwgaHR0cHM6Ly9naXRodWIuY29tL1dhcnJlbldlY2tlc3Nlci9udW1wbmd3aGhwYwAAAxtJREFUeJwBEAPv/AChDcGkLZrMNsaw4b5CRQk90PGcAA05N+8BH3rlSGZVLOn/vleX3mclnyfC1TXEOJYAfLtv7F3kSclGhnJDU93XTTWA2RcAutMYkETtG2kUZMWvbSDAxm0uSm8y0Do6gNQpANrN6Xt3cIoybwKclwb9jEQoIrdE6acAd4IoaaSEuH2mUIZ0tQ5plhBU61nCj7jBqQBQFH2x7wd5HR3IA5jo/jOrlalRhR2xgYRboyOl+MhIxExUhNjqEpz66SGJQNbUrWoAYQIWoEgFXiewRwACPUd35eo0DxDWYcrONkmj1fBGe03/JirvaJLQ79F7uAG2vrCXACLBrKntrzxPLoFa7Wgzl1nNEdo7Fenn7Jd+d2TAAMFELJTkhXAhUpQe3NyIjY9y+QD0Lmn11S/UYf4BcTqE3lMbbQ2iQpS1kcKOtbC2zjx7KovsK1M+JRIClOUzATlVnp8ANJS/zfP2ZTzR4dKjV2XfF3bxFs9Kk9qb+ilBHlBUSr2VA6CHxxVtcuvlkeiDvSA6AFQjA+Hd5vsgjgAcEUPkJx2SgqazSjXcSFFyQHqhgif47p3WOVM2lv0rWc4kSRwE5QCXsYTzOWusqZPbHxZSIWDvPjZcESUQ6FKZ4Pa0XFfdD2rLxp/4PF6w33k75h6uHpMAJWGC0yVRXBRW726WM9BAoX7+KjhLaJ6FhJ5aeRYL092uW8DxFwgPTq/evQg6PgOZAOWyz+saW+gMZukrMeZyQLQcQ5QMzX0sFXBtT7rAs9sEKUIx/HLH6wuTCd+zkzbvbAD6cNATGxklmLG15gwnLIX5bSCFD3wTAhDZwaXTdvUDt6LGc7oma5QfuVKUPGXtm7MAIghIuKbNd6S1wsqG6Ga/cB+wMJYklc5AG4dGzFnK+RbpACrQtchNn9v6o2zXJxjaAIaIY6VW1Db6VNeP/tXKHrusaqy1lssoVdSNGiDMo17vmM4EsTkqSkNMFAMct78+WQCb6h4CwetL6rzaQvUx03qW4GBgPbIEMe2MIS9l7D53VKdtUjhpl28zOqI5bW3YFN4cXne4IEUrgwAAAABJRU5ErkJggg=='

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 AppEngine 中存储 base64 编码的图像? - How do I store a base64-encoded image in AppEngine? 如何反序列化base64编码的数据并将其与DRF一起使用 - How to deserialize base64-encoded data and use it with DRF 如何在Python中播放base64编码的声音 - How to play base64-encoded sound in Python 如何在Python中将base64编码的字符串转换为没有磁盘I / O的文件(用于HTTP请求) - How to convert base64 encoded string to file without disk I/O in Python (for HTTP requests) 如何将 Base64DataURL 转换为 base64 编码的图像字节 - How to convert Base64DataURL to base64-encoded image bytes 如何在base64字符串和numpy数组之间进行编码和解码? - How to encode and decode between base64 string and numpy array? 如何从发布的base64编码图像创建MongoDB / mongoengine ImageField? - How to Create a MongoDB/mongoengine ImageField from POSTed base64-encoded image? 如何在磁盘上保留编码的numpy数组? - How to preserve encoded numpy array on disk? 如何加载存储在S3中的numpy字节数组而不将其写入磁盘? - How can I load a numpy byte array that is stored in an S3 without writing it to disk? 在ReportLab生成的PDF中包含base64编码的图像 - Including base64-encoded image in ReportLab-generated PDF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM