aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/image/shellrc
blob: 81e029b2e86707959115e8273a2ced348d5b4d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Image Utilities

# Concatenate image vertically
image.vertical() {
  if [ ! $# = 0 ]; then
    echo 'Usage: image.vertical input1.jpg input2.jpg'
    return 0
  fi

  ext=${1##*.}
  convert "$@" -append $(basename -s .$ext $1)-$(basename -s .$ext ${@: -1}).${format:-$ext}
}

# Concatenate image horizontally
image.horizontal() {
  if [ ! $# = 0 ]; then
    echo 'Usage: image.horizontal input1.jpg input2.jpg'
    return 0
  fi

  ext=${1##*.}
  convert "$@" +append output.$ext
}

# Export image with data url format
image.from_data_url() {
  [ -z "$1" ] && echo File name needed && return 1
  xsel -ob | sed -E 's/^.+,//' | base64 -d >$1
  identify $1
}

# Resize image
image.resize() {
  if [ ! $# = 2 ]; then
    echo 'Usage: image.resize INPUT.jpg 80%'
    return 0
  fi

  ext=${1##*.}
  echo OUTPUT: output.$ext
  convert -resize $2 $1 output.$ext
}

# Upload image to vps
# Usage:
#   image.upload foo.png
image.upload() {
  local month=$(date +%Y-%m)
  local filename=${2:-$(date +"%d_%Hh%Mm%Ss").${1##*.}}
  local fpath='$HOME/data/s3.photos/'$month
  cat $1 | ssh vps "mkdir -p $fpath && cat >$fpath/$filename" && \
  echo https://topo.tw/photos/$month/$filename || \
  echo Fail to upload
}

# Create an image contains text
image.text() {
  convert \
    -size 230x130 \
    -background lightblue \
    -font Noto-Sans-Bold \
    -pointsize 25 \
    -fill black \
    -gravity Center \
    caption:"${1:=foo}" \
    -flatten \
    "${2:=foo}".jpg
}