解析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 ...
随机推荐
- Linux学习总结(十五)文件查找 which whereis locate find
which命令 用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用which命令,就 ...
- 2019.2.27 Eclipse中的Tomcat设置Tomcat服务器手动重启
1.打开Tomcat的设置界面 2.找到Modules界面 3.去掉,就改为手动了
- Azure Blob数据迁移工具
数据迁移备份,更多的应用场景见https://docs.azure.cn/zh-cn/storage/common/storage-moving-data?toc=%2fstorage%2fblobs ...
- ArcSDE 数据迁移 Exception from HRESULT: 0x80041538问题及解决方案
一.问题描述 1.采用gdb模板文件,在ArcSDE(数据服务器)中批量创建数据库表(数据迁移)时,用到接口ESRI.ArcGIS.Geodatabase.IGeoDBDataTransfer的方法T ...
- 递归根据父ID 找所有子类ID
function getinfo($pid){ $str = ''; $row = M('user')->where(array('pid'=>$pid))->select(); i ...
- Redis的安装和部署(windows )
Redis是一个开源的试用ANSI C语言编写的.遵守BSD协议.支持网络.可基于内存可持久化的日志型.key-value数据库.通常被称为数据结构服务器. redis的数据类型有:字符串(strin ...
- 通过代码退出IOS程序
-(void) tapClick:(UITapGestureRecognizer *)tap{ [UIViewbeginAnimations:@"exitApplication"c ...
- 改变eclipse左侧目录数字体大小
不可在eclipse中修改,只能通过修改配置文件来实现. 找到eclipse的安装位置(或解压路径): eclipse\plugins\org.eclipse.ui.themes_1.2.100.v2 ...
- 使用vue封装一个tab栏切换的左侧导航栏的公共组件
首先看最终效果图: 1.compent文件夹里添加tab文件夹,里面创建index.vue index.js index.css index.vue内的template部份代码如下:(最新更正:代码 ...
- 数据结构08——Trie
一.什么是Trie? Trie树,一般被称为字典树.前缀树等等,Trie是一种多叉树,这个和二分搜索树.堆.线段树这些数据结构不一样,因为这些都是二叉树.,Trie树除了是一种多叉树,它是一种哈希树的 ...