A few hundred images (in the right order) to pdf

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?".
jamtat
Posts: 47
Joined: 2010-05-10T16:51:23-07:00
Authentication code: 8675308

A few hundred images (in the right order) to pdf

Post by jamtat »

I've got a few hundred images (jpg) I'm trying to make into a single, multi-page (one image per page) pdf. In case it's relavent each image is a photo of a page from an old manuscript. I've found some tips on how to combine all these images into a single pdf and am close to success, but am not quite there. What's come pretty close to working for me is the command convert -page Letter+57+62 *.jpeg -adjoin 000.pdf. That almost gives me what I need except the order of images is wrong: the images are inserted into the pdf in the order in which they display by default when issuing the command ls from the command line within the directory. What I need, on the other hand, is the ordering you get when you issue the command ls -v ("natural sort," according to the ls man page). So, my question: is there some way to get the command convert -page Letter+57+62 *.jpeg -adjoin 000.pdf to convert the images in the order I get when I issue the command ls -v?

Thank you,
James
Last edited by jamtat on 2010-05-10T22:22:32-07:00, edited 2 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: A few hundred images (in the right order) to pdf

Post by snibgo »

Is "ls" strictly alphabetical? If so, try naming your files accordingly, with leading zeroes as required.
snibgo's IM pages: im.snibgo.com
jamtat
Posts: 47
Joined: 2010-05-10T16:51:23-07:00
Authentication code: 8675308

Re: A few hundred images (in the right order) to pdf

Post by jamtat »

Thanks for your reply. My files are named, for example ms-pg1.jpg ms-pg1b.jpg ms-pg2.jpg ms-pg2b.jpg etc. There are almost 600 images in total. Before getting into renaming the whole lot I thought I'd see if there might be some way to get convert to honor the order of a command like ls -v. If there's a way to do that it could cut the amount of time I'll have to spend on this project in half, at least.

James
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: A few hundred images (in the right order) to pdf

Post by fmw42 »

You might try making a list of your images from their directory, then sort them as you desire (using unix? sort), then write a script to convert them in that order to your pdf, adding each page to the end of the last iteration of the loop.

If you were planning on using wildcards for the input, then take the sorted list and rename then appropriately so that the wild card will work.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: A few hundred images (in the right order) to pdf

Post by anthony »

I have a perl script called mv_renum that adds leading zeros to a number in the file name, so that they sort alphabetically.

http://www.cit.griffith.edu.au/~anthony ... /#mv_renum

By the way if you rename the script mv_reseq then it will change the numbers to remove or insert gaps between the numbers. rename files with the numbers 3 5 7 to the numbers 1 2 3
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jamtat
Posts: 47
Joined: 2010-05-10T16:51:23-07:00
Authentication code: 8675308

Re: A few hundred images (in the right order) to pdf

Post by jamtat »

Thanks for the replies. The perl script looks like the best possibility for my purposes since my scripting skills are virtually non-existent. But I think the script is unhappy with the letter I've appended (the letter "b") to about half of my numbers, at least when it comes to triple-digit numbers. For example, I got the error message Filenames "ms-pg100b.jpeg" and "ms-pg100.jpeg" have the same number -- ABORTING when trying to run the script within the directory where my files are located. It would be great if I could make your script work, though.

James

PS By way of explanation, in old manuscripts like the one I'm working with, they did not number each page individually, but only right-hand leaves. So leaf one has two pages, one on the front of the leaf and the other on the back. One page has the number "1" on it, but the back side has no number, so I've labelled it "1b."
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: A few hundred images (in the right order) to pdf

Post by anthony »

You can also try downloading a different more traditional perl renaming script "mv_perl"

http://www.cit.griffith.edu.au/~anthony ... e/#mv_perl

Inside the script is a commented out "mv_renum" pattern.
But you can still use it for a 'renum' operation....

mv_perl 's/\d+/sprintf "%05d", $&/eg' files....

Which will make all numbers in the filename 5 digits long with leading zeros.

yes it is very tricky, but it works.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jamtat
Posts: 47
Joined: 2010-05-10T16:51:23-07:00
Authentication code: 8675308

Re: A few hundred images (in the right order) to pdf

Post by jamtat »

Thanks again for your reply. Modifying the perl script looked a bit daunting to me so I decided to try some things with a utility I've previously used--a utility called "mmv." It didn't help much. Again, the problem seems to me to be that the convert command finds files using default ls output. That means that 100b gets found before 100, 10 only after 109, 11 only after 119, and 1b only after 199 and 19. To confirm this I stripped the leading letters from my file names (the ms- part) and was left only with numbers (e.g., 1.jpeg, 1b.jpeg, 2.jpeg, 2b.jpeg, etc). So running convert still seems to me like it's going to put my pages out of proper order unless I can make convert honor the order you get when running ls with the -v option. In short, I can't see at the moment how renumbering or adding zeros is going to help me do this conversion. Am I missing something?

