iEFdev

Code, Computers & Random Junk

2 Small Script/helpers for Images

— Updated:

2015-12-24: The post and both scripts have been updated to work with magick (IMv7)


Here are a couple of tiny scripts/helpers to help you with images… I use them almost everyday. One is to quickly make a jpg image, and the other one is to make thumbnails/miniatures …or at least, smaller images.

Since I move between Linux and OS X, they’re made to work on both. I have ImageMagick installed in my OS X, but I added sips»» (fallback) for those who doesn’t. But it will look for magick (IMv7) or convert (IMv6) on both as the default tool.

mkjpg

This is used just to be able to convert an image to a jpg real quick. Perfect for screenshots etc, where the settings usually are set to png or tiff. They can get quite large in filesize, so just by typing mkjpg and drag’n’drop the image into the terminal window is really a time saver - instead of open up an image editor, fiddle through menues, export/save as, etc etc… Helps me out (almost) every day.

mkjpg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env bash
#
# /usr/local/xbin
#
# Description:
# Convert image to jpeg.
# Quality (defaults) to 100%.
#
# Script is using 'convert' (ImageMagick).
# On OS X, it uses 'convert' if installed with a fallback on 'sips' (bundled).
#
# Usage:
#     mkjpg file.png [<procent>]
#

[[ $2 && $2 -lt 100 ]] && _q=$2 || _q=100;
_file=$(basename "$1");

if [[ `uname` == 'Darwin' ]]; then
  # IMv7
  if [[ `type magick 2> /dev/null` ]]; then
      magick "$1" -background white -alpha remove -quality $_q "${1%.*}.jpg";
      echo -e "$1\n  ${1%.*}.jpg";
  # IMv6
  elif [[ `type convert 2> /dev/null` ]]; then
      convert "$1" -background white -alpha remove -quality $_q "${1%.*}.jpg";
      echo -e "$1\n  ${1%.*}.jpg";
  else
      sips -s format jpeg -s formatOptions $_q "$1" --out "${1%.*}.jpg" 2> /dev/null;
  fi
else
  _msg="$FUNCNAME: Can't find 'magick' or 'convert'. Please install ImageMagick...";
  [[ ! `type magick 2> /dev/null` && ! `type convert 2> /dev/null` ]] && echo $_msg && return 1;
  # IMv7
  if [[ `type magick 2> /dev/null` ]]; then
      magick "$1" -background white -alpha remove -quality $_q "${1%.*}.jpg";
      echo -e "$1\n  ${1%.*}.jpg";
  # IMv6
  elif [[ `type convert 2> /dev/null` ]]; then
      convert "$1" -background white -alpha remove -quality $_q "${1%.*}.jpg";
      echo -e "$1\n  ${1%.*}.jpg";
  fi
fi

Install

Save it as mkjpg, and install it. I keep mine in my own “xbin” folder: /usr/local/xbin.

sudo install -v -m 755 mkjpg /usr/local/xbin;


# Verify
which mkjpg
ls -Ahl /usr/local/xbin/mkjpg

Usage

# mktjpg <image> [<procent>]
mkjpg Image.png
mkjpg Image.png 60

mkthumb

This one takes an image and creates a thumbnail/miniature of it. If no argument is added after the image path, it will default to 250px (wich are the desired size on the Arch forum, for example). Max size is set to 500. Larger images really doesn’t qualify as a “thumb”. smile

mkthumb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env bash
#
# /usr/local/xbin
#
# Description:
# Create thumbnails/miniatures.
# Thumb (defaults) to 250px. Max 500.
#
# Script is using 'convert' (ImageMagick).
# On OS X, it uses 'convert' if installed with a fallback on 'sips' (bundled).
#
# Usage:
#     mkthumb file.jpg         Result: file_250px.jpg
# mkthumb file.jpg 400     Result: file_400px.jpg
#

[[ $2 && $2 -lt 501 ]] && _px=$2 || _px=250;
_file=$(basename "$1");
_ext=$(echo $_file | sed -e "s/${_file%.*}//");

if [[ `uname` == 'Darwin' ]]; then
  # IMv7
  if [[ `type magick 2> /dev/null` ]]; then
      magick "$1" -resize ${_px}x${_px} -quality 100 "${1%.*}_${_px}px$_ext";
      echo -e "$1\n  ${1%.*}_${_px}px$_ext";
  # IMv6
  elif [[ `type convert 2> /dev/null` ]]; then
      convert "$1" -resize ${_px}x${_px} -quality 100 "${1%.*}_${_px}px$_ext";
      echo -e "$1\n  ${1%.*}_${_px}px$_ext";
  else
      sips -Z ${_px} -s formatOptions best "$1" --out "${1%.*}_${_px}px$_ext" 2> /dev/null;
  fi
else
  _msg="$FUNCNAME: Can't find 'magick' or 'convert'. Please install ImageMagick...";
  [[ ! `type magick 2> /dev/null` && ! `type convert 2> /dev/null` ]] && echo $_msg && return 1;
  # IMv7
  if [[ `type magick 2> /dev/null` ]]; then
      magick "$1" -resize ${_px}x${_px} -quality 100 "${1%.*}_${_px}px$_ext";
      echo -e "$1\n  ${1%.*}_${_px}px$_ext";
  # IMv6
  elif [[ `type convert 2> /dev/null` ]]; then
      convert "$1" -resize ${_px}x${_px} -quality 100 "${1%.*}_${_px}px$_ext";
      echo -e "$1\n  ${1%.*}_${_px}px$_ext";
  fi
fi

Install

Save it as mkthumb, and install it. I keep mine in my own “xbin” folder: /usr/local/xbin.

sudo install -v -m 755 mkthumb /usr/local/xbin;


# Verify
which mkthumb
ls -Ahl /usr/local/xbin/mkthumb

Usage

# mkthumb <image> [size]
mkthumb Image.jpg
mkthumb ~/Desktop/Another_Image.jpg 400

That will create:

Image_250px.jpg
~/Desktop/Another_Image_400px.jpg

Note(s) on ImageMagick

There’s a new version around (, up&coming)… I upgraded ImageMagick the other day and installed the git version. That is the new ImageMagick7 (nextgen). It’s great and has a few new intresting stuff in there, but also - it replaces convert with magick - leaving a symlink: convert -> magick.

The scripts above are updated to work with both.


If you have ImageMagick7, but also want/need IMv6, you could install it into a separate folder. Here’s what I did…

I have ImageMagick7 installed as normal in /usr/local. Then I also have ImageMagick-6.9.2 installed, using --prefix=/usr/local/ImageMagick6*, then I replaced the symlink to point at the IMv6 version: convert -> /usr/local/ImageMagick6/bin/convert.

$ cd /usr/local/bin
$ sudo ln -s ../ImageMagick6/bin/convert .
$ ls -Ahl convert

Or you could keep the symlink to magick and add convert.6 or something like that. If scripting with convert with IMv7, it will throw an error that you should use magick directly.

* Not only for this, but also for the imagick extension in PHP. I never got to compile against ImageMagick7.


If your interested in the new ImageMagick7, or want/need to install ImageMagick. Here are some links…

There are a page with binaries to, for those who can’t/won’t install from source.


Happy hacking…

/Eric

Comments