How to rotate an image on the Linux command line using imagemagick (convert)

Written by - 0 comments

Published on - Listed in Graphics Multimedia Linux


After a couple of photos were transferred from a camera to a web server, some of the pictures showed up horizontally even though were taken vertically.

Picture showing up horizontally instead of vertically

Now there are two types of people.

1) The ones opening each wrong horizontal pictures in a graphic software (GIMP, Photoshop, etc), rotating the image, saving and uploading to the web server.

2) The ones trying to automate this task.

I'm person 2.

Imagemagick convert

Imagemagick is an awesome graphic manipulation software which can be run on the command line. It is already part of the default repositories of most Linux distributions. On Debian and Ubuntu the installation is as easy as:

$ sudo apt-get install imagemagick

The "tricky" part is to find the imagemagick command; the command itself is actually called convert.

The convert command supports a lot of features and parameters (see convert --help). With the command you can change the size, quality and do many more image manipulations (even create an animated GIF from a series of images). To rotate a picture the syntax is the following:

$ convert -rotate degrees original new

Positive number of degrees (e.g. 90) rotate the picture clockwise (to the right if you have never seen a watch). Negative numbers (e.g. -90) do the opposite.

Important note is that you need to tell the convert command to use original and new file name of the picture or you might get an error like this:

$ convert -rotate 90 orig.jpg
convert-im6.q16: no images defined `orig.jpg' @ error/convert.c/ConvertImageCommand/3258.

To overwrite the original with the new rotated picture, simply use the same file name:

$ convert -rotate degrees original original

Rotating pictures in a loop

After a list of image files, which need to be rotated, is created, you can iterate over the list using a simple for loop:

ckadm@mintp /tmp/rotate $ for picture in 90-IMG_8367*.jpg 90-IMG_8379*.jpg ; do ls $picture; convert -rotate 90 $picture $picture; done
90-IMG_8367.jpg
90-IMG_8367_orig.jpg
90-IMG_8367_small.jpg
90-IMG_8367_tn.jpg
90-IMG_8379.jpg
90-IMG_8379_orig.jpg
90-IMG_8379_small.jpg
90-IMG_8379_tn.jpg

And voilà. All the pictures, including the different size variants, have rotated 90° clockwise.

Picture rotated clockwise in imagemagick


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   OpenSearch   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder