利用邻近法将一张图片的所有像素嵌入到另一张图片中
import cv2
import numpy as np
def point2point(x):
#幂函数
return np.power(x,2)
def log(base,x):
#对数运算
return np.log(x)/np.log(base)
def exp(x):
#指数运算
return np.exp(x)
def parse_from_img(img):
#图像的解析
img_h, img_w, _ = img.shape
res_img = np.zeros((160, 160, 3), np.uint8)
for m in range(0, 160):
for n in range(0, 160):
map_col = int(np.power(m,1.3))
map_row = int(np.power(n,1.3))
res_img[n, m] = img[map_row, map_col]
return res_img
def generate_img(big_img_path, small_img_path):
"""
Args:
big_img_path: 原始图片
small_img_path: 要隐藏的图片
Returns:
主要是利用插值算法将要隐藏的图片有规律的插入到原始图片中
"""
big_img = cv2.imread(big_img_path)
sml_img = cv2.imread(small_img_path)
big_img=cv2.resize(big_img,(800,800))
##要隐藏的图像
sml_img=cv2.resize(sml_img,(160,160))
dst_img = big_img.copy()
sml_h, sml_w, _ = sml_img.shape
for m in range(0, sml_w):
for n in range(0, sml_h):
##位置映射关系
map_col = int(np.power(m,1.3))
map_row = int(np.power(n,1.3))
dst_img[map_row, map_col] = sml_img[n, m]
# if map_col < int(np.power(sml_h,2)) and map_row
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,160)
y=np.power(x,1.3)
plt.plot(x,y)
plt.show()
def hh(base,x):
print('最小值',np.power(1,base))
print('最大值',np.power(x,base))
hh(1.3,160)
import cv2
import numpy as np
##文本转图片
def Text2Img(txt_fname, save_fname):
with open(txt_fname, "r", encoding="utf-8") as f:
text = f.read()
text_len = len(text)
img_h = 40
img_w = 40
img = np.zeros((img_h, img_w, 3))
x = 0
y = 0
for each_text in text:
idx = ord(each_text)
rgb = (0, (idx & 0xFF00) >> 8, idx & 0xFF)
img[y, x] = rgb
if x == img_w - 1:
x = 0
y += 1
else:
x += 1
cv2.imwrite(save_fname, img)
##图片转文本
def Img2Text(img_fname):
img = cv2.imread(img_fname)
height, width, _ = img.shape
text_list = []
for h in range(height):
for w in range(width):
R, G, B = img[h, w]
if R | G | B == 0:
break
idx = (G << 8) + B
text_list.append(chr(idx))
text = "".join(text_list)
with open("2.txt", "a", encoding="utf-8") as f:
f.write(text)
Text2Img('1.txt','hh.png')
Img2Text('hh.png')
参考文献:
写代码,必须要优雅! (cuijiahua.com)
99%的人都不知道照片隐藏的惊天秘密,实在是太优雅了!_哔哩哔哩_bilibili