|
| 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 |
0 commit comments