Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a bash script that cuts images into pieces using image magick?

I have a number of input images that contain multiple smaller images, all of them in a single row. All the contained images are the same size. So, for example, the image input.png may be 480x48 and contain 10 48x48 images, all in one row.

Using the imagemagick convert tool (or any other tool supplied with the defaul imagemagick suite), I want to write a bash script that takes an input image, the number of images to cut, and then cuts them all into individual images.

The user interaction i can do, but I've not been able to get convert to do the actual cutting. Can anyone suggest something? From reading the manual pages, i think this should work:

convert 'input.png[0x0+48+48]' output.png

but I get an error:

convert: no pixels defined in cache tb_icons_l.png' @ magick/cache.c/OpenCache/3572. convert: No IDATs written into file 1.png' @ coders/png.c/PNGErrorHandler/1391.

Any ideas?

like image 595
Thomi Avatar asked Sep 29 '09 09:09

Thomi


2 Answers

I would do it like that:

convert input.png -crop 48x48  +repage  +adjoin  output_%02d.gif

Read more at Tile Cropping, in ImageMagick documentation.

like image 78
psychoschlumpf Avatar answered Nov 15 '22 11:11

psychoschlumpf


I believe you've got the position and size swapped. Try:

convert 'input.png[48x48+0+0]' output.png

The third image would be:

convert 'input.png[48x48+96+0]' output.png

Or

convert 'input.png[48x48+0+96]' output.png
like image 30
Dennis Williamson Avatar answered Nov 15 '22 11:11

Dennis Williamson