缺点就是有点慢,毕竟遍历的次数比较多
import binascii
import struct
import sys
filename = sys.argv[1]
crcbp = open(filename, "rb").read() # 打开图片
crc32frombp = int(crcbp[29:33].hex(), 16) # 读取图片中的CRC校验值
print(f"图片中的CRC校验值:\t{crc32frombp}")
for i in range(4000): # 宽度1-4000进行枚举(如果图片太大,可以适当自行增大)
for j in range(4000): # 高度1-4000进行枚举(如果图片太大,可以适当自行增大)
data = crcbp[12:16] + struct.pack('>i', i) + struct.pack('>i', j) + crcbp[24:29]
crc32 = binascii.crc32(data) & 0xffffffff
# print(crc32)
if crc32 == crc32frombp: # 计算当图片大小为i:j时的CRC校验值,与图片中的CRC比较,当相同,则图片大小已经确定
print(f"图片大小(宽×高):\t{i} × {j}")
print(f'换算后hex:\t\t{hex(i)}, {hex(j)}')
这个方法有时候结果不太准确,可以两个方法相互印证
import struct
import sys
import zlib
filename = sys.argv[1]
with open(filename, 'rb') as f:
all_b = f.read()
crc32key = int(all_b[29:33].hex(), 16)
data = bytearray(all_b[12:29])
n = 4095
for w in range(n):
width = bytearray(struct.pack('>i', w))
for h in range(n):
height = bytearray(struct.pack('>i', h))
for x in range(4):
data[x + 4] = width[x]
data[x + 8] = height[x]
crc32result = zlib.crc32(data)
if crc32result == crc32key:
print("宽为:", end="")
print(width)
print("高为:", end="")
print(height)
exit(0)