zoukankan      html  css  js  c++  java
  • python手记(52)

    python将信息加密进图片

    从图片中解密信息

    >>> runfile(r'K: estpro est1.py', wdir=r'K: estpro')

    http://blog.csdn.net/myhaspl
    myhaspl@qq.com


    loading  ...
    正在处理中 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    >>> 

    往图片加密信息

    >>> runfile(r'K: estpro est.py', wdir=r'K: estpro')
    http://blog.csdn.net/myhaspl
    myhaspl@qq.com


    loading  ...
    正在处理中 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
    >>> 


    加密python代码

     

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    #code:myhaspl@qq.com
    #http://blog.csdn.net/myhaspl
    import cv2
    import numpy as np
    
    
    fn1="he1.jpg"
    fn2="he2.jpg"
    fn3="secret.png"
    redcolor=(0, 0, 255)
    if __name__ == '__main__':
        print 'http://blog.csdn.net/myhaspl'
        print 'myhaspl@qq.com'
        print
        print 'loading  ...'
        print u'正在处理中',
        img1 = cv2.imread(fn1)
        img2 = cv2.imread(fn2)
    
        w=img1.shape[1]
        h=img1.shape[0]  
       
    
        #加上需要隐藏的消息
        cv2.putText(img1,"http://blog.csdn.net/myhaspl", (20,20),cv2.FONT_HERSHEY_PLAIN, 1.0, redcolor, thickness = 1)
        cv2.putText(img1,"code by myhaspl:myhaspl@qq.com", (20,60),cv2.FONT_HERSHEY_PLAIN, 1.0, redcolor, thickness = 1) 
        cv2.putText(img1,"Installing Python is generally easy. ", (1,90),cv2.FONT_HERSHEY_PLAIN, 1, redcolor, thickness = 1)     
        
        cv2.namedWindow('img1')     
        cv2.imshow('img1', img1)   
        cv2.namedWindow('img2-1')     
        cv2.imshow('img2-1', img2)    
        #处理隐藏目标图
        #将所有蓝色值变成奇数
        for j in xrange(0,h):
            for i in xrange(0,w):
                if (img2[j,i,0]%2)==1:
                    img2[j,i,0]=img2[j,i,0]-1
            print '.',
            mirror_w=w/2
        #读取源图,并将信息写入目标图
        for j in xrange(0,h):
            for i in xrange(0,w):
                if (img1[j,i,0],img1[j,i,1],img1[j,i,2])==redcolor:
                    img2[j,i,0]=img2[j,i,0]+1
            print '.',
        #保存修改后的目标图,并显示
        cv2.namedWindow('img2-2')     
        cv2.imshow('img2-2', img2)         
        cv2.imwrite(fn3, img2)      
        cv2.waitKey()
        cv2.destroyAllWindows()    
        
        
    

    本博客所有内容是原创,未经书面许可,严禁任何形式的转载

    http://blog.csdn.net/u010255642


    加密过程的效果图


    解密的python代码

     

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    #code:myhaspl@qq.com
    #http://blog.csdn.net/myhaspl
    #解码文件
    import cv2
    import numpy as np
    fn="secret.png"
    if __name__ == '__main__':
        print 'http://blog.csdn.net/myhaspl'
        print 'myhaspl@qq.com'
        print
        print 'loading  ...'
        print u'正在处理中',
        img = cv2.imread(fn)
        w=img.shape[1]
        h=img.shape[0]  
        imginfo =np.zeros((h,w,3), np.uint8)   
        for j in xrange(0,h):
            for i in xrange(0,w):
                if (img[j,i,0]%2)==1:
                    imginfo[j,i,1]=255
            print '.',  
        cv2.imshow('info', imginfo)         
        cv2.imwrite(fn, imginfo)      
        cv2.waitKey()
        cv2.destroyAllWindows()         

    解密后的效果图



  • 相关阅读:
    管理员技术(三): 配置静态网络地址、 使用yum软件源 、 升级Linux内核、查找并处理文件、查找并提取文件内容
    管理员技术(二): 访问练习用虚拟机、 命令行基础技巧 、 挂载并访问光盘设备、ls列表及文档创建、复制删除移动
    管理员技术(一):装机预备技能、安装一台RHEL7虚拟机、使用RHEL7图形桌面、Linux命令行基本操作
    基础(三):yum(RedHat系列)和apt-get(Debian系列 )用法及区别
    基础(二):Linux系统/etc/init.d目录和/etc/rc.local脚本
    基础(一):SCSI硬盘与IDE硬盘有什么区别
    高级运维(六):源码安装Redis缓存服务、常用Redis数据库操作指令、配置Redis主从服务器
    错题
    count 【mysql】
    自连接和子查询
  • 原文地址:https://www.cnblogs.com/riskyer/p/3289831.html
Copyright © 2011-2022 走看看