James
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: A few hundred images (in the right order) to pdf

Post by fmw42 »

can you use sed to find any number followed by a period and rename it to the same number followed by the letter "a" followed by a period?

This may not be elegant, but it works:

list="1.jpg 1b.jpg 2.jpg 2b.jpg 3.jpg 3b.jpg"
echo $list | sed 's/\([0-9]*\).jpg/\1a.jpg/g; s/ba/b/g'
1a.jpg 1b.jpg 2a.jpg 2b.jpg 3a.jpg 3b.jpg
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: A few hundred images (in the right order) to pdf

Post by snibgo »

In short, I can't see at the moment how renumbering or adding zeros is going to help me do this conversion. Am I missing something?
I suggest you rename them thus:

ms-pg1.jpg ==> 001.jpg
ms-pg1b.jpg ==> 002.jpg
ms-pg2.jpg ==> 003.jpg
ms-pg2b.jpg ==> 004.jpg
etc

If you have thousands of images, you should use 0001.jpg, etc.
snibgo's IM pages: im.snibgo.com
jamtat
Posts: 47
Joined: 2010-05-10T16:51:23-07:00
Authentication code: 8675308

Re: A few hundred images (in the right order) to pdf

Post by jamtat »

'can you use sed to find any number followed by a period and rename it to the same number followed by the letter "a" followed by a period?'

Thanks for your reply. I don't have much experience with sed but would be willing to try using it. Only I don't see how the solution you've proposed gets around the problem of 100, 109, 11, 199, and 19 (etc) being found before 1b by convert. For further context, here's a list of the first 20 or so files that convert finds in the target directory:
0000100b.jpeg
0000100.jpeg
0000101b.jpeg
0000101.jpeg
0000102b.jpeg
0000102.jpeg
0000103b.jpeg
0000103.jpeg
0000104b.jpeg
0000104.jpeg
0000105b.jpeg
0000105.jpeg
0000106b.jpeg
0000106.jpeg
0000107b.jpeg
0000107.jpeg
0000108b.jpeg
0000108.jpeg
0000109b.jpeg
0000109.jpeg
000010b.jpeg
000010.jpeg
100 through 110 are somewhat ordered, though the numbers with "b" appended should be after rather than before the numbers without. Your suggestion of appending an "a" would resolve that problem. But it wouldn't resolve the problem of 100 being the very first page the convert script finds, nor would it resolve the problem of two-digit numbers being found after three-digit numbers (see the number 10 above). So, though your suggestion might be some part of a possible resolution, it doesn't look like it would be of much help as you've proposed it--assuming I've understood correctly.

James
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: A few hundred images (in the right order) to pdf

Post by snibgo »

This is why you need leading zeroes as required.

You might need 7 digits, so:
1 => 0000001
2 => 0000002
10 => 0000010
11 => 0000011
100 => 0000100
etc
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: A few hundred images (in the right order) to pdf

Post by anthony »

jamtat wrote:Thanks again for your reply. Modifying the perl script looked a bit daunting to me
No need to modify, Just use it as I said.

mv_perl 's/\d+/sprintf "%05d", $&/eg' files...
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
jamtat
Posts: 47
Joined: 2010-05-10T16:51:23-07:00
Authentication code: 8675308

Re: A few hundred images (in the right order) to pdf

Post by jamtat »

snibgo wrote:I suggest you rename them thus:

ms-pg1.jpg ==> 001.jpg
ms-pg1b.jpg ==> 002.jpg
ms-pg2.jpg ==> 003.jpg
ms-pg2b.jpg ==> 004.jpg
etc

If you have thousands of images, you should use 0001.jpg, etc.
Thanks for your comments. My initial idea for creating this document was to insert each image manually into a separate page of an OpenOffice document. That seemed a bit tedious and time-consuming so I asked for alternate suggestions at the OOo forums. Someone suggested using imagemagick to further automate the process, and when I discovered mutliple images could be concatenated together into a single pdf file, it seemed like the ideal resolution. But if doing this means I have to go and manually rename all those files as you've suggested, I think I wouldn't save much time over just using OpenOffice as I'd originally intended (I did actually do about 20 pages of the OpenOffice document so far, so I have a bit of a jump on things).

James
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: A few hundred images (in the right order) to pdf

Post by snibgo »

Why do it manually, when Anthony has a script?
snibgo's IM pages: im.snibgo.com
Post Reply