[英]base64 to numpy array in python
I have a pdf file which I converted into base64 and then decoded it into binary format ie(010101010) like this.我有一个 pdf 文件,我将其转换为 base64,然后像这样将其解码为二进制格式 ie(010101010)。 But I want this base64 encoded data to be in a NumPy array.
但我希望此 base64 编码数据位于 NumPy 数组中。 So that it represents in raster binary form.
以便它以光栅二进制形式表示。
Below is the code I have tried so far.以下是我迄今为止尝试过的代码。 It is giving me into binary numbers but how to convert it into a NumPy 2D array.
它给了我二进制数,但如何将其转换为 NumPy 2D 数组。
with open("sample.pdf", "rb") as pdf_file:
encoded_string = base64.b64encode(pdf_file.read())
decoded = base64.decodebytes(encoded_string)
decoded_binary_val= "".join(["{:08b}".format(x) for x in decoded])
print(decoded_binary_val)
The result of the same is in (010101010111101011101010111101011111010101011) this format.相同的结果是 (010101010111101011101010111101011111010101011) 这种格式。 But I want a NumPy 2D array ie (r*c)
但我想要一个 NumPy 2D 数组,即 (r*c)
You can do你可以做
arr = np.array([c for c in decoded_binary_val])
This will give you a numpy array of strings.这将为您提供一个 numpy 字符串数组。 If you want numbers you can do:
如果你想要数字,你可以这样做:
arr = np.array([int(c) for c in decoded_binary_val])
Edit - Another option:编辑 - 另一种选择:
If you want string values you can also just cast to list, like this:如果您想要字符串值,您也可以将其强制转换为列表,如下所示:
arr = np.array(list(decoded_binary_val))
But then if you want numbers you probably need to map:但是如果你想要数字,你可能需要映射:
arr = np.array(list(map(int, decoded_binary_val)))
I haven't been able to measure a difference in performance, so you'll have to check with your setup.我无法测量性能差异,因此您必须检查您的设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.