I bet you didn't know - Python image encryption


Let's talk about the principle of image first

哥几个又来学习了【手动狗头】
A picture is not just a picture, it is composed of pixels one by one. The more pixels we have, the clearer the image will be. This is what we often call high definition. This is how ultra high definition comes from. The pixel size of the horizontal and vertical coordinates also determines the size of our picture
. Obviously, it determines that our picture is a square
insert image description here
. Then, in fact, the picture we see is not just a picture, it is superimposed by three pictures (you can Seen as a three-dimensional array), that is, the three primary colors we often say
R (red) G (green) B (blue) can form almost all colors
R
insert image description here
G
insert image description here
B
insert image description here
we are doing tricks on this

1. Install cv2

pip install opencv-python

2. Encryption

import numpy as np
import cv2 as cv


#主要思路就是生成一个和图像一样大的三维数组去做异或
t_path=r"C:\Users\twy\PycharmProjects\1\2.jpg"#文件路径
lena=cv.imread(t_path)#读取图像的编码
w,h,c=lena.shape#获取 宽度 高度  有几张图像叠加(一般都是三张 RGB)
key=np.random.randint(0,256,size=[w,h,c],dtype=np.uint8)#在 宽度 高度 叠加 这样一个三位数组里面给每一个值生成0~255的随机值
#加密
encode=cv.bitwise_xor(lena,key)#异或处理
cv.imshow("encode1",encode)#显示
cv.imshow("222",cv.bitwise_xor(encode,key))#再做一次异或就还原了
cv.waitKey(2000)

insert image description here

3. Code

t_path=r"C:\Users\twy\PycharmProjects\1\2.jpg"#文件路径
#这个原理也比较简单,就是由某一个像素点的颜色来代替其他周围像素点的颜色
lena=cv.imread(t_path)#读取图片编码
w,h,c=lena.shape#获取 宽度 高度  有几张图像叠加(一般都是三张 RGB)
for m in range(0,1000):#我们打码的范围
    for n in range(0,1000):
        if m%30==0 and n%30==0:#每30个取一个颜色,让周围的颜色都等于这一个像素
            for i in range(0,30):
                for j in range (0,30):
                    (b,g,r)=lena[m,n]#m,n是不变的(b,g,r)三层
                    lena[m+i,n+j]=(b,g,r)#只有i,j在变
cv.imshow("1",lena)
cv.waitKey(200000)
cv.destroyAllWindows()

insert image description here

If we change the range... what's the fun [manual dog head], we can also get the position of the mouse, and then... eh, code manually? Do you want to combine face recognition with Xiaotang? ? ? Hey, automatically code your face hahaha, Python is so fun!

Guess you like

Origin blog.csdn.net/weixin_52521533/article/details/123442432
Recommended