我有一个十六进制字符串, 像这样
fd377a585a000004e6d6b4460200210116000000742fe5a3efe90feffe5d0028184b0486481b1ee7672d75b37b7281b783d542d4748d2226fe470fca883712b09ccca24194361cfa20ee5211467187890de03a4b906b5e81f85d886374a78de3d86b69b1120d812351201b4bd4b24cafeff61ff8198ee136d3609bcbbf41c34a10cdc12e91b065f6a7d6d03449af52ce4b927eaa1700c573b1a9dd2a5df871f4e8484dc22e2b81078e267e9cc282452289d89c203e15f76cbae5619f4526adaeeae83c9a2a299d7926418d4f738ff3b01d7bf75da6d7ea179fcfcfdf77e8b3b74c9f65518405fcdd7292973dfc0d391d1a1bfbe47320f71b98792b4ca90f0169f9d1c8a331087d177d84f14e2f2abcabead7ee87306c1bb6f5d398e78e005ea883696e82f1ded474546b21323c0025826811de223a9cd6acbfcb671afb7b9f5da97c707025d3b2c2da2d1cb9ba414d4abcd1e8f496278d96b79c6d314c8ac0cf9632e917158e7603b6ace0551a8c1a1368268eba1becb4f18b7b660bc73bc600181816344bbc3932628bd6df67248a7f792902ee6aef9ba5fe5e4b16a8d2cd509a04a83101a78a002012881508 ...等
它需要转换为二进制文件(有些图片)
答案 0 :(得分:0)
您可以尝试通过int(“ fd”,16)将此字符串的每个两个字符转换为整数,并使用Image.fromarray(np.uint8(matrix)创建一个需要图像大小的矩阵)命令。 我猜存在更快,更有效的方法,但是您应该尝试一下。
import numpy as np
from PIL import Image
string = "00112233445566778899aabbccddeeff......"
lenght = len(string)
""" These may change according to your desired/needed image shape """
myshape = int(np.sqrt(lenght/2)) # Assuming square output
shape = (myshape, myshape)
""" ------ """
pix = np.asmatrix(np.zeros(shape))
str2bytes = []
for i in range(int(lenght/2)):
str2bytes.append(int(string[2*i:(2*i)+2],16))
pix = np.array(str2bytes)
pix = pix.reshape(shape[0],shape[1])
pic = Image.fromarray(np.uint8(pix))
pic.show()