If you take a look at the wikipedia pages for PPM and PAM it says that the magic numbers for PPM are as follows:
excerpt from PPM wikipedia page
Each file starts with a two-byte magic number (in ASCII) that explains
the type of file it is (PBM, PGM, and PPM) and its encoding (ASCII or
binary). The magic number is a capital P followed by a single digit
number.
Magic Number Type Encoding
P1 Portable bitmap ASCII
P2 Portable graymap ASCII
P3 Portable pixmap ASCII
P4 Portable bitmap Binary
P5 Portable graymap Binary
P6 Portable pixmap Binary
Whereas with the PAM format it's magic number is P7. Also it was the following differences from the older formats (PPM, PNM, PGM, PBM):
excerpt from PAM wikipedia page
The header for the PAM file format begins with P7, and (unlike in the
other formats) ends in an explicit close: ENDHDR.
There is no plain (human-readable, ASCII-based) version of PAM. PAM
files are always binary, and attempts to use the switch -plain with
Netpbm programs that produce PAM output results in an error message.
For the black-and-white version of PAM (depth 1, tuple type
BLACKANDWHITE), corresponding to PBM, PAM uses one byte per pixel,
instead of PBM’s use of one bit per pixel (packing eight pixels in one
byte). Also, the value 1 in such a PAM image stands for white (“light
on”), as opposed to black in PBM (“ink on”).
Example
If I convert a PNG file to a PPM file using mogrify
I get the following:
convert blah.png to blah.ppm:
mogrify -format ppm blah.png
file info:
$ ll |grep bla
-rw-rw-r-- 1 saml saml 11870 May 29 21:36 blah.png
-rw-rw-r-- 1 saml saml 530613 May 29 21:36 blah.ppm
identify info:
$ identify blah.png blah.ppm
blah.png PNG 926x191 926x191+0+0 8-bit DirectClass 11.9KB 0.000u 0:00.000
blah.ppm[1] PNM 926x191 926x191+0+0 8-bit DirectClass 531KB 0.000u 0:00.000
hex header info:
$ xxd blah.ppm|head -3
0000000: 5036 0a39 3236 2031 3931 0a32 3535 0af2 P6.926 191.255..
0000010: f1f0 0000 0000 0000 0000 0000 0000 0000 ................
0000020: 0000 0000 0000 0000 0000 0000 0000 0000 ................
$ xxd blah.png |head -3
0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
0000010: 0000 039e 0000 00bf 0802 0000 0019 f594 ................
0000020: be00 0000 0373 4249 5408 0808 dbe1 4fe0 .....sBIT.....O.
As you can see, mogrify
correctly generated a PPM file (see the P6).
So what's wrong?
I'm wondering if there is something special about your source images that you're converting to PPM, which the PPM format doesn't support, and mogrify is not able to handle that automatically.
I would suggest interrogating the source image using the identify
command:
identify <original image>
Edit #1
The OP posted the original image here. Running this image through myself I couldn't reproduce his result where mogrify
would return a PAM file rather than a PPM file.
generated ppm:
mogrify -format ppm some.png
original png & new ppm files:
ll|grep some
-rw-rw-r-- 1 saml saml 387940 May 30 00:36 some.png
-rw-rw-r-- 1 saml saml 921615 May 30 07:00 some.ppm
$ identify some.p*
some.png PNG 640x480 640x480+0+0 8-bit DirectClass 388KB 0.000u 0:00.000
some.ppm[1] PNM 640x480 640x480+0+0 8-bit DirectClass 922KB 0.000u 0:00.009
header info from files:
$ xxd some.png |head -3
0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
0000010: 0000 0280 0000 01e0 0802 0000 00ba b34b ...............K
0000020: b300 0020 0049 4441 5478 0184 c181 b21c ... .IDATx......
$ xxd some.ppm |head -3
0000000: 5036 0a36 3430 2034 3830 0a32 3535 0a65 P6.640 480.255.e
0000010: 6e6b 656e 6b62 6e6a 626e 6a5f 706a 5f70 nkenkbnjbnj_pj_p
0000020: 6a5e 726b 5e72 6b5a 6d66 596c 6559 6a64 j^rk^rkZmfYleYjd
The file converted successfully for me. Running display some.ppm
displayed the file so I'm not sure what to make of this. I did notice that the OP's identify command showed the files as sRGB whlie on my system these files show up as "8-bit
DirectClass". The differences between these 2 is detailed here, but I'm not sure what to make of it.
While researching this I came across several threads where there were bugs in ImageMagick related to sRBG and PNG. Here's a link to one example.
This involves some trial & error and in the end, it's debatable which result you might consider to be the "best result". So allow me to just give some generic advice:
use the -flatten
option to get rid of transparent background. The transparency makes it hard to judge actual quality of the result. If you need the transparency in the final image, you can remove -flatten
once you're sure of the quality.
use something like -density 300
to get a high DPI result. The main issue with convert
is that it uses a very low density by default (72 DPI). This parameter has to be specified before the input file.
downscaling a high DPI image might cause additional blur, so perhaps calculating the correct DPI value to achieve the desired resolution is the way to go:
$ convert -density 100 file.pdf -flatten file100.png
$ file file100.png
file100.png: PNG image data, 827 x 1169, 8-bit colormap, non-interlaced
$ echo $((1080*10000/1169))
9238
$ convert -density 92.38 file.pdf -flatten file9238.png
$ file file9238.png
file9238.png: PNG image data, 764 x 1080, 8-bit colormap, non-interlaced
I'm not sure if there is a way to have convert
determine "ideal" DPI value by itself.
If you take this question to the ImageMagick IRC channel or forum, I'm sure you'd get some more advice. It helps if you provide the link to the PDF file you're working with. ;)
You can also improve quality in other ways, for example by trimming empty borders away. You're losing a lot of resolution if half of the page is white. There are even solutions that re-wrap PDF text to get the most out of available screenspace (e.g. k2pdfopt
).
Finally, also try other programs. This is a matter of opinion, but I prefer using Inkscape or GhostScript directly. ImageMagick has characters "glued together", Inkscape has a more balanced result, and GhostScript allows you to render a blur-free pure pixel image (if that's something you like - use pngalpha
for the blurry version, which is virtually identical to convert
).
ImageMagick:
Inkscape:
GhostScript:
gs -r92.38 -sDEVICE=png48 -sOutputFile=ghostscript.png file.pdf
Best Answer
You want all your photos to be 6x4 with a width of 1024, right? That means they should be 683 pixels high.
If that is correct, what you're looking for is ...
convert <input_image> -resize 1024x683^ -gravity center -extent 1024x683 <output_image>
... where you would replace with the filename of the image you want to resize, and with the new filename.
This will crop the edges to fit the aspect and resize them to 1024x683. It will make small images larger, as well as making large images smaller to fit your size.
When you want to resize all images in a directory, you'll need to write a quick bash script, which would look like ...
You then just make the script executable. (Say you saved the script as rename.sh, you run
chmod u+x rename.sh
.) Then you run the script./rename.sh
which will resize all the images in the same directory.