相关疑难解决方法(0)

从POST解码base64以在PIL中使用

我在Flask中创建了一个简单的API,它接受在base64中编码的图像,然后将其解码以便使用Pillow进行进一步处理.

我看了一些例子(1,2,3),我觉得我得到的过程的要点,但我不断收到一个错误的枕头无法读取我给它的字符串.

这是我到目前为止所得到的:

import cStringIO
from PIL import Image
import base64

data = request.form
image_string = cStringIO.StringIO(base64.b64decode(data['img']))
image = Image.open(image_string)
Run Code Online (Sandbox Code Playgroud)

这给出了错误:

IOError: cannot identify image file <cStringIO.StringIO object at 0x10f84c7a0>
Run Code Online (Sandbox Code Playgroud)

python base64 python-imaging-library flask pillow

40
推荐指数
3
解决办法
3万
查看次数

PIL open()方法不能与BytesIO一起使用

出于某种原因,当我尝试从BytesIO蒸汽制作图像时,它无法识别图像.这是我的代码:

from PIL import Image, ImageGrab
from io import BytesIO

i = ImageGrab.grab()
i.resize((1280, 720))
output = BytesIO()
i.save(output, format = "JPEG")
output.flush()
print(isinstance(Image.open(output), Image.Image))
Run Code Online (Sandbox Code Playgroud)

它抛出的错误的堆栈跟踪:

Traceback (most recent call last):
  File "C:/Users/Natecat/PycharmProjects/Python/test.py", line 9, in <module>
    print(isinstance(Image.open(output), Image.Image))
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 2126, in open
    % (filename if filename else fp))
IOError: cannot identify image file <_io.BytesIO object at 0x02394DB0>
Run Code Online (Sandbox Code Playgroud)

我正在使用PIL的Pillow实现.

python python-imaging-library pillow

16
推荐指数
1
解决办法
2万
查看次数

在 Jupyter Notebook 中打开 base64 字符串图像而不保存

我有以下图片:

在此处输入图片说明

转换为base64,看起来是这样的:

import base64
filename = 'image.jpg'  

with open(filename, "rb") as image_file:
    encoded_string = base64.b64encode(image_file.read())
    image_file.close()

with open("encoded_string.txt", "w") as converted_file:
    converted_file.write(str(encoded_string))
    converted_file.close()
Run Code Online (Sandbox Code Playgroud)

在此处下载输出文件 (base64):https : //file.io/NXV7v4

现在,我的问题是:

如何检索转换后的图像并将其显示在 jupyter notebook 中,而无需存储它?

基于 [this][2] 问题,我尝试了:

from PIL import Image
import cv2
import io


# Take in base64 string and return cv image
def stringToRGB(base64_string):
    imgdata = base64.b64decode(str(base64_string))
    image = Image.open(io.BytesIO(imgdata))
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)

stringToRGB(encoded_string)
Run Code Online (Sandbox Code Playgroud)

但我得到了:

---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-43-2564770fa4af> in <module>()
----> 1 stringToRGB(encoded_string)

<ipython-input-42-538f457423e9> …
Run Code Online (Sandbox Code Playgroud)

python string base64 python-3.x

7
推荐指数
3
解决办法
2818
查看次数

BytesIO对象到映像

我试图在程序中使用枕头将摄像机的字节字符串保存到文件中。这是一个示例,该示例包含一个来自我的相机的小原始字节字符串,该字符串应使用LSB和12位表示分辨率为10x5像素的灰度图像:

import io
from PIL import Image

rawBytes = b'_\x00`\x00[\x00^\x00`\x00`\x00\\\x00\\\x00Z\x00\\\x00_\x00[\x00\\\x00\\\x00`\x00]\x00\\\x00^\x00_\x00\\\x00\\\x00]\x00]\x00_\x00]\x00]\x00Z\x00\\\x00^\x00\\\x00Z\x00^\x00_\x00]\x00^\x00Z\x00\\\x00Z\x00\\\x00]\x00_\x00]\x00^\x00Z\x00[\x00[\x00X\x00]\x00]\x00Z\x00'
rawIO = io.BytesIO(rawBytes)
rawIO.seek(0)
byteImg = Image.open(rawIO)
byteImg.save('test.png', 'PNG')
Run Code Online (Sandbox Code Playgroud)

但是我在第7行(带有Image.open)收到以下错误:

OSError: cannot identify image file <_io.BytesIO object at 0x00000000041FC9A8>
Run Code Online (Sandbox Code Playgroud)

Pillow的文档暗示这是要走的路。

我试图从中应用解决方案

但无法正常运作。为什么这不起作用?

python io python-imaging-library python-3.x

5
推荐指数
1
解决办法
6093
查看次数

使用 ipywidgets.FileUpload() 在 Jupyter Notebook 中上传图像

我一直在努力使用 ipywidgets.FileUpload() 将图像上传到我的 Jupyter 笔记本中,它对于文本文件可以正常工作,但对于二进制文件,内容总是损坏。特别是对于图像,它们始终存储为“数据”,因此 keras.preprocessing.image.load_img() 无法使用它们。我正在使用的代码是:

import ipywidgets as widgets

uploader = widgets.FileUpload()
uploader

for name, file_info in uploader.value.items():
    with open(name, 'wb') as fp:
        fp.write(file_info['content'])
Run Code Online (Sandbox Code Playgroud)

我尝试了多种解决方案,但没有任何方法可以处理二进制文件,任何提示或帮助都会受到好评。我的环境是GCP AI Platform Notebooks(JupyterLabs 1.2.16,ipywidgets 7.5.1),我一直使用的参考文献是:

jupyter-notebook ipywidgets gcp-ai-platform-notebook

4
推荐指数
1
解决办法
4757
查看次数