简体   繁体   English

在 Python 上读取并转换 Heightmap.RAW 文件(灰度 16 位)

[英]Read and convert an Heightmap .RAW File (Grayscale 16bit) on Python

I have a grayscale 16bit heightmap from a game that i want to open and convert to PNG/JPEG using python, i've tried a lot of ways, found some tools, but still weren't able to do that i want...我有一个来自游戏的灰度 16 位高度图,我想使用 python 打开并转换为 PNG/JPEG,我尝试了很多方法,找到了一些工具,但仍然无法做到我想要的......

And i just want to say, that i want to do this, because each map of that game has around 30+ heightmaps files that are later align in a grid, and basically i want to do the same the game does, put every file in a grid and then export it as a single file (PNG or JPEG), i was able to do that with the minimaps, which are.DDS files, so converting was easy, and now i need to do the same for the heightmaps.我只想说,我想这样做,因为该游戏的每个 map 都有大约 30 多个高度图文件,这些文件后来在网格中对齐,基本上我想做与游戏相同的操作,将每个文件放入一个网格,然后将其导出为单个文件(PNG 或 JPEG),我可以使用小地图(即 .DDS 文件)来做到这一点,因此转换很容易,现在我需要对高度图做同样的事情。

Now, what i've tried so far...现在,我到目前为止所尝试的......

1: (from this how to convert raw images to png in python? ) 1:(从此如何在python中将原始图像转换为png?

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") 

On this the Image.fromstring() was giving an error saying that fromstring doesn't exist, and i couldn't find a way to replace it在这个 Image.fromstring() 给出了一个错误,说 fromstring 不存在,我找不到替换它的方法

2: Rawpy library examples 2:Rawpy 库示例

import rawpy
import imageio

path = 'image.raw'
with rawpy.imread(path) as raw:
    rgb = raw.postprocess()
imageio.imsave('default.tiff', rgb)

On this, rawpy just wasn't able to read the file, saying it was a raw file对此,rawpy 只是无法读取文件,说它是原始文件

3: OpenCV library 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 was able to open it correctly, i still couldn't figure out a way.无法让它显示/导出图像,即使 opencv 能够正确打开它,我仍然想不出办法。

And more and more stuff...还有越来越多的东西...

But with all this search i found out some things that helped but still wasn't enough但是通过所有这些搜索,我发现了一些有用但仍然不够的东西

First, i found out that i was able to insert the heightmap on photoshop and make it look as i liked by importing it with 131x131 resolution, 16 bit, PC IBM, but again, this needs to be automated since they are over 30+ files per map, and over 20 maps... Then, i found out a website called rawpixels.net that i was also able to load the image, properly by changing some settings:首先,我发现我可以在 photoshop 上插入高度图,并通过以 131x131 分辨率、16 位、PC IBM 导入它,让它看起来像我喜欢的那样,但同样,这需要自动化,因为它们有超过 30 多个文件根据 map 和 20 多张地图……然后,我发现了一个名为 rawpixels.net 的网站,我也可以通过更改一些设置来正确加载图像:

width: 131 height: 131 offset: 1 Predefined Format: Grayscale 8bit bpp1: 16宽度:131 高度:131 偏移量:1 预定义格式:灰度 8 位 bpp1:16

But again, automating it, the website uses javascript for reading the image and changing the settings, so converting this to python isn't that easy, what i tried after founding out about this website was, using js2py library run the functions on the page (no luck), trying to convert the javascript code to a python code (also no luck), and this was all yesterday, today on the work, i was thinking, maybe i can save the page as a single.html file, editing the.html file to have the default settings as i want, and then through python trying to load the.html file and make it load the map file, and then using js2py searching for the result div image, and getting it from there, didn't try this tho, but seems to be a bit complex, at least for me.但同样,自动化它,该网站使用 javascript 来读取图像并更改设置,因此将其转换为 python 并不是那么容易,我在发现这个网站后尝试的是,使用 js2py 库运行页面上的功能(没有运气),试图将 javascript 代码转换为 python 代码(也没有运气),这都是昨天的事情,今天在工作,我在想,也许我可以将页面保存为单个.ZFC35FDC70D5FC69D269883A822EZEZA 文件,编辑the.html file to have the default settings as i want, and then through python trying to load the.html file and make it load the map file, and then using js2py searching for the result div image, and getting it from there, didn不要尝试这个,但似乎有点复杂,至少对我来说。 .. ..

Sorry for having such a big post... Anyways, thks in advance!很抱歉有这么大的帖子......无论如何,提前谢谢!

UPDATE:更新:

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!!!得到它的工作! Thks to who made this https://www.pythonfixing.com/2022/01/fixed-reading-and-saving-12bit-raw.html感谢谁制作了这个https://www.pythonfixing.com/2022/01/fixed-reading-and-saving-12bit-raw.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM