Skip to content

Commit 06c20d9

Browse files
committed
Initial commit
0 parents  commit 06c20d9

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

Diff for: layer2ASCII.lua

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

Diff for: readme.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Aseprite Layers to ASCII Script
2+
3+
This is a script for [Aseprite](https://www.aseprite.org/), a pixel art editor, that converts each layer of a sprite to an ASCII text file.
4+
5+
## Installation
6+
7+
1. Download the `layer2ASCII.lua` file.
8+
2. Open Aseprite and navigate to `File > Scripts > Open Scripts Folder`.
9+
3. Place the `layer2ASCII.lua` file in this folder.
10+
11+
## Usage
12+
13+
1. Open the sprite you want to convert to ASCII.
14+
2. Navigate to `File > Scripts > layer2ASCII`.
15+
3. Wait for the script to finish running.
16+
4. For each layer in the sprite, there will be a corresponding text file in the same folder as the sprite.
17+
18+
Note that the script uses incremental symbols for each pixel. The output text files will contain the ASCII symbols corresponding to the colors in each layer of the sprite.
19+
20+
Enjoy!

0 commit comments

Comments
 (0)