說明:藍色=命令名稱python
淺綠=命令參數segmentfault
淺藍=選項windows
紫色=目錄ide
系統環境:windows XP SP3學習
python版本:Python 2.7.3測試
之前同事給了一個光棍節小遊戲(http://segmentfault.com/game),最後一關是把二進制文本轉換成圖片。最近學習python,發現python很簡單,代碼以下:spa
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- #Author:left_left
- import binascii
- import re
- def jpg2bin(data):
- b64 = binascii.b2a_base64(data)
- j = 0
- f = open('test.txt', 'w')
- for i in b64:
- f.write("".join([bin(int(binascii.b2a_hex(i), 16))[2:].rjust(8,'0'), " "]))
- j += 1
- if j % 6 == 0:
- f.write('\n')
- f.close()
- print "jpg to bin ok"
- def bin2jpg(data):
- result = re.split(r'[ \n]', data)
- b64 = ""
- for i in result:
- if i:
- hex_src = str(hex(int(i, 2)))[2:].rjust(2,"0")
- b64 = "".join([b64, binascii.a2b_hex(hex_src)])
- bin_data = binascii.a2b_base64(b64)
- f = open("new.jpg", 'wb')
- f.write(bin_data)
- f.close()
- print "jpg to bin ok"
- def main():
- f = open("test.jpg", "rb")
- data = f.read()
- jpg2bin(data)
- f.close()
- f = open("test.txt")
- data = f.read()
- bin2jpg(data)
- f.close()
- if __name__ == "__main__":
- main()
測試:3d
文件目錄結構如圖:blog
執行代碼:遊戲
目錄結構:
test文本內容: