I have a grayscale 16bit heightmap from a game that I want to convert to PNG/JPEG using Python. Each map has 30+ heightmaps in a grid. I want to put every file in a grid then export a single file. I am able to with the minimaps which are .DDS files, now I need to do that for the heightmaps. I tried:
From here:
rawData = open("foo.raw" 'rb').read() imgSize = (x,y) # Use the PIL raw decoder to read the data. # the 'F;16' informs the raw decoder that we are reading # a little endian, unsigned integer 16 bit data. img = Image.fromstring('L', imgSize, rawData, 'raw', 'F;16') img.save("foo.png")
Image.fromstring()
saysfromstring
doesn't exist.RawPy:
import rawpy import imageio path = 'image.raw' with rawpy.imread(path) as raw: rgb = raw.postprocess() imageio.imsave('default.tiff', rgb)
RawPy isn't able to read the file saying it is a raw file.
OpenCV:
import cv2 import numpy as np from PIL import Image import torchvision with open('height.raw', 'rb') as infile: buf = infile.read() x = np.fromstring(buf, dtype='uint8') img = cv2.imdecode(x, cv2.IMREAD_UNCHANGED) image = torchvision.transforms.ToPILImage(img) image = Image.open(image) image.show()
Can't make it show/export the image even if OpenCV is able to open it.
I can open the heightmap in Photoshop by importing as 131x131 16 bit PC IBM. But this needs to be automated since there are 30+ files per map and over 20 maps.
rawpixels.net can load the heightmap by changing some settings:
width: 131 height: 131 offset: 1 Predefined Format: Grayscale 8bit bpp1: 16
It uses JavaScript. Converting to Python isn't easy. I tried Js2Py to run (no luck), convert the JavaScript to Python (also no luck).
Maybe I can save the page, edit the settings as I want, in Python load the map file and use Js2Py searching for resulting div image, but that seems complex.