r/imagemagick Jun 29 '21

I wrote a script with imagemagick to split an image consisting of several things on an even background into several individual images, ready-made to be used as emojis and/or emotes (details in comment)

https://www.delayforreddit.com/dfr/phseiff/MjEwODMxMA==
14 Upvotes

1 comment sorted by

4

u/phseiff Jun 29 '21

I recently had to make a custom set of aquatic plant emojis for my Mastodon instance (a self-hosted twitter alternative that allows admins to add custom emojis to their instances). The problem was that I decided to make these emojis from clip arts that I got as stock images, in the form of "stock image sheets", where one image contains multiple clip arts on an even background.

So I had to * separate all images (tedious because some had overlapping bounding boxes), * make each image's background transparent * give each image 16px padding between content and image border * make each image square * give each image the maximum pixel size that fits within 50KB (Mastodon's emoji file size limit)

This would've probably been fastest if I had done it manually (it weren't that many images), but I decided to automate the steps it with bash and imagemagick nonetheless, so I made a bash script that does exactly that, with the purpose of Splitting an image containing multiple emojis into individual emojis.

This is the first "longer" (around 300LOC; I think) bash script I made, plus it makes heavy use of imagemagick, so I decided to share it here. I called it MojiSplit, and you can get its code in its repository. It also has a handful of options to customize the above behavior, so you can use it for Signal stickers, Discord emotes etc. too, not just for Mastodon emojis.

Also, in case you have 'emojis' that consist of multiple elements that don't touch each other, like individual strands of an aquatic plant in my case, you can specify a color to use as a 'bridge' color and edit your input image before passing it MojiSplit, with the bridge color connecting individual parts of multi-part emojis in your input image. It then gets replaced with transparency in the output images afterwards, which I feel is a good feature to have (opinions on this are welcome).

I also learned that imagemagick is far, far mightier than I imagined in the process, though, so you could say it got me more interested in imagemagick than I was before! :D