Pages

13 November 2012

Split an image into two individual files (ImageMagick).

This is a follow up article on the image re-size article previously written, and describes how to split an image file into two halves vertically. 

I use this often when I have a scanned music CD cover, which when opened, has two pages and I want to get the two pages as individual image files, using the ImageMagick package in linux. 

Although the example shown below splits the image into two vertical halves, you can obviously split an image into three vertical portions, or split it horizontally into two halves for example.

N.B. The original file is not kept, so if you still want the original file then make a copy of it before running the mogrify command.
  1. clive@dogmatix > cd /tmp
  2. clive@dogmatix > cp /home/clive/Wimpy1983.jpg .
  3. clive@dogmatix > cp Wimpy1983.jpg Wimpy1983-orig.jpg
  4. clive@dogmatix > ll *jpg
  5. -rw-r--r-- 1 clive clive 560927 Nov 13 12:34 Wimpy1983.jpg
  6. -rw-r--r-- 1 clive clive 560927 Nov 13 12:35 Wimpy1983-orig.jpg
  7. clive@dogmatix > identify -format '%wx%h' Wimpy1983.jpg
  8. 1280x944
  9. clive@dogmatix > mogrify -crop 50%x100% +repage Wimpy1983.jpg
  10. clive@dogmatix > ll Wimpy1983-*jpg
  11. -rw-rw-r-- 1 clive clive 308303 Nov 13 12:36 Wimpy1983-0.jpg
  12. -rw-rw-r-- 1 clive clive 302236 Nov 13 12:36 Wimpy1983-1.jpg
  13. -rw-r--r-- 1 clive clive 560927 Nov 13 12:35 Wimpy1983-orig.jpg
  14. clive@dogmatix > identify -format '%wx%h   %f\n' Wimpy1983-[0-1].jpg
  15. 640x944   Wimpy1983-0.jpg
  16. 640x944   Wimpy1983-1.jpg
  17. clive@dogmatix >
The command used on line 9 can be explained as follows:
-crop 50%x%100% means make the first resulting image 50% the width of the original image by 100% the height of the original image.
-repage means that the working canvas of the original image is shifted past the first resulting image and is then saved to the second resulting image.

As you can see from lines 7 & 8 the original file's dimensions are 1280x944 pixels. Then after we run the mogrify command (line 9) we end up with two individual files called "Wimpy1983-0.jpg" and "Wimpy1983-1.jpg". The original file (Wimpy1984.jpg) has now been deleted.

Then when I run the identity command (line 14) on each of those two files they are now both 640 pixels wide and 944 pixels high.

Please leave me any comments if anything is unclear, I always try to keep my notes as simple as possible, so that even linux beginners can understand. I have always found that examples are the best way to learn anything, so I always try and show some examples (some of my every day examples).

No comments:

Post a Comment