2015-07-04 99 views
2

在我玩的游戏中称为Starbound,我试图创建一个已修改的项目。在游戏中,商品代码基于JSON字符串。对我创建了一个项目一个字符串的示例使用以下JSON在游戏中区分绘制项目:如何将精灵图像转换为JSON以获得Starbound mod?

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

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

从我所了解的情况来看,游戏提供了一些有限的可用于绘制图像的拼贴。在一般情况下,我在我的形象应该光栅化为根据其提供的瓦片这个JSON格式:

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

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

+0

你说你想保持透明度,但是你的例子中只有RGB格式,它们没有能力表达透明度。如何以这种格式显示部分透明的像素?有没有alpha值?他们是否使用颜色来指定透明度(例如魔术般的紫色,魔术般的绿色)?我们会处理部分透明度,还是像素完全透明或完全不透明? – doppelgreener

回答

1

使用Ruby

你需要安装两个宝石:rmagickcolor

的代码非常简短:

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 

您可以添加一些其他位,使在命令提示符下运行的这种,或者只是requireirb并调用该函数存在。