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?

1
  • 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... Commented Aug 5, 2017 at 9:43

3 Answers 3

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 
6
  • The OP mentions nothing about parsing exif data, however. Commented Jun 21, 2017 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. Commented Jun 22, 2017 at 20:17
  • Do you know which filesystem is in question here? stat -c works for me in my tests. Commented Jun 22, 2017 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). Commented Jun 23, 2017 at 8:04
  • For a Mac, use stat -f "%a|%N". Commented Jun 28, 2017 at 16:05
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

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.

6
  • 1
    Don't parse ls. Commented Jun 21, 2017 at 21:43
  • Thank you, Deathgrip, for answering. How can I convert the pictures into a single pdf as asked above? Commented Jun 21, 2017 at 21:43
  • @DopeGhoti - huh? the command works fine and I tested it using CentOS 7. Commented Jun 21, 2017 at 21:46
  • It will fail if any of those file names contains [[:space:]]... Commented Jun 21, 2017 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. Commented Jun 21, 2017 at 21:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.