0

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:

  1. 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() says fromstring doesn't exist.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

user4157124
  • 2,809
  • 13
  • 27
  • 42

1 Answers1

0
import cv2
import numpy as np

width = 131
height = 131

with open("height.Raw", "rb") as rawimg:
   img = np.fromfile(rawimg, np.dtype('u2'), width * height).reshape(height, width)
   colimg = cv2.cvtColor(img, cv2.IMREAD_GRAYSCALE)
   cv2.imwrite("test.png", colimg)

Got it to work using this.

user4157124
  • 2,809
  • 13
  • 27
  • 42