python - 使用 Python 将 BLOB 图像转换为 PNG、JPG

标签 python blob

我在 Oracle 中有几个 blob 图像,所以我用 python 读取它们。 我可以使用我的代码正确读取和转换来自某个 table1 的图像,但是当更改为 table2 时,我无法执行相同的代码,因为出现以下错误。

cannot identify image file <_io.BytesIO object at 0x000000000C4520A0>

这是我的代码:

import pandas as pd
import cx_Oracle
from PIL import Image
#[connection to database with connecting string `conn`]
#[query to acces 1 single image]
result = pd.read_sql(query, conn) #connection to db
img = result["IMAGE"][0].read()  # reading the first BLOB result 
pre_img = io.BytesIO(img)
Image.open(pre_img)

此代码运行良好,所以唯一的问题是当我尝试从 table1 读取图像时。同样在 SQL 开发人员中,我可以对 table1 中的照片进行预可视化,但不能使用 table2。数据类型是 BLOB,如 Oracle 中的 describe(table) 所述。

img 的值可以查到here

sql can't read the image neither

最佳答案

这段代码对我有用

import base64
with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodebytes(img))

关于python - 使用 Python 将 BLOB 图像转换为 PNG、JPG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58804348/

相关文章:

java - 为Nifi编写流媒体服务时如何解决Java的空指针异常?

PHP sqlsrv 从数据库示例中插入/读取 blob(varbinary)字段

go - 在 Go 中将 Blob 转换为图像

python - 将字符串写入 csv

python - Threading.Times 不起作用

python - 从 Python 调用命令行实用程序

javascript - FileSaver.js 和 Blob.js - 更改目录

java - 如何从谷歌blobstore下载文件到android

mysql - 到 BLOB 还是不到 BLOB

python - turicreate 未知类型的图像分类器