Two things to consider:
- Simple resizing with or without aspect ratio conversion
- Cropping with aspect ration conversion
I address each in turn:
1. Simple resizing with or without aspect ratio conversion
imagemagick
by default will maintain the preexisting aspect ratio of an image during conversions. Using this test image:
which conveniently has the dimensions of 100x100 an attempt to resize to 300x150 with this syntax will silently fail:
convert test.png -resize 300x150 300_test.png
and will produce a 150x150 image:
To force imagemagick
to actually produce your 2:1 aspect ratio image the following slightly different syntax is required:
convert test.png -resize 300x150\! distort.png
And this achieves a 2:1 aspect ratio, with distortion of the original image of course:
2. Cropping with aspect ration conversion
If you would prefer to simply and automatically crop an image to a specified aspect ratio the easiest way is to use one of Fred's ImageMagick Scripts: 'Aspectcrop'. Usage on our test image is simply:
./aspectcrop -a 2:1 test.png Fred_wins.png
and the resulting image has been successfully cropped to a 2:1 ratio:
The other option to be manipulated is -g gravity
which defines which part of the image is used for cropping. The default is center, all options are:
- center (c)
- north (n)
- south (s)
- east (e)
- west (w)
- northwest (nw)
- northeast (ne)
- southwest, (sw)
- southeast (se)
So to give an example of this using north:
./aspectcrop -a 2:1 -g n test.png north.png
The results are:
If you needed to use this script for a batch load of images in a single directory you could use a bash 'for' loop. First place the script correctly and set it to executable:
sudo mv aspectcrop /usr/local/bin
sudo chmod +x /usr/local/bin/aspectcrop
This makes it a lot easier to use the script. Then run the 'for' loop from within a directory of images:
for i in *.png
do
aspectcrop -a 2:1 -g n "$i" "${i%.png}_cropped.png"
done
This loop can be varied according to the type of input and output files, different locations etc. You could also produce a similar syntax that will search recursively for images. Lots of possibilities...
I think that is all pretty cool :)
References:
pdfimages -all
returns the exact file that was stored in the pdf.
We can test this by doing a round-trip: starting with a jpg image, we add it to a pdf using LaTeX, extract it using pdfimages -all
, and then compare it to the original. (The reason for using LaTeX will be explained later.)
I have the first jpg image as extracted from your link and I named it device.jpg
. Let's put it in a PDF file using LaTeX:
$ cat img.tex
\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=5in,keepaspectratio]{device}
\end{document}
$ pdflatex img
[...snip...]
Output written on img.pdf (1 page, 672455 bytes).
Transcript written on img.log.
Now, let's extract it using pdfimages -all
and compare it with the original:
$ pdfimages -all img.pdf img-all
$ cmp device.jpg img-all-000.jpg
$
The extracted jpg is byte-for-byte identical to the original.
Footnote: the reason for using LaTeX
The above test cannot be done using just any PDF creator. This is because not all PDF creators will put images into a PDF unmolested. For example, let's try ImageMagick's convert
:
$ convert device.jpg device.pdf
$ pdfimages -all device.pdf device-all
$ cmp device.jpg device-all-000.jpg
device.jpg device-all-000.jpg differ: byte 4, line 1
convert
re-sampled the image to a smaller size before placing it in the pdf.
$ ls -1s device.jpg device-all-000.jpg
528 device-all-000.jpg
656 device.jpg
Image accuracy was part of pdflatex's design goals. Other PDF creation software may, by default, "optimize" images before placing them in the PDF.
Update: ShreevatsaR points out that the img2pdf utility also provides a lossless method to convert images to PDF. Non-TeX users will also likely find it much simpler to use.
Best Answer
The problem is with the
colorspace gray
part of the command. This option checks only difference between gray colour of the images.So the correct command should be
This should give you more appropriate answer.