我在 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
最佳答案
这段代码对我有用
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/