from PIL import Image
import numpy as np
path='out.png'
img = Image.open(path)
image_arr = np.array(img)
image_num=list(image_arr.reshape(image_arr.size))
while 1:
if image_num[-1]==0 and image_num[-2]==0:
if image_num[-3]==0:
image_num=image_num[:-2]
else:
image_num=image_num[:-1]
else:
break
cachedata=''
data=''''''
for x in image_num:
num1=hex(int(x))[2:]
if len(num1)<2:
num1='0'+num1
cachedata=cachedata+num1
for y in range (int(len(cachedata)/4)):
data=data+chr(int('0x'+cachedata[4*y:4*(y+1)],16))
with open('图解密文.txt','w',encoding='UTF-8') as file:
file.write(data)
file.close()
print('图转文完成,程序结束!')
|