1、将文本,图片转换为二进制
toBinary.py
import io
import chardet
import numpy as np
from PIL import Image
def txt2byte(msg):
return msg.encode(encoding="utf-8")
def image2byte(pic):
img = Image.open(pic)
img_bytes = io.BytesIO()
img.save(img_bytes, format="JPEG")
img_bytes = img_bytes.getvalue()
return img_bytes
def byte2Array(byte):
'''
二进制字节流数据转化为 Array 格式
'''
byteStream = io.BytesIO(byte)
image = Image.open(byteStream)
arr=np.asarray(image)
print('arr_shape: ', arr.shape)
print('arr_value: ', arr)
return arr
2、得到索引串
extract.py
n= 6
dt = []
dindex = []
index=[]
dict={'0':0,'1':0}
def str2index(str):
x, y, k = 0, 0, 0
for k in str:
dt.append(k)
for i in range(6):
dindex=dt[i:30:6]
for key in dindex:
dict[key]+=1
if(dict['0']>dict['1']):
index.append('0')
else:
index.append('1')
dict['0'] = 0
dict['1'] = 0
print("得到索引串:"+"".join(index))
return "".join(index)
3、连接数据库
conSql.py
import pymysql
def connect(str):
conn = pymysql.connect(
host='localhost',
user='root',
password='asdfgh',
db='index-msg',
charset='utf8',
)
cur = conn.cursor()
sql = "select msg from `message` where `index`="+str+";"
cur.execute(sql)
print(cur.fetchone())
cur.close()
conn.close()
|