python图片加密、解密、识别图片


python记录

 

import base64
from PIL import Image
import pytesseract

"""加密"""


def pic_encrypt(pic_path, txt_path):
    """
    :param txt_path: 图片地址
    :param pic_path: 加密后文件存放地址
    :return:
    """
    with open(pic_path, "rb") as f:
        content = f.read()
        file = base64.b64encode(content)
        with open(txt_path, "w") as ff:
            ff.write(str(file, "utf-8"))


"""解密"""


def pic_decrypt(txt_path, pic_path):
    """
    :param txt_path: 加密文件存放地址
    :param pic_path: 图片地址
    :return: 
    """
    with open(txt_path, "r") as f:
        content = f.read()
        pic = base64.b64decode(content)
        with open(pic_path, "wb") as ff:
            ff.write(pic)


"""识别图片"""


def pic_identify(pic_path):
    """
    :param pic_path: 图片地址
    :return:
    """
    file = Image.open(pic_path)
    return pytesseract.image_to_string(file, lang="eng")
 

 

 

 

 

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于面部识别技术的加密解密代码需要结合人脸识别库和加密解密算法库,以下是一个基于 Python 的示例代码: ```python import cv2 # 导入 OpenCV 库 from cryptography.fernet import Fernet # 导入 cryptography 库 # 加密解密密钥 key = Fernet.generate_key() cipher_suite = Fernet(key) # 加密函数 def encrypt(message): encrypted_text = cipher_suite.encrypt(message.encode('utf-8')) return encrypted_text # 解密函数 def decrypt(encrypted_text): decrypted_text = cipher_suite.decrypt(encrypted_text) return decrypted_text.decode('utf-8') # 人脸识别函数 def face_recognition(): face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: roi_gray = gray[y:y+h, x:x+w] roi_color = frame[y:y+h, x:x+w] # 将人脸图像转换成字符串 face_str = cv2.imencode('.jpg', roi_color)[1].tostring() # 加密人脸图像 encrypted_face_str = encrypt(face_str) # 解密人脸图像 decrypted_face_str = decrypt(encrypted_face_str) # 将解密后的人脸图像转换成 OpenCV 图像格式 nparr = np.fromstring(decrypted_face_str, np.uint8) img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # 在窗口中显示解密后的人脸图像 cv2.imshow('Decrypted Face', img) # 在窗口中显示原始图像 cv2.imshow('Original', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == '__main__': face_recognition() ``` 在此示例代码中,我们使用 OpenCV 库实现了人脸识别功能,将人脸图像转换成字符串,再使用 cryptography 库提供的加密解密算法进行加密解密操作。加密解密密钥使用 Fernet 算法生成,可以保证高强度的安全性。最后,我们将解密后的人脸图像显示在窗口中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值