Image conversion with a right-click

Image file manipulation is typically associated with programs like Inkscape, Photoshop, or even MS Paint. A graphical user interface, however, isn’t a pre-requisite for manipulating images. Often it is overkill. Simple tasks such as resizing or converting file type can be achieved with little more than a right click (and the power of ImageMagick).

ImageMagick is a command-line, Swiss-army-knife of image manipulation. It can achieve all the usual manipulations: sharpen, rotate, resize, convert to grayscale, convert to a sketch. The list goes on… Instead of harnessing all its creative power, here I show how ImageMagick commands can be added to the right-click menu (in either Windows or Linux) to achieve simple changes without ever opening the file.

Note: Other command-line tools with similar capabilities can be used in place of ImageMagick’s magick or convert commands. For example, Ghostscript, Inkscape, or ps2pdf.

Instructions for Windows users

  1. Download  and install ImageMagick.
  2. Go to the “Send To” folder. To get there, type sendto in the address bar in Windows Explorer.
  3. In this folder, create a new text file with a .bat extension. This indicates that the file is a “batch” file, and will be treated as a command/script.
  4. Edit the batch file to include the desired command:
    • The simplest example of an ImageMagick command is
      magick input.png input_converted.jpg
    • Older versions (<v7) of ImageMagick used the convert command in place of magick
  5. Replace ‘input.png’ and ‘output.jpg’ with variables:
    • input.png becomes "%~dpnx1"
    • input_converted.jpg becomes "%~dpn1_converted.jpg"
    • This appears nonsensical, but it’s actually straightforward:

      path_explanation
      The quotation marks shown are necessary
    • The total content of the batch file is therefore the following single command
      magick "%~dpnx1" "%~dpn1_converted.jpg"
  6. To invoke the batch script, right-click and go into the Send To menu:

    send_to_menu
    The five batch files provide a glimpse of the possible file manipulation
  7. Get creative and create batch files for every manipulation you find yourself repeating.

Troubleshooting

A likely reason for the above steps to not work as expected is because Windows cannot locate the magick command. Try the following:

  1. Logging out and back in.
  2. Head to the command prompt (Press Win + R, then type cmd, and hit enter), type magick, and hit enter. If you get an error, then the command is not on your path. Easiest solution is to unistall ImageMagick and reinstall. As noted above, older versions for ImageMagick use  convert, not magick.
  3. Add a second line to your batch file with the command pause. This will display the error produced by the batch file.

Instructions for Linux Users

Implementing right-click actions on Linux depends on the file manager used. The following instructions work for Nemo, but the early steps are reasonably generic. For example, the first four steps are the same for Nautilus (changing each mention of nemo to nautilus, of course).

  1. Download and install ImageMagick
    sudo apt-get imagemagick
  2. In the directory ~/.local/share/nemo/scripts create a shell file (let’s call it convert2png.sh).
    Edit the file to contain

    #!/bin/bash
    file_in="$@"
    file_wo_ext=$(echo $file_in | cut -f 1 -d '.')
    convert "$file_in" "$file_wo_ext".png
    

    Note the use of convert. Newer versions (>v7) of ImageMagick use the command magick instead.

  3. Ensure the script is executable
    chmod +x convert2png.sh
  4. The script is now available from the right-click menu via the “scripts” sub-menu
    nemo_right_click
  5. To simplify things further, create a second file in ~/.local/share/nemo/actions (let’s call it convert2png.nemo_action)
    Edit the file to contain

    [Nemo Action]
    Name=convert2png
    Exec=/home/username/.local/share/nemo/scripts/convert2png.sh %F
    Selection = any
    Extensions=jpg;pdf;svg;
    

    Be sure to replace username

  6. Note the final line of the nemo_action file. Right-clicking on a file with any of those three extensions will provide the option directly in the right-click menu.
    nemo_right_click_actions
Advertisements

Author: hugke729

PhD student in physical oceanography

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s