-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer2ASCII.lua
106 lines (91 loc) · 3.8 KB
/
layer2ASCII.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
-- Layers2ASCII
-- This script exports each layer of an image as a text file in ASCII format,
-- using incremental symbols to represent the pixels in each layer. The output
-- consists of multiple text files, one for each layer in the image.
----------------------------------------------------------------------
-- by TRBRY (https://github.com/Tranberry)
--
-- Inspired by joshalexjacobs (https://github.com/Joshalexjacobs)
-- (https://github.com/Joshalexjacobs/aseprite-ascii-script)
--
-- Inspired by Juan Gaspar García (https://github.com/PKGaspi)
-- (https://github.com/PKGaspi/AsepriteScripts)
----------------------------------------------------------------------
-- Get the active sprite
sprite = app.activeSprite
if sprite then
-- Set up a table to keep track of unique colors and how they're represented as symbols
primaryTable = {}
-- Keep track of how many symbols have been used so far
symbolsUsed = 0
-- Loop through each layer in the sprite
for _, layer in ipairs(sprite.layers) do
-- Set up a table to store the pixels in the layer
pixels = {}
-- Loop through each cel in the layer
for _, cel in ipairs(layer.cels) do
-- Loop through each pixel in the cel
for it in cel.image:pixels() do
-- Get the current pixel's value
local pixelValue = it()
-- Set the pixel to its current value (not sure why this is necessary)
it(pixelValue)
-- If the pixel is not transparent
if pixelValue ~= 0 then
-- Add a new unique color to the primary table if it doesn't already exist
if primaryTable[tostring(pixelValue)] == nil then
primaryTable[tostring(pixelValue)] = {
letter = string.char(48 + symbolsUsed)
}
symbolsUsed = symbolsUsed + 1
end
end
-- Store the pixel's value and coordinates in the pixels table
if pixels[it.y + 1] == nil then
pixels[it.y + 1] = {}
end
pixels[it.y + 1][it.x + 1] = {
pixelValue = pixelValue,
x = it.x,
y = it.y
}
end
end
-- Write the pixels to a text file
local path, title = sprite.filename:match("^(.+[/\\])(.-).([^.]*)$")
local fileName = path .. layer.name
-- Try to open the file for writing
file, err = io.open(fileName .. ".txt", "w")
if file == nil then
print("error " .. err)
else
-- Set the output to the file
io.output(file)
-- Loop through each row of pixels
for y = 1, #pixels, 1 do
-- Write a double quote to start the row
io.write('"')
-- Loop through each pixel in the row
for x = 1, #pixels[y] do
-- If the pixel is transparent, write a space
if pixels[y][x].pixelValue == 0 then
io.write(" ")
-- Otherwise, write the symbol for the pixel's color
else
io.write(primaryTable[tostring(pixels[y][x].pixelValue)].letter)
end
end
-- Add a comma to the end of the row, unless it's the last row
if y ~= #pixels then
io.write('",')
else
io.write('"')
end
-- Add a newline character to the end of the row
io.write('\n')
end
-- Close the file
io.close(file)
end
end
end