Exporting indices

Is it possible to export just the index values from a sprite in indexed mode?

Sounds like you’re just talking about saving the palette. In that case yes.

image

If not, then you’re gonna have to share a bit more of what you’re trying to do.

I already saved the palette. What I need is to export the raw index values that the image is using to lookup colors in the palette.

It feels like it should be basic functionality to be able export a raw indexed image format, even a just CSV or something is all I need. As far as I can tell, I am only able to export traditional image formats such as png, jpg, etc. I looked over the scripting API and I don’t see any obvious way to do what I need to do.

I really need this feature for my project, so if anyone has any ideas or can recommend another indexed-image editor that can actually export the raw data, please let me know.

I think I figured it out. I wrote an export script using the “Gameboy export” script posted here by boombuler as a guide. I just stripped out the gameboy assembly stuff and had it write the data directly to a file. Skimming over the output values it looks like it does give me the raw index values I wanted.

Do you mind sharing your script? I’m looking for the exact same thing.

Here you go:

local sprite = app.activeSprite

-- Check constraints
if sprite == nil then
  app.alert("No Sprite...")
  return
end
if sprite.colorMode ~= ColorMode.INDEXED then
  app.alert("Sprite needs to be indexed")
  return
end

local function getPaletteData(palette)
	local ncolors = #palette
	local res = string.format("%i\n", ncolors)

	for i=0, ncolors-1 do
		local color = palette:getColor(i)
		res = res .. string.format("%i %i %i %i\n", color.red, color.green, color.blue, color.alpha)		
	end

	return res
end

local function getIndexData(img, x, y, w, h)
	local res = ""
	for y = 0,h-1 do
		for x = 0, w-1 do
			px = img:getPixel(x, y)
			res = res .. string.format("%i ", px)
		end
		res = res .. "\n"
	end

	return res
end

local function exportFrame(frm)
	if frm == nil then
		frm = 1
	end

	local img = Image(sprite.spec)
	img:drawSprite(sprite, frm)
	
	io.write("Palette Indexed Picture\n")
	io.write("begin palette\n")
	io.write(getPaletteData(sprite.palettes[1]))
	io.write("end palette\n")
	io.write("begin indices\n")
	io.write(getIndexData(img, x, y, sprite.width, sprite.height))
	io.write("end indices\n")
end


local dlg = Dialog()
dlg:file{ id="exportFile",
          label="File",
          title="Gameboy-Assembler Export",
          open=false,
          save=true,
        --filename= p .. fn .. "pip",
          filetypes={"txt", "pip" }}
dlg:check{ id="onlyCurrentFrame",
           text="Export only current frame",
           selected=true }

dlg:button{ id="ok", text="OK" }
dlg:button{ id="cancel", text="Cancel" }
dlg:show()
local data = dlg.data
if data.ok then
	local f = io.open(data.exportFile, "w")
	io.output(f)

	if data.onlyCurrentFrame then
		exportFrame(app.activeFrame)
	else
	 	for i = 1,#sprite.frames do
	 		io.write(string.format(";Frame %d\n", i))
	 		exportFrame(i)
		end
	end

	io.close(f)
end

Just save it as a “.lua” file. When you are ready to export the index data, run the script and it should prompt you to save the file. It will save the palette information and the indices to a text file. Again, most of the credit goes to boombuler.

Hope it helps!

4 Likes

Works like a charm, thank you very much!

Thanks for sharing that script! It really helped me understand how to use aseprite’s API. I needed to export my files in binary format, so I tweaked it a little to dump the index values directly.

I bundled it into an extension that adds a File > Export Raw Pixels menu if this would be useful for anyone else :slight_smile:

1 Like