简体   繁体   English

解码来自rtf的base64图像

[英]decoding base64 images from rtf

In my rtf document, I want to extract image from string: The string is like this: 在我的rtf文档中,我想从字符串中提取图像:字符串如下所示:

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

questions: 1) is this really base64? 问题:1)这真的是base64吗?

2) How to decode it using below code.? 2)如何使用以下代码对其进行解码?

import base64

imgData = b"base64code00from007aove007string00bcox007idont007know007where007it007starts007and007ends"

with open("imageToSave.png", "wb") as fh:
    fh.write(base64.decodestring(imgData))

Full rtf text(which when saved as .rtf shows image) is at: 完整的rtf文本(保存为.rtf时显示图像)位于:

http://hastebin.com/axabazaroc.tex http://hastebin.com/axabazaroc.tex

No, that's not Base64-encoded data. 不,那不是Base64编码的数据。 It is hexadecimal . 它是十六进制的 From the Wikipedia article on the RTF format : Wikipedia上有关RTF格式的文章

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. RTF支持以十六进制包含JPEG,可移植网络图形(PNG),增强型图元文件(EMF),Windows图元文件(WMF),Apple PICT,与Windows设备相关的位图,与Windows设备无关的位图和OS / 2图元文件图片类型)或RTF文件中的二进制格式。

The binascii.unhexlify() function will decode that back to binary image data for you; binascii.unhexlify()函数将为您解码回二进制图像数据。 you have a PNG image here: 您在这里有一个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'

but of course the \\pngblip entry was a clue there. 但是当然, \\pngblip条目在那里是一个线索。 I won't include the image here, it is a rather dull 8-bit 320x192 black rectangle. 我不会在此处添加图片,它是一个相当呆板的8位320x192黑色矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM