A simple command-line tool that slices sprite sheets into individual sprites using OpenCVSharp for sprite detection and ImageMagick for image manipulation. The program supports sprite sheets with transparent backgrounds and automatically detects and slices sprites, snapping to the closest common sizes or powers of two.
- Automatically detects individual sprites on sprite sheets.
- Handles sprite sheets with transparent backgrounds.
- Resizes sprites to the nearest common size or power of two.
- Supports custom slicing based on pixel sizes or rows and columns.
- .NET 8.0 or higher
- OpenCVSharp4
- ImageMagick.NET (Magick.NET)
- ImageMagick
To get started, clone this repository to your local machine:
git clone https://github.com/tcgm/slicesheet.git
cd slicesheet
The project requires OpenCVSharp and Magick.NET (ImageMagick.NET bindings). You can install these via NuGet.
- Open the project in Visual Studio.
- Right-click the project in Solution Explorer.
- Select Manage NuGet Packages.
- Search for and install the following packages:
OpenCvSharp4.Windows
Magick.NET-Q16-AnyCPU
Alternatively, you can use the NuGet Package Manager Console or dotnet CLI to install the required packages:
dotnet add package OpenCvSharp4.Windows
dotnet add package Magick.NET-Q16-AnyCPU
After installing the dependencies, build the project to create the executable:
dotnet build -c Release
To create a standalone executable, use the dotnet publish command:
dotnet publish -c Release -r win-x64 --self-contained
This will generate a single executable that can be run on any machine without needing to install .NET or the dependencies.
Once built, you can run the executable from the command line to slice sprite sheets. The tool supports two main modes:
-
Slicing by Pixel Size:
Specify the width and height in pixels for slicing the sprite sheet.
slicesheet <filename.png> 32px 32px
-
Slicing by Rows and Columns:
Specify the number of rows and columns.
slicesheet <filename.png> 10 5
-
Automatic Mode (without filename):
The tool will detect and process all images in the current folder.
slicesheet .
slicesheet spritesheet.png 32px 32px
This command slices the spritesheet.png
into 32x32 pixel sprites and saves them in the same folder.
- filename.png: The sprite sheet to process.
- 32px 32px: The width and height in pixels of each sprite.
- 10 5: Number of rows and columns to slice the sprite sheet.
The output files will be saved in the same directory as the input file, in a folder named after the sprite sheet (without the file extension).
For example, if the input file is spritesheet.png
, the individual sprites will be saved as sprite_0.png
, sprite_1.png
, and so on in a folder named spritesheet
.
-
No sprites detected:
- Make sure the sprite sheet has a transparent background. The program relies on the alpha channel for sprite detection.
- Increase the dilation size in the code if sprites are too close together.
-
Memory issues with large images:
- If processing large sprite sheets, consider using a machine with more RAM or reducing the sprite sheet size.
-
Performance issues:
- For large sprite sheets, slicing may take some time. Consider reducing the number of sprites or simplifying the sprite sheet layout.
The program provides basic logging in the command line. If sprites are not being detected, review the output to check image dimensions and other logged details.
This project is licensed under the MIT License.