如题

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实现的更多相关文章

  1. Xml解析之——Java/Android/Python

    Xml解析之——Java/Android/Python 一.Xml文件 test.xml <note> <to>George</to> <from>Jo ...

  2. 解析prototxt文件的python库 prototxt-parser(使用parsy自定义文件格式解析)

    解析prototxt文件的python库 prototxt-parser https://github.com/yogin16/prototxt_parser https://test.pypi.or ...

  3. 解析搜狗词库(python)

    #!/usr/bin/python # -*- coding: utf-8 -*- import struct import sys import binascii import pdb #搜狗的sc ...

  4. julia与python中的列表解析.jl

    julia与python中的列表解析.jl #=julia与python中的列表解析.jl 2016年3月16日 07:30:47 codegay julia是一门很年轻的科学计算语言 julia文档 ...

  5. Python网页解析

    续上篇文章,网页抓取到手之后就是解析网页了. 在Python中解析网页的库不少,我最开始使用的是BeautifulSoup,貌似这个也是Python中最知名的HTML解析库.它主要的特点就是容错性很好 ...

  6. 精通 Oracle+Python,第 3 部分:数据解析

    进行数据解析的理由不计其数,相关的工具和技巧也同样如此.但是,当您需要用这些数据做一些新的事情时,即使有“合适的”工具可能也是不够的.这一担心对于异类数据源的集成同样存在.用来做这项工作的合适工具迟早 ...

  7. Python自动化运维之7、生成器、迭代器、列表解析、迭代器表达式

    迭代器和生成器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外 ...

  8. Python中第三方的用于解析HTML的库:BeautifulSoup

    背景 在Python去写爬虫,网页解析等过程中,比如: 如何用Python,C#等语言去实现抓取静态网页+抓取动态网页+模拟登陆网站 常常需要涉及到HTML等网页的解析. 当然,对于简单的HTML中内 ...

  9. python批量解析域名a记录

    #encoding:utf-8 import requests import json import dns.resolver import sys reload(sys) sys.setdefaul ...

随机推荐

  1. 关于python线程池threadpool

    #coding=utf-8 import time import threadpool def wait_time(n): print('%d\n' % n) time.sleep(2) #在线程池中 ...

  2. PHP面试系列 之框架(一)---- MVC框架基本工作原理

    题:谈谈你对MVC的认识,介绍集中目前比较流行的MVC框架 考点: (1)MVC工作原理 (2)常见MVC框架 延伸: (1)单一入口的工作原理 (2)模板引擎的理解 (1)MVC工作原理 Model ...

  3. 【SP2713 GSS4 - Can you answer these queries IV】 题解

    题目链接:https://www.luogu.org/problemnew/show/SP2713 真暴力啊. 开方你开就是了,开上6次就都没了. #include <cmath> #in ...

  4. 【noip 模拟赛curse,light,maze】 题解

    2018.10.16 总结:考的不好 原因: 1.考的时候没状态,读题读不进去 2.考的时候不仔细,该得分没得到 T1:curse 1.咒语 (curse.pas/c/cpp) [题目描述] 亮亮梦到 ...

  5. i2c 通信

    时间长了记忆就会模糊, 保存下逻辑分析抓到的图像, 什么时候需要可以看一眼. 当clk处于高电平时, data线有下降,说明开始传输, 有上升说明结束传输. 发送地址无回应: 发送地址有回应 正常数据 ...

  6. sharepoint rest api Add ListItem 报错

    Recently, I was trying to create a list item using Rest API on Sharepoint 2013. I got the following ...

  7. iOS利用block实现链式编程方法(Objective-C链式编程)

    objc利用block实现链式编程方法 因为不好读.block和其他语言的匿名函数一样,很多程序员刚开始很难主动去用他. 本文描述block作为属性的实际使用,看懂block,并讲解如何利用block ...

  8. iOS 利用KeyChain+ IDFV + BundleID 来作为UUID,保证传给服务端的UUID唯一

    查了相关资料,发现只有KeyChain + IDFV可以保证UUID唯一,参考以下代码 , UICKeyChainStore + (NSString*)identifierForVender{ UIC ...

  9. layDay日期格式不合法报错解决

    报错内容如下: Uncaught TypeError: Cannot read property 'appendChild' of undefined 相关报错内容的行代码如下 即使日期格式拼接正确也 ...

  10. iview admin 生成环境打包时路径问题

    关于生产打包路径不对,字体图标引用错误的问题.以下是解决方案供参考:1.webpack.base.config.js 17行修改为: path: path.resolve(__dirname, '.. ...