|
| 1 | +#!/usr/bin/env python3.4 |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import numpy as np |
| 6 | +import uuid |
| 7 | +from scipy import misc |
| 8 | +import numpy as np |
| 9 | +from PIL import Image |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +def read(file): |
| 14 | + if file.endswith('.float3'): return readFloat(file) |
| 15 | + elif file.endswith('.flo'): return readFlow(file) |
| 16 | + elif file.endswith('.ppm'): return readImage(file) |
| 17 | + elif file.endswith('.pgm'): return readImage(file) |
| 18 | + elif file.endswith('.png'): return readImage(file) |
| 19 | + elif file.endswith('.jpg'): return readImage(file) |
| 20 | + elif file.endswith('.pfm'): return readPFM(file)[0] |
| 21 | + else: raise Exception('don\'t know how to read %s' % file) |
| 22 | + |
| 23 | +def write(file, data): |
| 24 | + if file.endswith('.float3'): return writeFloat(file, data) |
| 25 | + elif file.endswith('.flo'): return writeFlow(file, data) |
| 26 | + elif file.endswith('.ppm'): return writeImage(file, data) |
| 27 | + elif file.endswith('.pgm'): return writeImage(file, data) |
| 28 | + elif file.endswith('.png'): return writeImage(file, data) |
| 29 | + elif file.endswith('.jpg'): return writeImage(file, data) |
| 30 | + elif file.endswith('.pfm'): return writePFM(file, data) |
| 31 | + else: raise Exception('don\'t know how to write %s' % file) |
| 32 | + |
| 33 | +def readPFM(file): |
| 34 | + file = open(file, 'rb') |
| 35 | + |
| 36 | + color = None |
| 37 | + width = None |
| 38 | + height = None |
| 39 | + scale = None |
| 40 | + endian = None |
| 41 | + |
| 42 | + header = file.readline().rstrip() |
| 43 | + if header.decode("ascii") == 'PF': |
| 44 | + color = True |
| 45 | + elif header.decode("ascii") == 'Pf': |
| 46 | + color = False |
| 47 | + else: |
| 48 | + raise Exception('Not a PFM file.') |
| 49 | + |
| 50 | + dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline().decode("ascii")) |
| 51 | + if dim_match: |
| 52 | + width, height = list(map(int, dim_match.groups())) |
| 53 | + else: |
| 54 | + raise Exception('Malformed PFM header.') |
| 55 | + |
| 56 | + scale = float(file.readline().decode("ascii").rstrip()) |
| 57 | + if scale < 0: # little-endian |
| 58 | + endian = '<' |
| 59 | + scale = -scale |
| 60 | + else: |
| 61 | + endian = '>' # big-endian |
| 62 | + |
| 63 | + data = np.fromfile(file, endian + 'f') |
| 64 | + shape = (height, width, 3) if color else (height, width) |
| 65 | + |
| 66 | + data = np.reshape(data, shape) |
| 67 | + data = np.flipud(data) |
| 68 | + return data, scale |
| 69 | + |
| 70 | +def writePFM(file, image, scale=1): |
| 71 | + file = open(file, 'wb') |
| 72 | + |
| 73 | + color = None |
| 74 | + |
| 75 | + if image.dtype.name != 'float32': |
| 76 | + raise Exception('Image dtype must be float32.') |
| 77 | + |
| 78 | + image = np.flipud(image) |
| 79 | + |
| 80 | + if len(image.shape) == 3 and image.shape[2] == 3: # color image |
| 81 | + color = True |
| 82 | + elif len(image.shape) == 2 or len(image.shape) == 3 and image.shape[2] == 1: # greyscale |
| 83 | + color = False |
| 84 | + else: |
| 85 | + raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.') |
| 86 | + |
| 87 | + file.write('PF\n' if color else 'Pf\n'.encode()) |
| 88 | + file.write('%d %d\n'.encode() % (image.shape[1], image.shape[0])) |
| 89 | + |
| 90 | + endian = image.dtype.byteorder |
| 91 | + |
| 92 | + if endian == '<' or endian == '=' and sys.byteorder == 'little': |
| 93 | + scale = -scale |
| 94 | + |
| 95 | + file.write('%f\n'.encode() % scale) |
| 96 | + |
| 97 | + image.tofile(file) |
| 98 | + |
| 99 | +def readFlow(name): |
| 100 | + if name.endswith('.pfm') or name.endswith('.PFM'): |
| 101 | + return readPFM(name)[0][:,:,0:2] |
| 102 | + |
| 103 | + f = open(name, 'rb') |
| 104 | + |
| 105 | + header = f.read(4) |
| 106 | + if header.decode("utf-8") != 'PIEH': |
| 107 | + raise Exception('Flow file header does not contain PIEH') |
| 108 | + |
| 109 | + width = np.fromfile(f, np.int32, 1).squeeze() |
| 110 | + height = np.fromfile(f, np.int32, 1).squeeze() |
| 111 | + |
| 112 | + flow = np.fromfile(f, np.float32, width * height * 2).reshape((height, width, 2)) |
| 113 | + |
| 114 | + return flow.astype(np.float32) |
| 115 | + |
| 116 | +def readImage(name): |
| 117 | + if name.endswith('.pfm') or name.endswith('.PFM'): |
| 118 | + data = readPFM(name)[0] |
| 119 | + if len(data.shape)==3: |
| 120 | + return data[:,:,0:3] |
| 121 | + else: |
| 122 | + return data |
| 123 | + |
| 124 | + return misc.imread(name) |
| 125 | + |
| 126 | +def writeImage(name, data): |
| 127 | + if name.endswith('.pfm') or name.endswith('.PFM'): |
| 128 | + return writePFM(name, data, 1) |
| 129 | + |
| 130 | + return misc.imsave(name, data) |
| 131 | + |
| 132 | +def writeFlow(name, flow): |
| 133 | + f = open(name, 'wb') |
| 134 | + f.write('PIEH'.encode('utf-8')) |
| 135 | + np.array([flow.shape[1], flow.shape[0]], dtype=np.int32).tofile(f) |
| 136 | + flow = flow.astype(np.float32) |
| 137 | + flow.tofile(f) |
| 138 | + |
| 139 | +def readFloat(name): |
| 140 | + f = open(name, 'rb') |
| 141 | + |
| 142 | + if(f.readline().decode("utf-8")) != 'float\n': |
| 143 | + raise Exception('float file %s did not contain <float> keyword' % name) |
| 144 | + |
| 145 | + dim = int(f.readline()) |
| 146 | + |
| 147 | + dims = [] |
| 148 | + count = 1 |
| 149 | + for i in range(0, dim): |
| 150 | + d = int(f.readline()) |
| 151 | + dims.append(d) |
| 152 | + count *= d |
| 153 | + |
| 154 | + dims = list(reversed(dims)) |
| 155 | + |
| 156 | + data = np.fromfile(f, np.float32, count).reshape(dims) |
| 157 | + if dim > 2: |
| 158 | + data = np.transpose(data, (2, 1, 0)) |
| 159 | + data = np.transpose(data, (1, 0, 2)) |
| 160 | + |
| 161 | + return data |
| 162 | + |
| 163 | +def writeFloat(name, data): |
| 164 | + f = open(name, 'wb') |
| 165 | + |
| 166 | + dim=len(data.shape) |
| 167 | + if dim>3: |
| 168 | + raise Exception('bad float file dimension: %d' % dim) |
| 169 | + |
| 170 | + f.write(('float\n').encode('ascii')) |
| 171 | + f.write(('%d\n' % dim).encode('ascii')) |
| 172 | + |
| 173 | + if dim == 1: |
| 174 | + f.write(('%d\n' % data.shape[0]).encode('ascii')) |
| 175 | + else: |
| 176 | + f.write(('%d\n' % data.shape[1]).encode('ascii')) |
| 177 | + f.write(('%d\n' % data.shape[0]).encode('ascii')) |
| 178 | + for i in range(2, dim): |
| 179 | + f.write(('%d\n' % data.shape[i]).encode('ascii')) |
| 180 | + |
| 181 | + data = data.astype(np.float32) |
| 182 | + if dim==2: |
| 183 | + data.tofile(f) |
| 184 | + |
| 185 | + else: |
| 186 | + np.transpose(data, (2, 0, 1)).tofile(f) |
| 187 | + |
0 commit comments