Created
June 12, 2017 06:53
-
-
Save yoshinari-nomura/867b1e172da075e3b866cfff7b820756 to your computer and use it in GitHub Desktop.
Create circle icon with green frame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # usage: make-icon-badge.sh [diameter_in_cm] src_file dst_file | |
| # make-icon-badge.sh 3.6 nom-with-cray.jpg nom-with-cray.png | |
| # Crop or mask an image into a circle - Stack Overflow | |
| # https://stackoverflow.com/questions/999251/crop-or-mask-an-image-into-a-circle | |
| # | |
| # I want to change DPI with ImageMagick without changing the actual byte-size of the image data | |
| # https://superuser.com/questions/479197/i-want-to-change-dpi-with-imagemagick-without-changing-the-actual-byte-size-of-t | |
| # | |
| function min() { | |
| if [[ "$1" -lt "$2" ]]; then | |
| echo "$1" | |
| else | |
| echo "$2" | |
| fi | |
| } | |
| function calc() { | |
| awk "BEGIN {print $1}" | |
| } | |
| diameter_in_cm="$1" | |
| src_file="$2" | |
| dst_file="$3" | |
| src_w=$(identify -format "%w" "$src_file") | |
| src_h=$(identify -format "%h" "$src_file") | |
| diameter_in_pixel=$(min $src_w $src_h) | |
| pixels_per_cm=$(calc "$diameter_in_pixel / $diameter_in_cm") | |
| mm_in_pixels=$(calc "int($pixels_per_cm / 10 + 0.5)") | |
| # Geometory for inner circle mask | |
| mask_radius=$(calc "int($diameter_in_pixel / 2)") | |
| mask_size=${diameter_in_pixel}x${diameter_in_pixel} | |
| xoff=$(calc "($src_w - $diameter_in_pixel) / 2") | |
| yoff=$(calc "($src_h - $diameter_in_pixel) / 2") | |
| crop_geom=$mask_size+$xoff+$yoff | |
| # Geometory for green circle frame | |
| frame_radius=$(calc "$mask_radius + $mm_in_pixels") | |
| frame_diameter=$(calc "$frame_radius * 2") | |
| frame_size=${frame_diameter}x${frame_diameter} | |
| convert \ | |
| -units PixelsPerCentimeter -density $pixels_per_cm \ | |
| -size $frame_size canvas:none -fill green -draw "circle $frame_radius,$frame_radius $frame_radius,0" \ | |
| \(\ | |
| "$src_file" -crop $crop_geom \ | |
| \( -size $mask_size canvas:none -fill white -draw "circle $mask_radius,$mask_radius $mask_radius,0" \) \ | |
| -compose CopyOpacity -composite \ | |
| \)\ | |
| -compose Over -gravity center -composite "$dst_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment