[SOLVED]Dynamic append

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
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

[SOLVED]Dynamic append

Post by zdrassvouitie »

Hello,

I have a first program which creates a Repository (/home/working) containing multiples png files.
Each png file represent an exercice (I teach chemistry).
I succeed to write in each filename the dimensions of the image.

For instance, I have my Repository with:
/home/working/exo1#2400#816.png
/home/working/exo2#2400#676.png
/home/working/exo3#2400#450.png
/home/working/exo4#2400#1256.png
/home/working/exo5#2400#498.png
/home/working/exo6#2400#2244.png
/home/working/exo7#2400#832.png
/home/working/exo8#2400#524.png
/home/working/exo9#2400#624.png
(Sometimes I have less files, sometimes more, generally between 5 et 30 exercices/png-files.)

They all have the same width (2400px, but I can change it very easily if necessary) but they all have a different height (816px, 676px, 450px, 1258px,498px....).
I choose 2400px because I read that the dimensions of A4 page is 2480*3508 in 300 dpi.

I would like to generate a pdf file with all those exerices (keeping the original size of each png file), so I started to look on internet.
My first researchs always gave me the same example : the command line "convert *.png multipage.pdf"
This command doesn't work for me because the final pdf file have 9 pages (as much as the initial number of png files. It is a huge waste of paper when I have 30 exercices to print for the students).
Then I found the "-append" option but the command line "convert -append *.png final.pdf" doesn't work for me neither because this generates a final pdf file with 1 big page (extra tall one, so you can't read it when it's printed on A4)

So I suppose I need a special script/command in order to append (vertically) the exercices/png-files, and this "append function" need to be dynamic (because the number of files and their heights may vary : sometimes, 9 png files will "fill" 3 pages A4, sometimes 9 png files will "fill" only one page A4).

Considering the example below, a simple/stupid script (which append the files, one by one, till the total height is less than 3500) could generate a pdf file containing 3 pages A4, the first one contains exo1+exo2+exo3+exo4, the second page contains exo5+exo6 and the last page contains exo7+exo8.

Is it possible to do this? And how?

Thank you very much for your time and consideration.

PS : I have a drawing of what I need, in case you don't understand what I mean, but I don't know how to add it.
Last edited by zdrassvouitie on 2014-09-17T11:10:58-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Dynamic append

Post by fmw42 »

PS : I have a drawing of what I need, in case you don't understand what I mean, but I don't know how to add it.
Upload it to some free hosting service such as dropbox.com and put the URL here.

You should be able to write a script that appends each image, then checks the current height and next image height and adds them. Then if less than your desired height, add the new one.

Pleases identify your IM version and platform, since the script will depend upon platform and some windows IM syntax is different from unix syntax.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Dynamic append

Post by snibgo »

I would do it as a script that built the commands, eg:

Code: Select all

convert /home/working/exo1#2400#816.png \
/home/working/exo2#2400#676.png \
/home/working/exo3#2400#450.png \
/home/working/exo4#2400#1256.png \
/home/working/exo5#2400#498.png \
-append \
out1.pdf

convert /home/working/exo6#2400#2244.png \
/home/working/exo7#2400#832.png \
-append \
out2.pdf

convert /home/working/exo8#2400#524.png \
/home/working/exo9#2400#624.png \
-append \
out3.pdf
From your forward-slashes, I guess you use Unix and bash, which I know nothing about, so I'll back out there. This is really a scripting issue, not an IM issue.
snibgo's IM pages: im.snibgo.com
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

Re: Dynamic append

Post by zdrassvouitie »

Hello,

Here is a drawing (with another example of 6 png files) of what I want to do.
Image
https://docs.google.com/file/d/0B3fAjPx ... ItWTA/edit

I'm using fedora 20, perl 5 (v5.16.3) and ImageMagick 6.7.8-9 2013-03-10.
I'm working on a kind of research engine, that I'm coding in perl. The objective is : I type keywords, and the program finds me automatically the corresponding exercises, then it generates a pdf file with all the exercises, ready to print for the students.

