How to use imagemagick from shell_command?

Hi, this is my first post and I’m fairly new to Home Assistant. I’m running the supervised version on a Pi4.
I want to be able to process a still image taken from my front door camera and process (rotate & crop) it before sending it via Pushover when somebody rings my doorbell.

I already got the automation working using shell_command to curl the snap.jpg from the camera - but for the cropping and rotating part I’d need ImageMagick which isn’t available in the default setup.

What would be the best way to approach this? I searched the forums and found advice to ssh into the docker container and apk-install ist. But which container? homeassistant? Tried that - didn’t work. Well it was available in the bash shell after install - but not from shell_command.

Same thing with my attempt to use the plugin “SSH & Web Terminal” which offers the ability to make Alpine packages available on startup. But still, shell_command - which I use in my automation - has no access to the convert or magick command.

Any advice on how to make this work?

You could try writing a shell script which you then trigger using a shell command in Home Assistant.

[image_convert.sh]

#!/bin/sh
curl -O https://www.home-assistant.io/images/hero_screenshot.png
convert hero_screenshot.png -rotate 90 hero_rotated.png

You could then set up as shell_command like

shell_command:
  convert_image: 'sh /config/<file-location>/image_convert.sh'

where convert is part of the imagemagick Alpine package.

Something to watch out for is having to reinstall imagemagick within the homeassistant container every HA core update. See this post for information on setting up an automation to do this for you.

This is because the SSH & Web Terminal addon runs as a separate container compared to the homeassistant core container. You are able to access the same files because both mount the same directories. However, any APKs installed in one container are unavailable in the other.

@rootnegativ1, thank you for the explanation and the advice. I will have a shot at the (automated) shell script when I find some time (i.e. if kids and wife will let me).

In the meantime I tried several different approaches - one of them suddenly lead to success. Unfortunately I’m not sure which one, since I also forgot to restart core after each and every attempt. I guess it was this one, where I did the following:

docker exec -it homeassistant /bin/bash
apk add --update imagemagick

Well I guess it’s also only a temporary fix until the next update or restart of the system.

Restarting HA or the system will not revert your changes to the container. Only when you update HA (download a new image the container is based on) will those changes be erased.

TBH I’ve been trying to figure out a very similar setup of running 3rd party shell commands within HA. I used to use the stdin feature of the SSH & Web Terminal addon. Unfortunately, this was removed in a release late last year. The reason behind the removal make sense security-wise, but it’s still a bummer.

Anyways, in the release notes of that change @frenck made a comment about using ssh in its place. This is another rabbit hole you may consider pursuing as it keeps the HA core container in an unmodified immutable state. This is objectively better long-term as the end-user is no longer required to edit the core container.

Process (so far):

  1. Configure the SSH & Web Terminal addon by including any needed APKs to its configuration. This way they are automatically added each time that container is recreated.
  2. Create and test my script inside the addon
  3. Setup SSH keys between the core and addon containers
  4. Create a shell_command in HA to ssh into the addon container and run my script, e.g.
shell_command:
  command_1: 'echo "SOME COMMAND" | ssh [email protected]'

Full disclosure: I’m still working this out myself and currently use the process I described in an earlier post. It’s a (mostly always) fun learning adventure!

P.S. sorry for the info dump