如何将sprite图像转换为JSON以便进行绑定?

时间:2022-09-01 21:23:02

In a game that I play called Starbound I'm trying to create a modded item. In the game, the item code is based on JSON strings. An example of a string on an item that I created uses the following JSON to distinguish a drawable item in game:

在一个叫做Starbound的游戏中,我试图创建一个被修改过的物品。在游戏中,项目代码基于JSON字符串。我创建的一个项目上的字符串示例使用以下JSON来区分游戏中的可绘制项:

[{
    "image":"/particles/ash/3.png?setcolor=090909",
    "position":[0,0]
},
    ... and so on for each pixel in the image ...
]

Is there a way that I can take a sprite that has already been created in image editing software in PNG format, keeping transparency, and rasterize the colors and pixel locations into this JSON format? Something like a batch file that converts the PNG image into this format would do. (I could write the JSON manually, but I really don't want to have to do that.)

是否有一种方法可以将已经在PNG格式的图像编辑软件中创建的精灵保持透明度,并将颜色和像素位置栅格化为JSON格式?类似于批处理文件,可以将PNG图像转换成这种格式。(我可以手工编写JSON,但我真的不想那样做。)

From what I understand, the game provides some limited set of tiles which you can use to draw your image. In general, I my image should be rasterized into this JSON format based upon their provided tiles:

据我所知,这个游戏提供了一些有限的贴图,你可以用它们来绘制你的图像。一般来说,我的图像应该基于它们提供的贴图被栅格化成JSON格式:

[ { "image": "tile.png?setcolor=FFFFFF", "position": [X,Y] }, ... ]

(where in this format, the setcolor variable can be any six digit hex-code color).

(在这种格式中,setcolor变量可以是任何六位数的十六进制码颜色)。

1 个解决方案

#1


1  

Use Ruby

You'll need to install two gems: rmagick and color.

你需要安装两个宝石:rmagick和color。

The code is fairly short:

代码相当简短:

require 'Rmagick'
require 'color'
require 'json'

def rasterize_to_json(inImagePath, outJsonPath)
  image = Magick::Image.read(inImagePath)

  pixels = []

  image.each_pixel do |px,col,row|
    hsla = px.to_hsla
    if hsla[3] > 0.75 # ignore pixels that are less than 75% opaque
      # Need to convert the HSL into HTML hex code (dropping the '#')
      hexcode = (Color::HSL.new(*hsla[0,2]).to_rgb.html.upcase)[1,6]
      pixels << { :image => "/tile.png?setcolor=#{hexcode}", :position => [col, row] }
    end
  end

  f = File.new(outJsonPath, "w")
  f.write(pixels.to_json)
  f.close
end

You could add some other bits to make this runnable from the command prompt, or just require it in irb and call the function there.

您可以添加一些其他的位以使这个可运行的命令提示符,或者只需要它在irb中并在那里调用函数。

#1


1  

Use Ruby

You'll need to install two gems: rmagick and color.

你需要安装两个宝石:rmagick和color。

The code is fairly short:

代码相当简短:

require 'Rmagick'
require 'color'
require 'json'

def rasterize_to_json(inImagePath, outJsonPath)
  image = Magick::Image.read(inImagePath)

  pixels = []

  image.each_pixel do |px,col,row|
    hsla = px.to_hsla
    if hsla[3] > 0.75 # ignore pixels that are less than 75% opaque
      # Need to convert the HSL into HTML hex code (dropping the '#')
      hexcode = (Color::HSL.new(*hsla[0,2]).to_rgb.html.upcase)[1,6]
      pixels << { :image => "/tile.png?setcolor=#{hexcode}", :position => [col, row] }
    end
  end

  f = File.new(outJsonPath, "w")
  f.write(pixels.to_json)
  f.close
end

You could add some other bits to make this runnable from the command prompt, or just require it in irb and call the function there.

您可以添加一些其他的位以使这个可运行的命令提示符,或者只需要它在irb中并在那里调用函数。