Snibgo:
thanks for your answer, but your command lines are not dynamic at all, I repeat : the number and the size of the png files may vary.
For instance, if the height of the exo3 changes to 3000px, I will have to type another command, so I'm not interested in your solution, thank anyway.

fmw42:
You should be able to write a script that appends each image, then checks the current height and next image height and adds them. Then if less than your desired height, add the new one.
Perhaps I'm able, but I don't know how to start to code this!!
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Dynamic append

Post by glennrp »

zdrassvouitie wrote: Snibgo:
thanks for your answer, but your command lines are not dynamic at all, ...
You've misunderstood snibgo's answer. While the command-line script itself is static, it
is a one-time-use script that you generate with a dynamic script (shell, perl, or whatnot). So
you'd do something like

Code: Select all

gen-script files   // creates run-script.sh that is exemplified by snibgo's reply
run-script.sh               // runs convert -append commands
BTW you'll probably need to escape the "#" in the filenames in snibgo's example.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Dynamic append

Post by fmw42 »

Your file cannot be downloaded unless one has an account.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Dynamic append

Post by snibgo »

Thanks, Glen, yes, that's what I meant.

I gave examples of commands that would be created by the main script (which is perl or bash or whatever). Those commands could be created as strings and then executed.

I don't know perl.
snibgo's IM pages: im.snibgo.com
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

Re: Dynamic append

Post by zdrassvouitie »

glennrp wrote:
zdrassvouitie wrote: Snibgo:
thanks for your answer, but your command lines are not dynamic at all, ...
You've misunderstood snibgo's answer. While the command-line script itself is static, it
is a one-time-use script that you generate with a dynamic script (shell, perl, or whatnot). So
you'd do something like

Code: Select all

gen-script files   // creates run-script.sh that is exemplified by snibgo's reply
run-script.sh               // runs convert -append commands
BTW you'll probably need to escape the "#" in the filenames in snibgo's example.

Ok guys, sorry for misunderstanding you.
But I still have no idea how to make that first script which examine the height of my png files one by one and add their filename, then do the convert -append thing.
I'm going to dig this a little bit by my own, I let you know.
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

Re: Dynamic append

Post by zdrassvouitie »

fmw42 wrote:
You should be able to write a script that appends each image, then checks the current height and next image height and adds them. Then if less than your desired height, add the new one.
I think this is the way I'm going to do it, I have a small idea how to code that in perl.

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

Re: Dynamic append

Post by fmw42 »

You can also loop over each image and just figure out from the heights what grouping of images you need and then at the end run several convert .. appends to create the few resulting images you need.
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

Re: Dynamic append

Post by zdrassvouitie »

I'll try to do it both ways, probably one way will seem easier for me...
I'll look at it next week.

Thanks all of you for your ideas and advises.

(The problem is that the command line "convert -append" in a terminal is a little bit different than the code lines in perl. I'll have to check this out first because I would like to have just one program that do everything, and 90% are already programmed in perl, so....)
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

Re: Dynamic append

Post by zdrassvouitie »

Hi guys,

It went faster than I thought.
I just succeed to finish a script that read the files, add them by height, then prepare the command lines I will need to put inside IM.
Thanks again.
zdrassvouitie
Posts: 7
Joined: 2014-09-13T14:40:40-07:00
Authentication code: 6789

Re: Dynamic append

Post by zdrassvouitie »

I can't find how to add a "SOLVED" tag to this topic!!
It's not in the FAQ, neither in the "Read this first before posting a new topic" post...
Somebody know how to tag a topic as "SOLVED" ?
I just add it in the title (but I don't know if it's the correct way to do it)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: [SOLVED]Dynamic append

Post by fmw42 »

Adding it to the title is the only way I know to do it. Perhaps there is some other, but this seems sufficient.
Post Reply