def save(self, filename): """Save the image to a file.""" assert self.is_valid() assert filename.endswith('.hdr') assert not (self.rotated or self.xFlipped or self.yFlipped), 'Flip or rotation flags are not supported.' with open(filename, "wb") as f: f.write('#?RADIANCE\n'.encode('ascii')) f.write('FORMAT=32-bit_rle_rgbe\n'.encode('ascii')) f.write('\n'.encode('ascii')) f.write( ('-Y %d +X %d\n' % (self.height, self.width)).encode('ascii')) for i in range(self.width * self.height): r = pow(self.data[3 * i], GAMMA) g = pow(self.data[3 * i + 1], GAMMA) b = pow(self.data[3 * i + 2], GAMMA) v = max(r, g, b) e = math.ceil(math.log(v, 2)) if v != 0.0 else 0.0 s = pow(2, e - 8) arr = [ clamp_int(r / s, 0, 255), clamp_int(g / s, 0, 255), clamp_int(b / s, 0, 255), clamp_int(e + 128, 0, 255) ] f.write(bytes(arr))
def to_pil(self): """Create a PIL image to test the script.""" assert self.is_valid() from PIL import Image im = Image.new('RGB', (self.width, self.height)) pixels = im.load() for y in range(self.height): for x in range(self.width): i = 3 * (y * self.width + x) r = clamp_int(255.0 * self.data[i], 0, 255) g = clamp_int(255.0 * self.data[i + 1], 0, 255) b = clamp_int(255.0 * self.data[i + 2], 0, 255) pixels[x, y] = (r, g, b) return im
theta = math.atan2(y, x) # range -pi to pi r = math.hypot(x, y) phi = math.atan2(z, r) # range -pi/2 to pi/2 uf = -0.5 * equi.width * (math.pi / 2.0 + theta) / math.pi vf = equi.height * (math.pi / 2.0 - phi) / math.pi u1 = int(math.floor(uf)) # coord of pixel to bottom left v1 = int(math.floor(vf)) u2 = u1 + 1 # coords of pixel to top right v2 = v1 + 1 mu = uf - u1 # fraction of way across pixel nu = vf - v1 # Bilinear interpolation A = equi.get_pixel(u1 % equi.width, clamp_int(v1, 0, equi.height - 1)) B = equi.get_pixel(u2 % equi.width, clamp_int(v1, 0, equi.height - 1)) C = equi.get_pixel(u1 % equi.width, clamp_int(v2, 0, equi.height - 1)) D = equi.get_pixel(u2 % equi.width, clamp_int(v2, 0, equi.height - 1)) P = (A[0] * (1 - mu) * (1 - nu) + B[0] * (mu) * (1 - nu) + C[0] * (1 - mu) * nu + D[0] * mu * nu, A[1] * (1 - mu) * (1 - nu) + B[1] * (mu) * (1 - nu) + C[1] * (1 - mu) * nu + D[1] * mu * nu, A[2] * (1 - mu) * (1 - nu) + B[2] * (mu) * (1 - nu) + C[2] * (1 - mu) * nu + D[2] * mu * nu) cubemap.set_pixel(i, j, P) cubemap.save(cm_filename)
print('Load the HDR image...') hdr = HDR.load_from_file(hdr_path) assert hdr.is_valid(), 'Invalid input HDR file.' print('Create the result image...') result = HDR.create_black_image(args.width, args.height) for y in range(args.height): for x in range(args.width): uf = float(x) * hdr.width / args.width vf = float(y) * hdr.height / args.height u1 = int(math.floor(uf)) # coord of pixel to bottom left v1 = int(math.floor(vf)) u2 = u1 + 1 # coords of pixel to top right v2 = v1 + 1 mu = uf - u1 # fraction of way across pixel nu = vf - v1 # Bilinear interpolation A = hdr.get_pixel(u1 % hdr.width, clamp_int(v1, 0, hdr.height - 1)) B = hdr.get_pixel(u2 % hdr.width, clamp_int(v1, 0, hdr.height - 1)) C = hdr.get_pixel(u1 % hdr.width, clamp_int(v2, 0, hdr.height - 1)) D = hdr.get_pixel(u2 % hdr.width, clamp_int(v2, 0, hdr.height - 1)) P = (A[0] * (1 - mu) * (1 - nu) + B[0] * (mu) * (1 - nu) + C[0] * (1 - mu) * nu + D[0] * mu * nu, A[1] * (1 - mu) * (1 - nu) + B[1] * (mu) * (1 - nu) + C[1] * (1 - mu) * nu + D[1] * mu * nu, A[2] * (1 - mu) * (1 - nu) + B[2] * (mu) * (1 - nu) + C[2] * (1 - mu) * nu + D[2] * mu * nu) result.set_pixel(x, y, P) result.save(hdr_path)
dest='quality', default=98, type=int, help='specifies the output quality (this affects only the JPG format)') args = parser.parse_args() hdr_path = args.input result_path = hdr_path.replace('.hdr', '.' + args.format) assert hdr_path.endswith('.hdr'), 'Invalid input extension.' assert hdr_path != result_path, 'Identical input and output paths.' assert os.path.isfile(hdr_path), 'Input file doest not exits.' print('Load the HDR image...') hdr = HDR.load_from_file(hdr_path) assert hdr.is_valid(), 'Invalid input HDR file.' print('Create the result image') result = RegularImage.create_black_image(hdr.width, hdr.height) for y in range(hdr.height): for x in range(hdr.width): pixel = hdr.get_pixel(x, y) pixel = (clamp_int(255.0 * pixel[0], 0, 255), clamp_int(255.0 * pixel[1], 0, 255), clamp_int(255.0 * pixel[2], 0, 255)) result.set_pixel(x, y, pixel) if format == 'png': result.save(result_path, quality=args.quality) else: result.save(result_path)