python如何读取二进制文件为图片_python读取图片中的数据再用二进制做位运算后写入文件...

#!/usr/bin/python#encode.pyimportsysimportos.pathimportosimportglobimportbinasciidefcur_file_dir():path=sys.path[0]ifos.path.isdir(path):returnpathelifos.path.isfile(path...

#!/usr/bin/python

#encode.py

import sys

import os.path

import os

import glob

import binascii

def cur_file_dir():

path = sys.path[0]

if os.path.isdir(path):

return path

elif os.path.isfile(path):

return os.path.dirname(path)

def encode(fileName):

file = open(fileName, 'rb+')

try:

all_the_text = file.read()

for byte in all_the_text:

temp = binascii.b2a_hex(byte)

#coding

byte = binascii.a2b_hex(temp)

file.seek(0, 0)

file.write("sandglass")

file.write(all_the_text)

file.flush()

finally:

file.close()

return None

def codePic(path):

print path

for root, dirs, files in os.walk( path ):

for file in files:

if len(file and ".png") or len(file and ".jpg"):

fullpath = os.path.join(root, file)

print fullpath

encode(fullpath)

return None

codePic(cur_file_dir())

os.system("pause")

查找文件和读写文件都实现了,但是完全搞不懂python里面的二进制和字符串之间该怎么转换和运算

#coding中是想将读出来的字符转成二进制后再做位运算,再转回字符

求大神帮忙看看该怎么实现

展开

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值