python 二进制读写文件
#-*- coding: utf-8 -*-
f = open('f:/text.bmp','rb')
filedata = f.read()
filesize = f.tell()
f.close()
filedata2 = bytearray(filedata)
width = filedata2[18]
height = filedata2[22]
print('width:', width)
print('height:', height) # less than 255 , width and height have 4 bytes
print('pix:', width * height)
print('filesize:', filesize)
f2 = open('f:/t2.bmp','wb')
change = 0
index = 54
loop = 0
while index < filesize - 2:
loop += 1
r = filedata2[index]
g = filedata2[index+1]
b = filedata2[index+2]
threshold = 110
if (r < threshold) and (g < threshold) and (b < threshold):
bytes = bytearray(3)
bytes[0] = 0
bytes[1] = 255
bytes[2] = 0
filedata2[index:index+3] = bytes
else:
bytes = bytearray(3)
bytes[0] = bytes[1] = bytes[2] = 255
filedata2[index:index+3] = bytes
change += 1
index += 3
f2.write(filedata2)
f2.close()
print ('change:',change)
print ('loop:',loop)
python 二进制读写文件的更多相关文章
- python二进制读写文件
#coding=gbk ''' Created on 2014-5-7 ''' import os.path inputPath = './input.txt' outPath = './out.tx ...
- Python 3 读写文件的简单方法!
Python 3 读写文件的简单方法! a = open('test.txt','w') 这行代码创建了一个名为test的文本文档,模式是写入(模式分为三种,w代表写入,r代表阅读,a代表在尾行添加) ...
- Python:读写文件(I/O) | 组织文件
1. I/O 概述 程序与用户交互涉及到程序的输入输出(I/O) 一种类型是字符串,通过input() 和 print() 函数以及数据类型转换类函数如(int()),实现数据的输入输出. 另一种类 ...
- 笨方法学python之读写文件、open函数的用法
一.python读写文件相关知识点 close:关闭文件 read:读取文件的内容//你可以把结果赋给一个变量 readline:只读取文件中的一行 truncate 美 /trʌŋ'ket/ :清空 ...
- MATLAB 通过二进制读写文件
这几天在做信息隐藏方面的应用,在读写文本文件时耗费许久,故特别的上网学习一二,这里给出一常用读写,其他的都类似. 很多时候,我们都要将一个.txt以二进制方式读出来,操作后在恢复成.txt文本. ma ...
- python二进制读写及特殊码同步
python对二进制文件的操作需要使用bytes类,直接写入整数是不行的,如果试图使用f.write(123)向文件中以二进制写入123,结果提示参数不是bytes类型. import os impo ...
- python查找读写文件
import os ''' 跟据文件名称,后缀查找指定文件 path:传入的路径 filename:要查找的文件名 suffix:要查找的文件后缀 return :返回查找的文件路径 ''' file ...
- python之读写文件
1. 读取文件数据,文件必须存在才可以读且如要读取的文件不和当前.py在同一个包下,需要特别指定此文件路径才行 f=open('test.txt',encoding='utf-8')#填写文件路径,打 ...
- python 多进程读写文件
import time from multiprocessing import Process, JoinableQueue, cpu_count import csv ####处理一条数据的方法 d ...
随机推荐
- [转]解决WebClient或HttpWebRequest首次连接缓慢问题
http://blog.csdn.net/rrrfff/article/details/6170653?reload 设置代理为空: <?xml version="1.0"? ...
- 使用VS2013在WIN8.1上运行gaclib的hello world
首先:gaclib的官网是http://www.gaclib.net/ 需要了解更多信息的请自己去官网,我也是刚刚研究 第一步 下载gaclib的源码 这些文件是运行程序所必须的 第二步 ...
- Fragment之间的通信
在本节中,你会学到 1.定义接口 2.实现接口 3.将消息传递给fragment 为了重用Fragment UI 组件,在设计中你应该通过定义每一个fragemnt自己的layout和行为,让frag ...
- JPA oneToMany 级联更新
oneToMany 使用: 示例:Employee与Phone为例. 1.类定义如下: package com.vrvwh.wh01.domain; import javax.persistence. ...
- paip.提升性能---jvm java 工具使用.
paip.提升性能---jvm java 工具使用. 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn ...
- Asset Catalog Help (二)---Creating an Asset Catalog
Creating an Asset Catalog Create an asset catalog to simplify management of your app’s images. 创建一个a ...
- PHP--------memcache技术
新事物的产生都不是偶然的 1.为什么会产生memcache? 在大型的电商web页面上,数据量庞大,大量用户需要同时访问海量的数据,为了提高用户的访问效果,如何才能让页面加载最快,更友好的展示到用户面 ...
- Leetcode 168 Excel Sheet Column Title 进制数转化
题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- class Solution { public: string ...
- AngularJS(一)
什么是AngularJS[双向数据绑定:从界面的操作能实时反映到数据,数据的变更能实时展现到界面.]?1.AngularJS 使得开发现代的单一页面应用程序(SPAs:Single Page Appl ...
- JavaScript 语句 数组与冒泡排序法
数组 数组:不管是什么类型,都可以进行存放.存放是有一定顺序的. 顺序:索引号从0开始. 1.需要先对数组进行初始化 var array //数组名称 = new Arrary() //Array 注 ...