以简洁(相对)全面的方式处理base64编码图像文件的解码。
libmagic
(或其他一些python库)尽可能确定编码文件的mime类型,以指示正确的文件类型输出。base64
包是为此而制作的,除非我离开基地。我创建了一个名为Converter
的python类来处理任务,具有以下功能
__init__(self, file="none", str_object="none")
file="none"
- 文件路径,作为基于文件解码的字符串。str_object="none"
- 裸编码字符串。convert_ascii_to_byte_stream(self)
base64
对象convert_byte_stream_to_jpg(self)
convert_ascii_to_byte_stream
,然后关闭该文件。@staticmethod
strip_all_whitespace(s)
__main__.py
from Converter import *
c = Converter('ascii_image.txt', 'none') # create Converter instance
# print(c.get_encoding_type()) # attempt to get encoding
c.convert_byte_stream_to_jpg() # output the decoded data to image file
Converter('none', 'XyZxYzXyZxYzXyZxYz ...)
,那么转换就会完美无缺。显然写入文件存在问题。<img>
源属性的前缀,data:image/jpg;base64,
必须在解码前被删除。因此,使用ascii_image.txt
的测试文件已经过编辑以反映这一点。libmagic
,python-libmagic
,python-magic-bin
,file-magic
以及各种其他扩展程序包的一系列复杂方法进展。他们中的大多数只是创建了关于缺少依赖项,编译器错误和其他问题的意大利面错误消息流。任何建议都将不胜感激。Converter
Python类from io import *
import base64
# import magic
class Converter:
def __init__(self, file="none", str_object="none"):
self.file = file
self.str_object = str_object
if file == "none" or "":
self.isFromStringInput = True
self.asciiString = str_object
else:
f = open(file, 'r')
self.isFromStringInput = False
# this was the source of the pad error
#self.asciiString = f.read(file.__len__())
# changed to ... duh!
self.asciiString = f.read()
f.close()
def convert_byte_stream_to_jpg(self):
f = open('ascii_image.jpg', 'wb')
f.write(self.convert_ascii_to_byte_stream())
f.close()
return
def convert_ascii_to_byte_stream(self):
return base64.b64decode(self.strip_all_whitespace(self.asciiString))
@staticmethod
def strip_all_whitespace(s):
return s.replace('\n', ' ').replace('\r', '').replace('\t', '').replace(' ', '')
# def get_encoding_type(self):
# m = magic.MAGIC_MIME
# m.from_bytes(self.convert_ascii_to_byte_stream(), 'little')
# return m.file('./' + self.file)
Traceback (most recent call last):
File "/Users/auser/PycharmProjects/btobin/__main__.py", line 5, in <module>
c.convert_byte_stream_to_jpg() # output the decoded data to image file
File "/Users/auser/PycharmProjects/btobin/Converter.py", line 22, in convert_byte_stream_to_jpg
f.write(self.convert_ascii_to_byte_stream())
File "/Users/auser/PycharmProjects/btobin/Converter.py", line 27, in convert_ascii_to_byte_stream
return base64.b64decode(self.strip_all_whitespace(self.asciiString))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/base64.py", line 87, in b64decode
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
如果将以下<img>
标记放在html文件中并使用浏览器打开它们,则会看到正确解码的图像。
以下是Decoded Image
<img src="data:image/jpg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNr
eQABAAQAAAAeAAD/4QOPaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYm
VnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHht
bG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZSA1LjYtYzAxNC
A3OS4xNTY3OTcsIDIwMTQvMDgvMjAtMDk6NTM6MDIgICAgICAgICI+IDxyZGY6UkRGIHhtbG5z
OnJkZj0iaHR0cDovL3d3dy53My5vcmcvMTk5OS8wMi8yMi1yZGYtc3ludGF4LW5zIyI+IDxyZG
Y6RGVzY3JpcHRpb24gcmRmOmFib3V0PSIiIHhtbG5zOnhtcE1NPSJodHRwOi8vbnMuYWRvYmUu
Y29tL3hhcC8xLjAvbW0vIiB4bWxuczpzdFJlZj0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS
4wL3NUeXBlL1Jlc291cmNlUmVmIyIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hh
cC8xLjAvIiB4bXBNTTpPcmlnaW5hbERvY3VtZW50SUQ9In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