Skip to content

Instantly share code, notes, and snippets.

@LoveRenamon
Last active March 1, 2023 11:53
Show Gist options
  • Save LoveRenamon/33d419e273a53428afba9a9dbb89601f to your computer and use it in GitHub Desktop.
Save LoveRenamon/33d419e273a53428afba9a9dbb89601f to your computer and use it in GitHub Desktop.
A few simple ImageMagick 7.0+ scripts to aid textures through CLI
#!/bin/bash
# quit on error
set -e
# test if you have ImageMagick
[ ! $(LC_ALL=C type -t magick) ] && echo -e "missing \"magick\", please install ImageMagick 7.0+ to use this" && exit 1
HelpMe="The purpose of this script is, allow management and aid of specific formats to a common format, PNG.
And why don't use and editor like PhotoShop, GIMP or Krita?
Simple, I don't wanna touch the gamma correction and preserve much as possible information.
This script also allow be used as loop/batch, which makes a good option for many files.
usage:
\t${0##*/} <option> <image>\n\n
<split> <image>
This option will separatate the image each channel to a separated image with _channel.png post fix
alias: [split|separate|separate|separar]
<as> <image>
Save a new image with only alpha information
alias: [splitalpha|alphasplit|sa|separealpha]
<ja> <image> <image2>
Discart <image> Alpha Channel and composite/bind/add Alpha Channel from <image2> argument
alias: [join|addalpha|alpha]
<solid> <px> <#RGB> <png>
Write a RGB Hex solid color to <png>
alias: [color|cor]
<swap> <image>
Swap Red and Green colors of the said <image>
<inv> <image>
Invert all channels
alias: [invertnormal|normal|invertall|negateall]
<invb> <image>
Invert Blue Channel
alias: [invertnormalb|normalb|invertb|negateb]
<invg> <image>
Invert Green Channel
alias: [invertnormalg|normalg|invertg|negateg]
<invr> <image>
Invert Red Channel
alias: [invertnormalr|normalr|invertr|negater]
<inva> <image>
Invert Alpha Channel
alias: [invertnormala|normala|inverta|negateal]
<puregrey> <image>
Convert RGB to average greyscale RGB values
alias: [pgrey|avggrey|averagegrey|midgrey|avgrgb]
<modulategrey> <image>
Set RGB saturation to zero
alias: [mgrey|modulate2grey|2grey]
<lumagrey> <image>
Convert RGB values to rec601luma
alias: [2luma|grey]
<noalpha> <image>
Set image's -alpha off
alias: [removealpha]
<combinergba> <image> <red> <green> <blue> <alpha>
Will compute <red>, <green>, <blue> and optionaly <alpha> input images then write to <image>
alias: [combine|merge]"
# store the image input
img="${2}"
# HACK: workaround the "wontfix" TGA problem
# https://github.com/ImageMagick/ImageMagick/issues/3844
tex_edit() {
magick "${img}" -auto-orient "${@}"
}
case "${1}" in
splita*|alphas*|as*|sa*|separealpha)
tex_edit -channel A -separate "${img::-4}_alpha.png"
;;
split|separ*|strip)
tex_edit -channel R -separate "${img::-4}_red.png"
tex_edit -channel G -separate "${img::-4}_green.png"
tex_edit -channel B -separate "${img::-4}_blue.png"
tex_edit -channel A -separate "${img::-4}_alpha.png"
;;
join|adda*|alpha|ja)
if [ "${3}" ] ; then
alpha="${3}"
#magick "${img}" \( "${alpha}" -colorspace gray \) -alpha off -compose copy_opacity -composite result
tex_edit "${alpha}" -alpha off -compose CopyOpacity -composite "${img%%.*}_out.png"
else
echo -e "${HelpMe}"
fi
;;
solid|col*|cor)
px=${2}
RGB=${3###}
img=${4%%.png}
magick -size ${px}x${px} xc:"#${RGB}" "${img}.png"
;;
swap)
tex_edit -separate -swap 0,2 -combine "PNG32:${img%%.*}.png"
;;
inv|invertnormal|normal|invertall|negateall)
tex_edit -negate "${img%%.*}_out.png"
;;
invb|invertnormalb|normalb|invertb*|negateb*)
tex_edit -channel b -negate "${img%%.*}_out.png"
;;
invg|invertnormalg|normalg|invertg*|negateg*)
tex_edit -channel g -negate "${img%%.*}_out.png"
;;
invr|invertnormalr|normalr|invertr*|negater*)
tex_edit -channel r -negate "${img%%.*}_out.png"
;;
inva|invertnormala|normala|inverta*|negateal*)
tex_edit -channel a -negate "${img%%.*}_out.png"
;;
puregrey|pgrey|avggrey|averagegrey|midgrey|avgrgb)
tex_edit -fx '(r+g+b)/3' "${img%%.*}_avg_grey.png"
;;
modulategrey|mgrey|modulate2grey|2grey)
tex_edit -modulate 100,0 "${img%%.*}_modulate_grey.png"
;;
lumagrey|2luma|grey)
tex_edit -grayscale rec601luma "${img%%.*}_luma_grey.png"
;;
noalpha|removealpha)
tex_edit -alpha off "${img%%.*}_no_alpha.png"
;;
combine*|merge*)
red="${3}"
green="${4}"
blue="${5}"
[ -n "${6}" ] && alpha="${6}" || magick -size 4x4 xc:"#FFFFFF" "/tmp/alpha.png" && alpha="/tmp/alpha.png"
magick "${red}" -auto-orient "${green}" "${blue}" "${alpha}" -channel RGBA -combine "PNG32:${img%%.*}.png"
;;
*) echo -e "${HelpMe}"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment