Converting Texture Atlas exported from Adobe Animate to Images

30 Views Asked by At

I would like to know how to convert Texture Atlas into images. To solve this, I tried a lot of searching and even tried to analyze the files, but it was too difficult for me. It contains Animation.json, spritemap1.json, spritemap1.png.

The dictionary written in Animation.json was so difficult that I couldn't analyze it. Although I primarily use Python, I'm willing to try other things as well. I did some analysis and tried to convert it to an image, but PILLOW's image pasting method was too complicated, so even this failed.

1

There are 1 best solutions below

0
KBill On
  1. Use json to read the JSON files content (Animation.json and spritemap1.json)
  2. Analyse informations of these files to extract coordinates and dimensions of the different parts of the Atlas.
  3. Use Pillow to load the main image (spritemap1.png)
  4. Cut the different parts of the main image using the extracted coordinates.
  5. Save each parts as an image.
import json
from PIL import Image

# Load content of file Animation.json
with open('Animation.json', 'r') as animation_file:
    animation_data = json.load(animation_file)

# Load content of file spritemap1.json
with open('spritemap1.json', 'r') as spritemap_file:
    spritemap_data = json.load(spritemap_file)

# Load main image
image = Image.open('spritemap1.png')

# Extraction of coordinates and dimensions from data animation
for animation_name, animation_info in animation_data.items():
    frame = animation_info['frame']
    x, y, width, height = spritemap_data[frame]['frame']

    # Crop image from coordinates 
    sprite = image.crop((x, y, x + width, y + height))

    # Save image
    sprite.save(f'{animation_name}.png')

print("finished!")