如何使用 python 将图像数据类型从 windows sql 服务器转换为图像?
How to convert image data type from windows sql server to image using python?
我需要将 windows sql 服务器的压缩图像列数据转换为图像文件并将其保存到文件系统。
我正在使用 Python 2.7.2,Pillow on mac。
谢谢!
我所做的是在我的浏览器中打开你的要点,然后另存为...到一个名为 'chenchi.txt'.
的文件
然后我使用这个程序将十六进制编码的字符串转换为原始字节并将它们加载到 Pillow 中以从中制作图像:
from PIL import Image
import StringIO
import binascii
# In your case, 's' will be the string from the field
# in the database.
s = open("chenchi.txt").read()
# chop off the '0x' at the front.
s = s[2:]
# Decode it to binary.
binary = binascii.unhexlify(s)
# Wrap the bytes in a memory stream that can be read like a file.
bytes = StringIO.StringIO(binary)
# Use pillow to read the memory stream into an image (it autodetects the format).
im = Image.open(bytes)
# And show it. Or you could .save() it.
im.show()
使用 b16decode 对我有用。
我从 sql 导出的图像是这样的:'FFD8FFE000104A46494600010101004800480000FFE13...'
所以我不得不转换内容并保存到文件中。
source = 'data.dat'
destination = 'data.jpg'
with open(source, 'r') as f:
content = f.read()
content = base64.b16decode(content)
with open(destination, 'wb') as g:
g.write(content)
我需要将 windows sql 服务器的压缩图像列数据转换为图像文件并将其保存到文件系统。
我正在使用 Python 2.7.2,Pillow on mac。
谢谢!
我所做的是在我的浏览器中打开你的要点,然后另存为...到一个名为 'chenchi.txt'.
的文件然后我使用这个程序将十六进制编码的字符串转换为原始字节并将它们加载到 Pillow 中以从中制作图像:
from PIL import Image
import StringIO
import binascii
# In your case, 's' will be the string from the field
# in the database.
s = open("chenchi.txt").read()
# chop off the '0x' at the front.
s = s[2:]
# Decode it to binary.
binary = binascii.unhexlify(s)
# Wrap the bytes in a memory stream that can be read like a file.
bytes = StringIO.StringIO(binary)
# Use pillow to read the memory stream into an image (it autodetects the format).
im = Image.open(bytes)
# And show it. Or you could .save() it.
im.show()
使用 b16decode 对我有用。
我从 sql 导出的图像是这样的:'FFD8FFE000104A46494600010101004800480000FFE13...'
所以我不得不转换内容并保存到文件中。
source = 'data.dat'
destination = 'data.jpg'
with open(source, 'r') as f:
content = f.read()
content = base64.b16decode(content)
with open(destination, 'wb') as g:
g.write(content)