Crop and slice a tall image into multiple images (pages).

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
satsun
Posts: 1
Joined: 2013-03-01T14:26:50-07:00
Authentication code: 6789

Crop and slice a tall image into multiple images (pages).

Post by satsun »

I have pages of sheet music in PNG (724×1024 each) format that I would otherwise just put in a PDF to organize but, as pages progress, the print creeps down about 8 pixels on each page and eventually runs between page breaks causing breaks in the print. Page through these examples to see what I'm talking about: 00, 01, 02. As you can see, the print goes a little further down the page each time. I'm using page 1 as a reference point for where the measure (the horizontal lines that the music sits on) sits on subsequent pages so page 1 remains untouched but I need to vertically join (append) all pages starting with page 2, crop 8 pixels from the top of the tall image I created, cut off 1024 pixels to make a new page 2, then continue the process for the remaining pages.

First step I've decided I needed done was to vertically join the individual pages starting with page 2 so that the broken print would be solid. I used this command:
convert -append notation*.png combined.png

This creates a very tall image and that's fine but now I need to figure out how to first crop 8 pixels from the top of the large vertical image, cut off 1024 pixels to make a page, and repeat those two steps to hopefully come out with new individual images/pages that are aligned and don't bleed print off the bottom.

To remove 8 pixels from the top of the tall image, I used this:
convert combined.png -crop +0+8 +repage trimmed.png

This removed 8 pixels from the top of page the tall image but now I need to figure out how to cut 1024 pixels and deliver that to an output%d.png and repeat that process.

My numbers might be shaky but I can figure that stuff out later; I'm currently trying to work out the logistics of how I'm going to make this work. The documentation talks a lot about modifying horizontal images so I'm thinking that maybe I should flip the tall/appended image to lay horizontally and do the cutting that way and then go back and flip them 90-degrees afterwards. My process may also be screwy but I'm tired so I need to take a break. I'm open to suggestions.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Crop and slice a tall image into multiple images (pages)

Post by snibgo »

A bit of fiddling shows the staves (is that the right word?) repeat at 128 or 129 pixels, even when the pages are appended. So we just need to add blank rows of pixels at the top and bottom so the result, when split into three pages, gives a regular result.

Some arithmetic shows we need an extra 9 pixels at the top, at 16 at the bottom.

3x1024 = 3072. Add 9 and 16, and we get 3097.

Code: Select all

convert n00.png n01.png n02.png -append nCombined.png

convert -size 724x3097 xc:White ( nCombined.png -geometry +0+9 ) -composite nC2.png

convert nC2.png -crop 1x3@ ncPage%%03d.png
These can be easily combined into a single command, and the principle extended to any number of pages.

EDIT: Perhaps I should have said: it needs 25 extra rows of pixels, but it doesn't matter how these are distributed between top and bottom. Putting 9 at the top means the gap to the first stave is 128/2 rows.

For (n) pages, you may find you need to add (8*n) or (9*n) rows.
snibgo's IM pages: im.snibgo.com
Post Reply