Fix #4 saving in grayscale

This commit is contained in:
Aroy-Art 2025-01-07 13:10:20 +01:00
parent b64a47abe6
commit ff0c30c952
Signed by: Aroy
GPG key ID: 583642324A1D2070

View file

@ -7,8 +7,8 @@ tile_width="$3" # Width of each tile
tile_height="$4" # Height of each tile tile_height="$4" # Height of each tile
# Get the dimensions of the input image # Get the dimensions of the input image
image_width=$(identify -format "%w" "$input_image") image_width=$(magick identify -format "%w" "$input_image")
image_height=$(identify -format "%h" "$input_image") image_height=$(magick identify -format "%h" "$input_image")
# Calculate the number of tiles in both directions # Calculate the number of tiles in both directions
tiles_across=$((image_width / tile_width)) tiles_across=$((image_width / tile_width))
@ -18,26 +18,30 @@ tiles_down=$((image_height / tile_height))
new_width=$((tiles_across * (tile_width + 1) - 1)) new_width=$((tiles_across * (tile_width + 1) - 1))
new_height=$((tiles_down * (tile_height + 1) - 1)) new_height=$((tiles_down * (tile_height + 1) - 1))
# Create a blank canvas with a transparent background # Create a blank canvas with a transparent background and enforce the correct color space
convert -size "${new_width}x${new_height}" xc:none blank_canvas.png magick -size "${new_width}x${new_height}" xc:none -colorspace sRGB 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
for ((x = 0; x < tiles_across; x++)); do for ((x = 0; x < tiles_across; x++)); do
# Extract the tile from the original image # Extract the tile from the original image
tile_x=$((x * tile_width)) tile_x=$((x * tile_width))
tile_y=$((y * tile_height)) tile_y=$((y * tile_height))
convert "$input_image" -crop "${tile_width}x${tile_height}+$tile_x+$tile_y" "tile_${x}_${y}.png"
# Place the tile on the new image, accounting for the 1-pixel padding # Crop the tile while preserving the color space
new_tile_x=$((x * (tile_width + 1))) magick "$input_image" -crop "${tile_width}x${tile_height}+$tile_x+$tile_y" -colorspace sRGB "tile_${x}_${y}.png"
new_tile_y=$((y * (tile_height + 1)))
composite -geometry "+${new_tile_x}+${new_tile_y}" "tile_${x}_${y}.png" blank_canvas.png blank_canvas.png # Place the tile on the new image, accounting for the 1-pixel padding
done new_tile_x=$((x * (tile_width + 1)))
new_tile_y=$((y * (tile_height + 1)))
# 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
done
done done
# Save the final image # Save the final image while preserving the color space and transparency
mv blank_canvas.png "$output_image" magick blank_canvas.png -colorspace sRGB "$output_image"
# Clean up temporary tiles # Clean up temporary tiles
rm tile_*.png rm tile_*.png