用户:
憨憨白熊查看:0 回复:2 评论:0 创建时间:2021-02-07T21:11:15
吧编程课上学的图像加密和解密课程的代码稍微做了一丢丢的改动
代码如下(又是超级非常懒得写注释):
from tkinter import *
from easygui import fileopenbox as OPEN
from easygui import filesavebox as SAVE
from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import random
import sys
import json
class MainWindow(Tk):
def __init__(self):
super().__init__()
self.setup()
def setup(self):
self.resizable(0, 1)
self.geometry("1000x300")
self.title("图像加密解密器")
self.encrypt_btn = Button(self, text='加密图片', font=("楷体", 40),
width=10, height=2, command=self.encrypt_func, bg='#F0D200', activebackground='#FFFFAF',)
self.decrypt_btn = Button(self, text='解密图片', font=("楷体", 40),
width=10, height=2, command=self.decrypt_func, bg='#F0D200', activebackground='#FFFFAF',)
self.encrypt_btn.place(x=100, y=80)
self.decrypt_btn.place(x=620, y=80)
def encrypt_func(self):
path = OPEN(title="请选择要加密的图片:", filetypes=[
"*.png", '*.jpg', '*.bmp'], default='*.png')
save = SAVE(title='请选择要保存加密图片的路径:',default='加密图片.png')
key_path = SAVE(title='请选择要保存加密密钥的路径:',default='加密密钥.json')
to_list, size = self.pic_to_list(path)
en_list, key = self.encryption(to_list, size[0], size[1])
self.list_to_pic(en_list, save+'.png')
with open(key_path,'w',encoding='utf-8-sig') as f:
json.dump(key,f)
def decrypt_func(self):
encrypt_path = OPEN(title="请选择加密图片的路径:", filetypes=[
"*.png"], default='*.png')
key_path = OPEN(title="请选择密钥文件的路径:", filetypes=[
"*.json"], default='*.json')
save = SAVE(title='请选择要保存解密图片的路径:',default='解密图片.png')
with open(key_path,'r',encoding='utf-8-sig') as f:
key = json.load(f)
de_pic, de_size = self.pic_to_list(encrypt_path)
de_pic_list = self.decryption(de_pic, de_size[0], de_size[1],key)
self.list_to_pic(de_pic_list,save)
def get_key(self, row, col):
keys = []
for i in range(row):
keys.append(random.randint(1, col))
return keys
def pic_to_list(self, path):
pic = Image.open(path).convert("RGB")
width, height = pic.size
pic_array = np.array(pic)
pic_list = pic_array.tolist()
return pic_list, (width, height)
def list_to_pic(self, RGB_list, path):
en_array = np.array(RGB_list)
en_pic = Image.fromarray(np.uint8(RGB_list))
en_pic.save(path)
def decryption(self, pic_list, width, height, key_list):
for i in range(height):
key = key_list[i]
de_key = width-key
list_b = pic_list[i][:de_key]
list_a = pic_list[i][de_key:]
pic_list[i][:width-de_key] = list_a
pic_list[i][width-de_key:] = list_b
return pic_list
def encryption(self, picture, width, height):
keys = self.get_key(width, height)
for i in range(height):
key = keys[i]
list_a = picture[i][:key]
list_b = picture[i][key:]
picture[i][:width-key] = list_b
picture[i][width-key:] = list_a
return picture, keys
if __name__ == '__main__':
Screen = MainWindow()
Screen.mainloop()
sys.exit()
课上写的代码:
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
import random
def pic_to_list(path):
pic = Image.open(path).convert("RGB")
width, height = pic.size
pic_array = np.array(pic)
pic_list = pic_array.tolist()
return pic_list, (width, height)
def get_key(row, col):
keys = []
for i in range(row):
keys.append(random.randint(1, col))
return keys
def encryption(picture, width, height):
keys = get_key(width, height)
for i in range(height):
key = keys[i]
list_a = picture[i][:key]
list_b = picture[i][key:]
picture[i][:width-key] = list_b
picture[i][width-key:] = list_a
return picture, keys
def decryption(pic_list, width, height, key_list):
for i in range(height):
key = key_list[i]
de_key = width-key
list_b = pic_list[i][:de_key]
list_a = pic_list[i][de_key:]
pic_list[i][:width-de_key] = list_a
pic_list[i][width-de_key:] = list_b
return pic_list
def list_to_pic(RGB_list, path):
en_array = np.array(RGB_list)
en_pic = Image.fromarray(np.uint8(RGB_list))
en_pic.save(path)
# 加密
to_list, size = pic_to_list("图片.png")
en_list, key = encryption(to_list, size[0], size[1])
list_to_pic(en_list, "加密图片.png")
# 解密
de_pic, de_size = pic_to_list("加密图片.png")
de_pic_list = decryption(de_pic, de_size[0], de_size[1], key)
list_to_pic(de_pic_list, "解密图片.png")
img = Image.open("解密图片.png")
img.show(
需要的图片:
为什么安装不了库呢????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
点赞0
评论