如今來實現如何將一個圖片數字轉爲二進制的數據,並保存到爲本中python
代碼在github託管git
#!/usr/bin/env python3 # -*- coding: utf-8 -*- ''' 圖片處理成32x32的二進制數據 ''' from PIL import Image # 打開要處理的圖像 img_src = Image.open('a.png') size = img_src.size # 轉換圖片的模式爲RGBA img_src = img_src.convert('RGB') with open('1.txt','wb') as f: for i in range(size[0]): for j in range(size[1]): tmp = img_src.getpixel((j,i)) if(tmp != (255,255,255)): f.write(b'1') else: f.write(b'0')