解码base64图像

2024-09-28 12:14:16 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的rtf文档中,我想从字符串中提取图像: 字符串如下:

    \pard\pard\qc{\*\shppict{\pict\pngblip\picw320\pich192\picwgoal0\pichgoal0 
    89504e470d0a1a0a0000000d4948445200000140000000c00802000000fa352d9100000e2949444[.....]6c4f0000000049454e44ae426082
}}

问题: 1) 这真的是base64吗?在

2)如何使用下面的代码解码。?在

^{pr2}$

完整的rtf文本(保存为.rtf时显示图像)位于:

http://hastebin.com/axabazaroc.tex


Tags: 字符串代码文档图像base64qcrtfpict
1条回答
网友
1楼 · 发布于 2024-09-28 12:14:16

不,那不是Base64编码的数据。它是十六进制的。从Wikipedia article on the RTF format

RTF supports inclusion of JPEG, Portable Network Graphics (PNG), Enhanced Metafile (EMF), Windows Metafile (WMF), Apple PICT, Windows Device-dependent bitmap, Windows Device Independent bitmap and OS/2 Metafile picture types in hexadecimal (the default) or binary format in a RTF file.

^{} function将为您解码回二进制图像数据;这里有一个PNG图像:

>>> # data contains the hex data from your link, newlines removed
...
>>> from binascii import unhexlify
>>> r = unhexlify(data)
>>> r[:20]
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01@'
>>> from imghdr import test_png
>>> test_png(r, None)
'png'

当然,\pngblip条目就是其中的一条线索。我不会在这里包括图像,这是一个相当单调的8位320x192黑色矩形。在

相关问题 更多 >

    热门问题