ShellScripts: set a dedicated temp folder for the image split script

This commit is contained in:
Aroy-Art 2025-01-10 22:39:48 +01:00
parent 1357a30650
commit ac100607f1
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -7,6 +7,13 @@ tile_width="$3" # Width of each tile
tile_height="$4" # Height of each tile tile_height="$4" # Height of each tile
tile_padding="$5" # Padding between tiles tile_padding="$5" # Padding between tiles
# Get working directory
workdir=$(pwd)
# Make temporary folder for temp files
tmpfolder="${workdir}/.tmp-tilemap-add-padding"
mkdir -p "${tmpfolder}"
# Get the dimensions of the input image # Get the dimensions of the input image
image_width=$(magick identify -format "%w" "$input_image") image_width=$(magick identify -format "%w" "$input_image")
image_height=$(magick identify -format "%h" "$input_image") image_height=$(magick identify -format "%h" "$input_image")
@ -20,7 +27,7 @@ new_width=$((tiles_across * (tile_width + tile_padding) - tile_padding))
new_height=$((tiles_down * (tile_height + tile_padding) - tile_padding)) new_height=$((tiles_down * (tile_height + tile_padding) - tile_padding))
# Create a blank canvas with a transparent background and enforce the correct color space # Create a blank canvas with a transparent background and enforce the correct color space
magick -size "${new_width}x${new_height}" xc:none -colorspace sRGB blank_canvas.png magick -size "${new_width}x${new_height}" xc:none -colorspace sRGB "${tmpfolder}/blank_canvas.png"
# Iterate through each tile in the tilemap # Iterate through each tile in the tilemap
for ((y = 0; y < tiles_down; y++)); do for ((y = 0; y < tiles_down; y++)); do
@ -30,21 +37,21 @@ for ((y = 0; y < tiles_down; y++)); do
tile_y=$((y * tile_height)) tile_y=$((y * tile_height))
# Crop the tile while preserving the color space # Crop the tile while preserving the color space
magick "$input_image" -crop "${tile_width}x${tile_height}+$tile_x+$tile_y" -colorspace sRGB "tile_${x}_${y}.png" magick "$input_image" -crop "${tile_width}x${tile_height}+$tile_x+$tile_y" -colorspace sRGB "${tmpfolder}/tile_${x}_${y}.png"
# Place the tile on the new image, accounting for the 1-pixel padding # Place the tile on the new image, accounting for the 1-pixel padding
new_tile_x=$((x * (tile_width + tile_padding))) new_tile_x=$((x * (tile_width + tile_padding)))
new_tile_y=$((y * (tile_height + tile_padding))) new_tile_y=$((y * (tile_height + tile_padding)))
# Composite the tile onto the new canvas, ensuring color space is preserved # Composite the tile onto the new canvas, ensuring color space is preserved
magick composite -geometry "+${new_tile_x}+${new_tile_y}" "tile_${x}_${y}.png" -colorspace sRGB blank_canvas.png blank_canvas.png magick composite -geometry "+${new_tile_x}+${new_tile_y}" "${tmpfolder}/tile_${x}_${y}.png" -colorspace sRGB "${tmpfolder}/blank_canvas.png" "${tmpfolder}/blank_canvas.png"
done done
done done
# Save the final image while preserving the color space and transparency # Save the final image while preserving the color space and transparency
magick blank_canvas.png -colorspace sRGB "$output_image" magick "${tmpfolder}/blank_canvas.png" -colorspace sRGB "$output_image"
# Clean up temporary tiles # Clean up temporary tiles
rm tile_*.png rm -r "${tmpfolder}"
echo "Output saved to $output_image" echo "Output saved to $output_image"