-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_5.py
132 lines (112 loc) · 5.44 KB
/
main_5.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author: Yeryo
# datetime: 2022/10/17 19:02
# func: 三十图
import cv2 as cv
import numpy as np
import encode as e
import decode as d
# import tinify
# import compress as cp
# import compress_CV
# from compress_all import CV, PIL, Tinify
# import operator
# from typing import List, Union
def data_init() -> list:
"""
初始化要嵌入的数据,存储至列表中返回
:return:
"""
try:
path_text = [[r"D:\need\txt\thirty\share_all_{}_{}.txt".format(i, j) for j in range(6)] for i in range(5)]
data_raw = [[e.get_text(path_text[i][j]) for j in range(6)] for i in range(5)]
except IOError as err:
print(f"没有找到文件或读取文件失败。Error is {err}")
else:
print("读取数据文件成功!")
return data_raw
def start_thirty(path_raw, path_save):
"""
将path_raw中的数图片拷贝三十份,然后将三十份数据分别写入,再初步压缩,得到三十张图片,分别存储于对应路径path_save(见compress_all)。
:param path_raw: 输入图片的路径(jpg)
:param path_save: 保存图片的路径(png)
:return:
"""
img_raw = cv.imread(path_raw, cv.IMREAD_UNCHANGED)
a, b, c = img_raw.shape[2], img_raw.shape[1], img_raw.shape[0]
# 【方法一】用numpy声明三维数组
# img_new_png: list[np.ndarray] = [np.array([[[int] * a] * b] * c)]
img_new_png = [np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray,
np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray] # 必须显式声明个数。还不能直接*5,若更多怎么写???
for i in range(30):
img_new_png[i]: np.ndarray = np.array([[[int] * a] * b] * c)
# 【方法二】用list声明三维数组ten\
# img_new_png: list[list[list[int]]] = [[[0 for col in range(a)] for row in range(b)] for row1 in range(c)]
print("\n>>>开始读取数据文件和图片...")
data = data_init()
try:
img = [cv.imread(path_raw, cv.IMREAD_UNCHANGED) for i in range(30)]
except IOError as err:
print(f"没有找到文件或读取文件失败,图片路径不要有中文哦。Error is {err}")
else:
print("读取图片成功!")
pix_raw: np.ndarray = np.array([[int] * 6] * 5)
pix_new: np.ndarray = np.array([[int] * 6] * 5)
img_new = [[np.ndarray([1])] * 6] * 5
for i in range(5):
for j in range(6):
pix_raw[i][j] = e.get_pix(img[i], data[i][j])
pix_new[i][j] = e.encode(pix_raw[i][j], data[i][j])
img_new[i][j] = e.encode_write(img[i], pix_new[i][j], data[i][j])
cv.imwrite(path_save[i][j], img_new[i][j], [cv.IMWRITE_PNG_COMPRESSION, 9])
# cv.imencode(path_save[i], img_new[i])[1].tofile(path_save[i])
print("\n>>>开始显示图片...")
# cv.imshow("new", img_new[0])
# # print("在这个地方卡死,程序停不下来。。。")
# cv.waitKey(0)
# # print("在这个地方卡死,程序停不下来。。。")
# cv.destroyAllWindows()
def start_one(num_1, num_2, path_raw, path_save):
"""
对上述图片进行复写操作。一次只输入一张图片,只复写对应的一份数据,再初步压缩,存储于对应路径(见compress_all)。
用于压缩图片里的循环压缩时调用(compress_all.out_png)
:param num_1: 输入图片的编号
:param num_2: 输入图片的编号
:param path_raw: 输入图片的路径(jpg)
:param path_save: 保存图片的路径(png)
:return:
"""
print("\n>>>开始读取数据文件和图片...")
data = data_init()
try:
# path_img = r'd:\need\all_picture\test.jpg'
# 这里一定要保证传入的是压缩后的图片path_raw而非上面指定的path_img,否则图片得不到压缩
img = cv.imread(path_raw, cv.IMREAD_UNCHANGED)
except IOError as err:
print(f"没有找到文件或读取文件失败,图片路径不要有中文哦。Error is {err}")
else:
print("读取图片成功!")
# 将对应数据写入对应编号的图片中
pix_raw = e.get_pix(img, data[num_1][num_2])
pix_new = e.encode(pix_raw, data[num_1][num_2])
img_new = e.encode_write(img, pix_new, data[num_1][num_2])
# png初步压缩
cv.imwrite(path_save, img_new, [cv.IMWRITE_PNG_COMPRESSION, 9])
# 验证压缩后的图片是否仍可恢复数据
img_new_png = cv.imread(path_save, cv.IMREAD_UNCHANGED)
d.decode(e.to_ascii(data[num_1][num_2]), img_new_png)
if __name__ == '__main__':
path_png_save1 = [["d:/need/all_picture/threshold/thirty/share_{}_{}.png".format(i, j) for j in range(6)]
for i in range(5)]
path_jpg_save1 = [["d:/need/all_picture/threshold/thirty/share_{}_{}.jpg".format(i, j) for j in range(6)]
for i in range(5)]
path_jpg_raw1 = r'd:/need/all_picture/test.jpg'
start_thirty(path_jpg_raw1, path_png_save1)
data = data_init()
img_raw = cv.imread(path_jpg_raw1, cv.IMREAD_UNCHANGED)
d.get_rec_thirty(img_raw, data, path_png_save1)