prettify-screenshot-using-imagemagick.mdx
1 --- 2 title: Prettify your screenshot using imagemagick 3 date: 2020-08-06 4 description: Wanna make your screenshot a bit more fancy? Say no more, Imagemagick got you covered 5 tags: 6 - linux 7 --- 8 9 # Introduction 10 11 Let's make our screenshot prettier by using a software called [Imagemagick](https://imagemagick.org). In this post, we will add some fancy effects like backdrop, shadow, border, and rounded corner with a simple script. 12 13 # Prerequisite 14 15 Before we make the script, there are a few things that you'll need to prepare. 16 17 - **Imagemagick** - obviously 18 - **Any screenshot software** - I use [flameshot](https://flameshot.js.org/) 19 - **Clipboard** - I use [xclip](https://github.com/astrand/xclip) (optional) 20 21 After having all of that, let's make the script. 22 23 # Making The Script 24 25 ## Backdrop 26 27 Backdrop is actually just a really thicc border. Here's how to do that on imagemagick. 28 29 ```bash 30 convert source.png -bordercolor white -border 10 result.png 31 ``` 32 33 You can adjust the color by changing the `bordercolor` value. I use white because it looks nice to me. You can use HEX, RGB, and RGBA format. For more references, go to [their website](https://imagemagick.org/script/color.php). 34 35 To adjust the thickness of it, you change the `border` value. I use 10 here because it's not too big and it's not too small for me. 36 37 ## Border 38 39 Previously, we made a thicc border as a backdrop, this time we make a thin border before the shadow 40 41 ```bash 42 convert source.png -bordercolor white -border 4 result.png 43 ``` 44 45 It's basically the same, just with a different value. 46 47 ## Shadow 48 49 To give you screenshot a shadow is quite simple. Here's how to do it. 50 51 ```bash 52 convert source.png \( +clone -background black -shadow 40x5+0+0 \) \ 53 +swap -background none -layers merge +repage result.png; \ 54 ``` 55 56 To change the shadow color, adjust the first `background` value. I use black because black shadow is the only one that is acceptable to me. The `40` here is the shadow opacity, you can change it to whatever you like. The shadow radius here is `5`. This will add to your backdrop thickness. If you have `10` of backdrop and you have `5` of border radius then you'll end up with `15` of backdrop. 57 58 ## Rounded Corner 59 60 It's quite a lengthy one, but don't worry. The only thing we'd change is the border radius. 61 62 ```bash 63 # rounded corners 64 convert /tmp/image.png \ 65 \( +clone -alpha extract \ 66 -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \ 67 \( +clone -flip \) -compose Multiply -composite \ 68 \( +clone -flop \) -compose Multiply -composite \ 69 \) -alpha off -compose CopyOpacity -composite /tmp/image.png 70 ``` 71 72 The border radius here is `5`. Change every `5` to whatever you want like `10` for example but `5` is the sweet spot for me. For more technical explanation and details you can refer to [their website](http://www.imagemagick.org/Usage/thumbnails/#rounded) 73 74 ## Combining Them All 75 76 Those are all the parts that we need. Let's combine them together. Make a file called whatever you want and make it executable by using `chmod +x filename` and edit the file. 77 78 ```bash 79 #!/bin/dash 80 ``` 81 82 I use `dash` for my script, but `bash` or `zsh` will do just fine. I don't know if it'll work with `fish` though, I suppose it'll work just fine. Add your screenshot program to take the image that we will be using. I'm using [Flameshot](https://flameshot.js.org/) so it will look like this. 83 84 ```bash 85 #!/bin/dash 86 87 flameshot gui --raw > /tmp/image.png 88 ``` 89 90 I store the image on `/tmp` directory because I will copy it to my clipboard and won't be using the original image. 91 92 ```bash 93 #!/bin/dash 94 95 flameshot gui --raw > /tmp/image.png 96 97 # rounded corners 98 convert /tmp/image.png \ 99 \( +clone -alpha extract \ 100 -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \ 101 \( +clone -flip \) -compose Multiply -composite \ 102 \( +clone -flop \) -compose Multiply -composite \ 103 \) -alpha off -compose CopyOpacity -composite /tmp/image.png 104 ``` 105 106 The first effect I apply is the rounded corner and store the result in `/tmp/image.png`. 107 108 ```bash 109 #!/bin/dash 110 111 flameshot gui --raw > /tmp/image.png 112 113 # rounded corners 114 convert /tmp/image.png \ 115 \( +clone -alpha extract \ 116 -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \ 117 \( +clone -flip \) -compose Multiply -composite \ 118 \( +clone -flop \) -compose Multiply -composite \ 119 \) -alpha off -compose CopyOpacity -composite /tmp/image.png 120 121 # shadow 122 convert /tmp/image.png \( +clone -background black -shadow 40x5+0+0 \) \ 123 +swap -background none -layers merge +repage /tmp/image.png; \ 124 ``` 125 126 Next one is the shadow. I don't use the small border because it looks weird on a smaller screenshot. You can use it if you like. 127 128 ```bash 129 #!/bin/dash 130 131 flameshot gui --raw > /tmp/image.png 132 133 # rounded corners 134 convert /tmp/image.png \ 135 \( +clone -alpha extract \ 136 -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \ 137 \( +clone -flip \) -compose Multiply -composite \ 138 \( +clone -flop \) -compose Multiply -composite \ 139 \) -alpha off -compose CopyOpacity -composite /tmp/image.png 140 141 # shadow 142 convert /tmp/image.png \( +clone -background black -shadow 40x5+0+0 \) \ 143 +swap -background none -layers merge +repage /tmp/image.png; \ 144 145 # white backdrop 146 convert /tmp/image.png -bordercolor white -border 10 /tmp/image.png 147 ``` 148 149 The last effect I apply is white backdrop. Next step is optional, but if you want to you can add it as well. 150 151 ```bash 152 #!/bin/dash 153 154 flameshot gui --raw > /tmp/image.png 155 156 # rounded corners 157 convert /tmp/image.png \ 158 \( +clone -alpha extract \ 159 -draw 'fill black polygon 0,0 0,5 5,0 fill white circle 5,5 5,0' \ 160 \( +clone -flip \) -compose Multiply -composite \ 161 \( +clone -flop \) -compose Multiply -composite \ 162 \) -alpha off -compose CopyOpacity -composite /tmp/image.png 163 164 # shadow 165 convert /tmp/image.png \( +clone -background black -shadow 40x5+0+0 \) \ 166 +swap -background none -layers merge +repage /tmp/image.png; \ 167 168 # white backdrop 169 convert /tmp/image.png -bordercolor white -border 10 /tmp/image.png 170 171 # copy to clipboard 172 xclip -selection clipboard -i /tmp/image.png -t image/png 173 ``` 174 175 I added the last line to copy the result into my clipboard so I can easily paste it anywhere and don't have to delete the screenshot when I no longer need it. Here's the result. 176 177  178 179 Here's another version 180 181  182 183 # Conclusion 184 185 Imagemagick is a powerful CLI tools to manipulate an image. It can do so much more, if you're interested on that then you can go to [their website](https://www.imagemagick.org/) for some advanced guide. That's all for this post, thanks for reading and have a nice day ツ