2024-10-07 20:15:25 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Input parameters
|
|
|
|
input_image="$1" # Input tilemap image
|
|
|
|
output_image="$2" # Output image with transparent pixels
|
|
|
|
tile_width="$3" # Width of each tile
|
|
|
|
tile_height="$4" # Height of each tile
|
2025-01-07 13:26:10 +01:00
|
|
|
tile_padding="$5" # Padding between tiles
|
2024-10-07 20:15:25 +02:00
|
|
|
|
2025-01-10 22:39:48 +01:00
|
|
|
# Get working directory
|
|
|
|
workdir=$(pwd)
|
|
|
|
|
|
|
|
# Make temporary folder for temp files
|
|
|
|
tmpfolder="${workdir}/.tmp-tilemap-add-padding"
|
|
|
|
mkdir -p "${tmpfolder}"
|
|
|
|
|
2024-10-07 20:15:25 +02:00
|
|
|
# Get the dimensions of the input image
|
2025-01-07 13:10:20 +01:00
|
|
|
image_width=$(magick identify -format "%w" "$input_image")
|
|
|
|
image_height=$(magick identify -format "%h" "$input_image")
|
2024-10-07 20:15:25 +02:00
|
|
|
|
|
|
|
# Calculate the number of tiles in both directions
|
|
|
|
tiles_across=$((image_width / tile_width))
|
|
|
|
tiles_down=$((image_height / tile_height))
|
|
|
|
|
|
|
|
# New image size (considering the 1 pixel padding between tiles)
|
2025-01-07 13:26:10 +01:00
|
|
|
new_width=$((tiles_across * (tile_width + tile_padding) - tile_padding))
|
|
|
|
new_height=$((tiles_down * (tile_height + tile_padding) - tile_padding))
|
2024-10-07 20:15:25 +02:00
|
|
|
|
2025-01-07 13:10:20 +01:00
|
|
|
# Create a blank canvas with a transparent background and enforce the correct color space
|
2025-01-10 22:39:48 +01:00
|
|
|
magick -size "${new_width}x${new_height}" xc:none -colorspace sRGB "${tmpfolder}/blank_canvas.png"
|
2024-10-07 20:15:25 +02:00
|
|
|
|
|
|
|
# Iterate through each tile in the tilemap
|
|
|
|
for ((y = 0; y < tiles_down; y++)); do
|
2025-01-07 13:10:20 +01:00
|
|
|
for ((x = 0; x < tiles_across; x++)); do
|
|
|
|
# Extract the tile from the original image
|
|
|
|
tile_x=$((x * tile_width))
|
|
|
|
tile_y=$((y * tile_height))
|
|
|
|
|
|
|
|
# Crop the tile while preserving the color space
|
2025-01-10 22:39:48 +01:00
|
|
|
magick "$input_image" -crop "${tile_width}x${tile_height}+$tile_x+$tile_y" -colorspace sRGB "${tmpfolder}/tile_${x}_${y}.png"
|
2025-01-07 13:10:20 +01:00
|
|
|
|
|
|
|
# Place the tile on the new image, accounting for the 1-pixel padding
|
2025-01-07 13:26:10 +01:00
|
|
|
new_tile_x=$((x * (tile_width + tile_padding)))
|
|
|
|
new_tile_y=$((y * (tile_height + tile_padding)))
|
2025-01-07 13:10:20 +01:00
|
|
|
|
|
|
|
# Composite the tile onto the new canvas, ensuring color space is preserved
|
2025-01-10 22:39:48 +01:00
|
|
|
magick composite -geometry "+${new_tile_x}+${new_tile_y}" "${tmpfolder}/tile_${x}_${y}.png" -colorspace sRGB "${tmpfolder}/blank_canvas.png" "${tmpfolder}/blank_canvas.png"
|
2025-01-07 13:10:20 +01:00
|
|
|
done
|
2024-10-07 20:15:25 +02:00
|
|
|
done
|
|
|
|
|
2025-01-07 13:10:20 +01:00
|
|
|
# Save the final image while preserving the color space and transparency
|
2025-01-10 22:39:48 +01:00
|
|
|
magick "${tmpfolder}/blank_canvas.png" -colorspace sRGB "$output_image"
|
2024-10-07 20:15:25 +02:00
|
|
|
|
|
|
|
# Clean up temporary tiles
|
2025-01-10 22:39:48 +01:00
|
|
|
rm -r "${tmpfolder}"
|
2024-10-07 20:15:25 +02:00
|
|
|
|
|
|
|
echo "Output saved to $output_image"
|