#! /bin/zsh # # this file: # https://dataswamp.org/~incal/conf/.zsh/gfx . ~/public_html/conf/.zsh/show alias i=identify space-label () { local src=$1 label "SPACE COLA BICYCLE CLUB\nfirst Saturday every month 17:00" $src } label () { local args=$# local min_args=2 if (( $args < $min_args )); then wrong-number-of-arguments-message $min_args $args fi local str=$1 local src=$2 local x=${3:-0} local y=${4:-0} local geo=+${x}+${y} local size=${5:-25} local name=${src:r} local ext=${src:e} local dst=${name}-label.${ext} convert $src \ -gravity Center \ -pointsize $size \ -fill white \ -stroke blue \ -strokewidth 16 \ -annotate $geo $str \ -blur 0x3 \ -stroke none \ -annotate $geo $str \ $dst sw $dst } border () { local src=$1 local dst=${2:-${src:r}-border.${src:e}} convert $src \ -trim \ -bordercolor white \ -border 10 \ +repage \ $dst } cols-perc () { local img=$1 convert $img -colorspace RGB -format %c histogram:info:- | sort -n -r } # DNC? rmbg () { local src=$1 local nom=${src:r} local ext=${src:e} local new=${nom}-sbg.${ext} convert $src \ -fill none \ -fuzz 1% \ -draw 'matte 0,0 floodfill' \ -flop \ -draw 'matte 0,0 floodfill' \ -flop \ $new } # DNC? rmbg2 () { local src=$1 local nom=${src:r} local ext=${src:e} local new=${nom}-sbg2.${ext} convert $src \ -transparent white \ -fuzz 90% \ $new } dabuzz () { local q=$1 shift local fs=($@) for f in $fs; do mogrify -fuzz ${q}% -trim +repage $f done } exif-remove () { local fs=($@) local f for f in $fs; do exif --remove -o $f $f done } img-on-img () { local bg=$1 local fg=$2 convert -composite $bg $fg -gravity center comp.png } fix-png () { # https://github.com/opentoonz/opentoonz/issues/1046 # pngcrush(1) # -ow # ?? # -rem allb # removes all but tRNS and gAMA # -reduce # ?? local png=$1 pngcrush -ow -rem allb -reduce ${png} } img2a4 () { local src=$1 local name=${src:r} local ext=${src:e} local max_w=738 local max_h=590 local w=${name}-w.${ext} local h=${name}-h.${ext} convert -resize ${max_w} $src $w convert -resize x${max_h} $src $h } alias img2page=img2a4 remove-slack () { local files=($@) local name local ext local new for f in $files; do name=${f:r} ext=${f:e} new=${name}-noslack.${ext} convert $f -trim +repage $new done } stack-two () { local top=$1 local bottom=$2 local ori_top=${top:r} local ori_bottom=${bottom:r} local ori_ext=${top:e} local top_width=$(pic-width-1 $top) local bottom_width=$(pic-width-1 $bottom) local biggest_width local smallest_img local replace_top=0 local replace_bottom=0 if [[ $top_width > $bottom_width ]]; then biggest_width=$top_width smallest_img=$bottom replace_bottom=1 elif [[ $top_width < $bottom_width ]]; then biggest_width=$bottom_width smallest_img=$top replace_top=1 fi local same_size_img local base local ext if (( $replace_top || $replace_bottom )); then base=${smallest_img:r} ext=${smallest_img:e} same_size_img=${base}-same-size.${ext} convert -resize $biggest_width $smallest_img $same_size_img if (( $replace_top )); then top=$same_size_img else bottom=$same_size_img fi fi convert \ -append \ $top $bottom \ ${ori_top}-${ori_bottom}.${ori_ext} } merge-two () { local left=$1 local right=$2 local ori_left=${left:r} local ori_right=${right:r} local ori_ext=${left:e} local left_height=$(pic-height-1 $left) local right_height=$(pic-height-1 $right) local biggest_height local smallest_img local replace_left=0 local replace_right=0 if [[ $left_height > $right_height ]]; then biggest_height=$left_height smallest_img=$right replace_right=1 elif [[ $left_height < $right_height ]]; then biggest_height=$right_height smallest_img=$left replace_left=1 fi local same_size_img local base local ext if (( $replace_left || $replace_right )); then base=${smallest_img:r} ext=${smallest_img:e} same_size_img=${base}-same-size.${ext} convert -resize x${biggest_height} $smallest_img $same_size_img if (( $replace_left )); then left=$same_size_img else right=$same_size_img fi fi convert +append \ $left $right \ ${ori_left}-${ori_right}.${ori_ext} } # 1 refers to one file pic-height-1 () { local img=$1 identify $img | awk '{print $3}' | cut -d 'x' -f 2 } pic-height-dwim () { if [[ $# > 1 ]]; then local files=($@) for f in $files; do echo -n "$f " pic-height-1 $f done else local file=$1 pic-height-1 $file fi } alias pic-height=pic-height-dwim # 1 refers to one file pic-width-1 () { local img=$1 identify $img | awk '{print $3}' | cut -d 'x' -f 1 } pic-width-dwim () { if [[ $# > 1 ]]; then local files=($@) for f in $files; do echo -n "$f: " pic-width-1 $f done else local file=$1 pic-width-1 $file fi } alias pic-width=pic-width-dwim get-rect () { local img=$1 local x=$2 local y=$3 local width=$4 local height=$5 local geo=${width}x${height}+${x}+${y} local base=${img:r} local ext=${img:e} local new_name=${base}-cropped.${ext} convert -crop $geo $img $new_name } cut-from-left () { local pxl=$1 local src=$2 local name=${src:r} local ext=${src:e} local dst=${name}-l.$ext local w=$(pic-width $src) local h=$(pic-height $src) local new_w=$(( $w - $pxl )) local geo=${new_w}x${h}+${pxl}+0 convert +repage -crop $geo $src $dst } cut-from-right () { local pxl=$1 local src=$2 local name=${src:r} local ext=${src:e} local dst=${name}-r.${ext} local w=$(pic-width $src) local h=$(pic-height $src) local new_w=$(( $w - $pxl )) local geo=${new_w}x${h}+0+0 convert +repage -crop $geo $src $dst } cut-from-top () { local pixels=$1 local img=$2 local base=${img:r} local ext=${img:e} local new_name=${base}-t.${ext} local width=$(pic-width $img) local height=$(pic-height $img) local new_height=$(( $height - $pixels )) local new_geo=${width}x${new_height}+0+$pixels convert +repage -crop $new_geo $img $new_name } cut-from-bottom () { local pixels=$1 local img=$2 local base=${img:r} local ext=${img:e} local new_name=${base}-b.${ext} local width=$(pic-width $img) local height=$(pic-height $img) local new_height=$(( $height - $pixels )) local new_geo=${width}x${new_height}+0+0 convert +repage -crop $new_geo $img $new_name } img-to-square () { local img=$1 local col=${2:-white} local old_w=$(pic-width-1 $img) local old_h=$(pic-height-1 $img) local w=$old_w local h=$old_h local pn=0 local pe=0 local ps=0 local pw=0 local lines local diff local temp_img=temp.img if [[ $w > $h ]]; then diff=$(( $w - $h )) lines=$(( $diff / 2 )) pad-top-down $img $lines $col pn=$lines ps=$lines if [[ $(( $diff % 2)) > 0 ]]; then pad-top $img 1 $col $temp_img # b/c mogrify DNC? mv -f $temp_img $img pn=$(( $pn + 1 )) fi elif [[ $w < $h ]]; then diff=$(( $h - $w )) lines=$(( $diff / 2 )) lines=$(( ($h - $w)/2 )) pad-left-right $img $lines $col pw=$lines pe=$lines if [[ $(( $diff % 2)) > 0 ]]; then pad-left $img 1 $col $temp_img # b/c mogrify DNC? mv -f $temp_img $img pw=$(( $pw + 1 )) fi fi w=$(pic-width-1 $img) h=$(pic-height-1 $img) echo "$img [${old_w}x${old_h} -> ${w}x${h}] (padded $pn $pe $ps $pw)" } pad-left-right () { local img=$1 local lines=${2:-1} local col=${3:-black} mogrify -mattecolor $col -frame ${lines}x0 $img } pad-top-down () { local img=$1 local lines=${2:-1} local col=${3:-black} mogrify -mattecolor $col -frame 0x${lines} $img } pad-top () { local src=$1 local lines=${2:-1} local col=${3:-black} local dst=${4:-${src:r}-top-padded.${src:e}} convert $src -background $col -splice 0x${lines} $dst } pad-left () { local src=$1 local lines=${2:-1} local col=${3:-black} local dst=${4:-${src:r}-top-padded.${src:e}} convert $src -background $col -splice ${lines}x0 $dst } black-and-white () { local img=$1 local base=${img:r} local ext=${img:e} local new=${base}-bw.${ext} convert $img -negate -threshold 0 -negate $new } alias bw=black-and-white grayscale () { local files=($@) local base local ext local new for f in $files; do base=${f:r} ext=${f:e} new=${base}-gs.${ext} convert $f -set colorspace Gray -separate -average $new done } alias gray=grayscale ### thumb WIDTH IMG_1 [ IMGS ] thumb () { local pics=($@) local width=320 local thumbs_dir_name=thumb local file_path local name local ext local thumbs_path local thumb_final local p for p in $pics; do file_path=${p:a:h} name=${p:r} ext=${p:e} thumbs_path=${file_path}/${thumbs_dir_name} mkdir -p $thumbs_path thumb_final=${thumbs_path}/${name}-thumb.${ext} convert -resize $width $p $thumb_final done } alias make-thumb=thumb alias make-thumbs=thumb alias thumbs=thumb pscrot () { mogrify -crop 1012x752+2+0 $@ } resize-img () { local file=$1 local size=$2 local name=${file:r} local ext=${file:e} local new=${name}-${size}.${ext} convert -resize ${2}% $file $new } show-map () { local map=$1 DISPLAY=":0" feh \ --hide-pointer \ --image-bg "black" \ --reverse \ --fullscreen \ --auto-zoom \ $map & } frames () { local video=$1 local video_dir=${video}-frames mkdir $video_dir ffmpeg -i $video -vsync 1 -r 2 -an -y "$video_dir/%d.bmp" }