解析hdr图像文件的python实现
如题
import cv2
import numpy as np
def rgbe2float(rgbe):
res = np.zeros((rgbe.shape[0],rgbe.shape[1],3))
p = rgbe[:,:,3]>0
m = 2.0**(rgbe[:,:,3][p]-136.0)
res[:,:,0][p] = rgbe[:,:,0][p] * m
res[:,:,1][p] = rgbe[:,:,1][p] * m
res[:,:,2][p] = rgbe[:,:,2][p] * m
return res def readHdr(fileName = 'belgium.hdr'):
fileinfo = {}
with open(fileName, 'rb') as fd:
tline = fd.readline().strip()
if len(tline)<3 or tline[:2] != '#?':
print 'invalid header'
return
fileinfo['identifier'] = tline[2:] tline = fd.readline().strip()
while tline:
n = tline.find('=')
if n>0:
fileinfo[tline[:n].strip()] = tline[n+1:].strip()
tline = fd.readline().strip() tline = fd.readline().strip().split(' ')
fileinfo['Ysign'] = tline[0][0]
fileinfo['height'] = int(tline[1])
fileinfo['Xsign'] = tline[2][0]
fileinfo['width'] = int(tline[3]) data = [ord(d) for d in fd.read()]
height, width = fileinfo['height'], fileinfo['width']
if width<8 or width>32767:
data.resize((height, width, 4))
return rgbe2float(data) img = np.zeros((height, width, 4))
dp = 0
for h in range(height):
if data[dp] !=2 or data[dp+1]!=2:
print 'this file is not run length encoded'
print data[dp:dp+4]
return
if data[dp+2]*256+ data[dp+3] != width:
print 'wrong scanline width'
return
dp += 4
for i in range(4):
ptr = 0
while(ptr < width):
if data[dp]>128:
count = data[dp]-128
if count==0 or count>width-ptr:
print 'bad scanline data'
img[h, ptr:ptr+count,i] = data[dp+1]
ptr += count
dp += 2
else:
count = data[dp]
dp += 1
if count==0 or count>width-ptr:
print 'bad scanline data'
img[h, ptr:ptr+count,i] = data[dp: dp+count]
ptr += count
dp +=count
return rgbe2float(img)
if __name__ == '__main__':
m = readHdr()
m1,m2 = m.max(), m.min()
img = (m-m2)/(m1-m2)
m1 = m[:,:,0].copy(); m[:,:,0] = m[:,:,2]; m[:,:,2]=m1
cv2.imwrite('tmp.jpg', img*255)
解析hdr图像文件的python实现的更多相关文章
- Xml解析之——Java/Android/Python
Xml解析之——Java/Android/Python 一.Xml文件 test.xml <note> <to>George</to> <from>Jo ...
- 解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)
解析prototxt文件的python库 prototxt-parser https://github.com/yogin16/prototxt_parser https://test.pypi.or ...
- 解析搜狗词库(python)
#!/usr/bin/python # -*- coding: utf-8 -*- import struct import sys import binascii import pdb #搜狗的sc ...
- julia与python中的列表解析.jl
julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...
- Python网页解析
续上篇文章,网页抓取到手之后就是解析网页了. 在Python中解析网页的库不少,我最开始使用的是BeautifulSoup,貌似这个也是Python中最知名的HTML解析库.它主要的特点就是容错性很好 ...
- 精通 Oracle+Python,第 3 部分:数据解析
进行数据解析的理由不计其数,相关的工具和技巧也同样如此.但是,当您需要用这些数据做一些新的事情时,即使有“合适的”工具可能也是不够的.这一担心对于异类数据源的集成同样存在.用来做这项工作的合适工具迟早 ...
- Python自动化运维之7、生成器、迭代器、列表解析、迭代器表达式
迭代器和生成器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外 ...
- Python中第三方的用于解析HTML的库:BeautifulSoup
背景 在Python去写爬虫,网页解析等过程中,比如: 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站 常常需要涉及到HTML等网页的解析. 当然,对于简单的HTML中内 ...
- python批量解析域名a记录
#encoding:utf-8 import requests import json import dns.resolver import sys reload(sys) sys.setdefaul ...
随机推荐
- [CQOI2006]凸多边形(半平面相交)
嘟嘟嘟 本来我要写feng shui这道题的.然后网上都说什么半平面相交,于是我还得现学这个东西,就来刷这道模板题了. 所谓的半平面相交和高中数学的分数规划特别像.比如这道题,把每一条边看成一条有向直 ...
- win10 系统下获取系统版本号为6.2的问题
近期赶时髦升级了win10,用着挺爽.但是某天在测试一个bug时发现要对win10做特殊处理,于是直接调用了GetVersionEx,并取出版本号进行判断,但是发现得到的版本竟然是6.2.当时就被雷到 ...
- Python - if-else 的多种简洁写法
本博客原文来自:http://www.cnblogs.com/xiexiaoxiao/p/7772441.html,对原作者表示感谢,此处个人转载. 1. 常用 if ... else写法 # 语法 ...
- 401. Binary Watch 回溯
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- PyTorch Notes | PyTorch 编程实践笔记
[ 今天最开心的事情! ] PyTorch的stable版本更新为1.0之后,原本3D模型无脑out of memory.3D模型torch.backends.cudnn.benchmark必须Fal ...
- 知乎TensorFlow入门学习记录
知乎地址:https://zhuanlan.zhihu.com/p/30487008 import tensorflow as tf a=tf.placeholder(tf.int16) # 接受的数 ...
- HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- CentOS7安装mysql兼容性问题
Linux上安装MySQL时出现不兼容的解决办法: [root@localhost ~]# rpm -ivh MySQL-server-5.5.24-1.linux2.6.x86_64.rpm Pre ...
- SharePoint2010代码启动工作流
1. private void StartWorkFlow() { //获得该列表上的发布的所有工作流 SPWorkflowAssociationCollection wfAssociationCol ...
- GoBelieve IM 消息推送的方案
消息推送设计方案如下: 所有接入im SDK的deviceTOken都会存储到IM服务器.就可以 IM服务器来根据你们服务器指定的useId来下发消息.判断客户端在线,并且APP在前台.就是socke ...