G
G
Georgy Pelageikin2015-09-21 22:17:02
linux
Georgy Pelageikin, 2015-09-21 22:17:02

How to make ImageMagick take file order correctly?

Hello. There are .png files, you need to convert them to pdf with compression. I do it like this:

convert -quality 75 *.png result.jpg && convert *.jpg result.pdf && rm *.jpg

But here's the problem. PNG files are sorted by name (in alphabetical order). But the resulting JPGs are no longer complete , the
result-0, result-1 ... result-9, result-10>
problem is that IM has a primitive sorting mechanism by name - numbers are not converted into numbers, as a result result-10.jpg in pdf will go in second, not 11th, as it should . What to do (except for a crutch in the form of renaming by hands)?
PS Directly from .png the file is 2 times larger, so I do intermediate compression to .jpg. Maybe there are better ways?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
D
Dimonchik, 2015-09-21
@dimonchik2013

wincmd.ru/multirename.php is speeding up the crutch
www.imagemagick.org/Usage/files and these are the rules:
use wildcard not "*", but ? - one character
i.e. first shove everything with one, then with two, etc.
but safer - rename

G
Georgy Pelageykin, 2015-09-21
@ArXen42

#!/bin/bash
mkdir converted

for file in *.png;
do
  convert -quality 70 $file 'converted/'result.jpg
  mv 'converted/'result.jpg 'converted/'$file'_result'.jpg
  echo "Converted..."
done

echo "Done"

Thanks to the unknown author for the answer. In theory, this should be processed by IM in the same order as the source files.

N
Nadz Goldman, 2015-09-21
@nadz

Oh. You read man there. What can be thrown out of PNG and in what "quality" to get the output.
But my answer to the question is simple: use xargs.
Well, or we do it corny, for example, ls *.png | convert ...
But there is nothing better than xargs in this case.

M
Maxim Moseychuk, 2015-09-22
@fshp

convert `ls *.jpg | sort -n` result.pdf

W
wwwtaras, 2016-07-04
@wwwtaras

example input_0.png input_1.png input_2.png ...input_10.png
result result_0.png result_1.png result_2.png ...result_10.png
or if only selected files are needed
but better
then the result is result_ 00 .png result_ 01 .png result_ 02 .png ...result_10.png which is much more convenient when working with files

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question