3

On a Mac, how can I sort about 2400 jpg according to their creation date (i. e. stat -f %SB) via terminal and convert them in that order into one pdf?

What if one jpg has been created today at 11:10 pm and a second one shortly after within the same minute at 11:10 pm? Are there seconds in the creation date, which can be taken into account?

  • You can do this easily with zsh (e.g. custom sorting). I don't have access to OSX so cannot post an answer but this should be piece of cake... – don_crissti Aug 5 '17 at 9:43
1

A simple command line with ImageMagick convert works for me.

I tested with the following command line (in a directory with 14 png files), and there will be one picture per page in the pdf file.

convert  *.png out-parrot.pdf

But there can be problems with some versions of convert

It works as intended with the version of convert in Parrot 4.4

$convert --version
Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org

but it does not work with the version of convert in Ubuntu 18.04.1 LTS (up to date in February 2019)

$ convert --version
Version: ImageMagick 6.9.7-4 Q16 x86_64 20170114 http://www.imagemagick.org

This version is 'not authorized' to write pdf files

$ convert  *.png out-ubuntu.pdf
convert-im6.q16: not authorized `out-ubuntu.pdf' @ error/constitute.c/WriteImage/1037.

$ apt-cache policy imagemagick
imagemagick:
  Installed: 8:6.9.7.4+dfsg-16ubuntu6.4
  Candidate: 8:6.9.7.4+dfsg-16ubuntu6.4
  Version table:
 *** 8:6.9.7.4+dfsg-16ubuntu6.4 500
        500 http://se.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages
        100 /var/lib/dpkg/status
     8:6.9.7.4+dfsg-16ubuntu6 500
        500 http://se.archive.ubuntu.com/ubuntu bionic/main amd64 Packages

Via an Ubuntu mailing list I had the following answer (that converting to pdf was turned off because of problems with ImageMagick vulnerabilities)

Is this a bug in ImageMagick convert, specifically for Ubuntu 18.04 LTS, or is converting to pdf turned off by intention?

The change was intentional. See https://usn.ubuntu.com/3785-1/

Thanks, Jeremy Bicha

3

It depends on the filesystem. For example, on my host, I am using the fourth extended filesystem (ext4), and stat reports thusly for files:

$ touch foo; stat foo; rm foo
  File: 'foo'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fc00h/64512d    Inode: 262155      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   ownerusername)   Gid: ( 1000/   ownerusername)
Access: 2017-06-21 14:28:16.150323827 -0700
Modify: 2017-06-21 14:28:16.150323827 -0700
Change: 2017-06-21 14:28:16.150323827 -0700
 Birth: -

So you can use the last-modified time as create time is something of a misnomer.

find /path/to/images -type f -print0 -name \*.jpg | xargs -0 stat -c "%y|%n" | sort | awk -F'|' '{print $2}'

This somewhat cumbersome construct will give you a list of files in order by last modification time (provided you have no files with | in their names).

Once you have and have reviewed this list, you can use Imagemagick's convert tool to assemble the PDF:

convert <<list of files>> outputfile.pdf 

Or, to do it all at once:

convert $(find /path/to/images -type f -print0 -name \*.jpg | xargs -0 stat -c "%y|%n" | sort | awk -F'|' '{print $2}') outputfile.pdf
  • The OP mentions nothing about parsing exif data, however. – DopeGhoti Jun 21 '17 at 21:51
  • DopeGhoti, thank you very much your reply. When I try the above command I get the following error message stat: illegal option -- c. It is true, the picture I have do not have EXIF data. – Til Hund Jun 22 '17 at 20:17
  • Do you know which filesystem is in question here? stat -c works for me in my tests. – DopeGhoti Jun 22 '17 at 20:31
  • I might add that I want to use the above command on a Mac. I did not mentioned it before as I thought it would be irrelevant to the question. The filesystem would be HFS Plus or Mac OS Extended (Journaled). – Til Hund Jun 23 '17 at 8:04
  • For a Mac, use stat -f "%a|%N". – DopeGhoti Jun 28 '17 at 16:05
0

Install ImageMagick. Assuming the JPG images are in ~/images and the file names don't contain any spaces (nor any of \[*?) and you have a directory ~/combined:

convert -combine -append $(ls -tr ~/images/*.jpg) ~/combined/all.jpg

or

  convert -combine -append $(ls -tr ~/images/*.jpg) ~/combined/all.pdf

If the images are not the same size, you will get warnings. -append combines images top to bottom. Change to +append and images will combine left to right.

Time: Although ls -l shows time down to hour:minute, Linux keeps track of the access, modify, and age/change times down to the nano second I believe. So sorting by ls -tr does account for even fractions of seconds.

  • 1
    Don't parse ls. – DopeGhoti Jun 21 '17 at 21:43
  • Thank you, Deathgrip, for answering. How can I convert the pictures into a single pdf as asked above? – Til Hund Jun 21 '17 at 21:43
  • @DopeGhoti - huh? the command works fine and I tested it using CentOS 7. – Deathgrip Jun 21 '17 at 21:46
  • It will fail if any of those file names contains [[:space:]]... – don_crissti Jun 21 '17 at 21:47
  • Trying to parse the output of an ls is an exercise which, while it may seem to work, is a terribly bad habit to get into for many many security-related reasons, which the link included in my answer goes into in great detail. More detail can be found here. – DopeGhoti Jun 21 '17 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.