Problems converting PDF to BMP using ImageMagick? - imagemagick

I would like to convert a PDF to BMP. The PDF I am talking about is available here.
I use ImageMagick with the following command :
convert -verbose -resize 256x256 phantom-brain-ellipse.pdf phantom.bmp
But the result is a phantom.bmp file corrupted or empty file with pixel dimensions 0x0.
The error message is :
"/opt/local/bin/gsx" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72" "-sOutputFile=/var/tmp/magick-1199-UNts2yPB9u2%d" "-f/var/tmp/magick-1199rG26g03biUuT" "-f/var/tmp/magick-1199-sNgqMmlD53M"
/var/tmp/magick-1199-UNts2yPB9u21 PNG 263x263 263x263+0+0 8-bit sRGB 54.6KB 0.010u 0:00.009
phantom-brain-ellipse.pdf PDF 263x263 263x263+0+0 16-bit sRGB 54.6KB 0.000u 0:00.000
phantom-brain-ellipse.pdf=>phantom.bmp PDF 263x263=>256x256 256x256+0+0 16-bit sRGB 262KB 0.020u 0:00.019
When I convert to PNG everything works fine with the same command :
convert -verbose -resize 256x256 phantom-brain-ellipse.pdf phantom.png
Any idea how to fix that ?
Partial Solution
This command allowed me to get the right BMP :
convert phantom.png BMP3:phantom.bmp
But I don't get why it works !

Try:
convert -verbose -resize 256x256 phantom-brain-ellipse.pdf[0] BMP3:phantom.bmp
Update/Explanation
PDF is a multipage format. Potentially, the input PDF has more than one page.
ImageMagick commands can select a specific page with the naming convention filename[i], where i is the index of the page. Indexing is zero-based, so some.pdf[0] means: "Use page 1 of this PDF."
The SOMEFORMAT:filename convention determines the file format for output files and overrides any file format which may be indicated by the filename suffix. So an output filename of GIF:some.png will create a GIF file with the (wrong) suffix .png.
The BMP3: is a specific version of the general BMP format. By default, BMP:filename or filename.bmp will produce version 4 of BMP. To get version 3 use BMP3:filename.bmp. To get version 2 use BMP2:filename.bmp.
I used BMP3: because you reported that it worked...

Related

ImageMagick Convert BMP from 24 bit to 16 bit question?

Trying to convert bmp file from 24-bit to 16-bit bmp by ImageMagick and ViX 統合画像ビュアー. This is the same as the thumbnail in my images, but in hex content is not my expected. And the question I ask because I have to covert these 16-bit bmp files to a .spr (Sprite image file) for MMORPG Game(Lineage 1).
ImageMagickVersion: ImageMagick-7.1.0-portable-Q16-HDRI-x64
ViX 統合画像ビュアー Version: ViX 2.21
Plateform: Win11
Source fils here
Input_24bit.bmp
Output_16bit_ViX.bmp
Output_16bit_IM.bmp
ImageMagick Command
convert '24_bit.bmp' -alpha off -define bmp:subtype=RGB565 '16_bit.bmp'
ViX 統合画像ビュアー
Use GUI windows to decreasing color to Highcolor(16bit)
Result presenting (Red background will been transparent in Game)
BMP Hex Compare
(A) result from ViX 統合画像ビュアー that I expected
(B) result from Imagemagick convert
The difference is that ImageMagick, by default, writes a v5 BMP, whereas your program has written a v3 BMP.
To make ImageMagick write the older BMP3 format, prefix your output filename with BMP3: like this:
convert input.bmp -alpha off -define bmp:subtype=RGB565 BMP3:output.bmp

Embedding a hex value in a .jpg or .png file that doesn't impact display?

Is there a location inside the .jpg or .png where I can put an arbitrary 32-bit value method for embedding a 32-bit value inside that will not be visible in the image (but would change the hash of the image file.)
Updated Answer
You can do that by adding a comment. If you want a technique that works with both JPEG and PNG and doesn't recompress your image data, I would suggest exiftool.
You can add a comment like this to JPEG or PNG:
exiftool -Comment=32768 a.jpg
exiftool -Comment=32768 a.png
You can retrieve the comment along with all other information using:
exiftool a.jpg
Or, you can get just the comment and its value with:
exiftool -Comment a.jpg
Comment : 32768
Or, if parsing in a shell script, you can get the unadorned values like this:
exiftool -Comment -s -s -s a.jpg
32768
Original Answer
You can put a number or comment in a file with jhead like this:
jhead -cl "32768" a.jpg
Modified: a.jpg
Then read it back, with jhead:
jhead a.jpg
File name : a.jpg
File size : 49732 bytes
...
...
Comment : 32768 <--- HERE IT IS

Contrast feature of ImageMagick convert

I just want to use the convert command in a way, so that the pictures get in colorspace gray, negated and with a higher contrast.
The colorspace and negation are not a problem, but the contrast is one. I use the following bash script:
for i in *
do
echo $i
convert $i -colorspace gray gray/$i
convert gray/$1 -contrast -contrast contrasted/$1
convert contrasted/$i -negate negated/$i
done
I moved the contrast command in every possible position, but there always comes the following error:
convert-im6.q16: no decode delegate for this image format `' # error/constitute.c/ReadImage/504.
convert-im6.q16: no images defined `contrasted/' # error/convert.c/ConvertImageCommand/3258.
convert-im6.q16: unable to open image `contrasted/s5_rechts_abgeschnitten.png': Datei oder Verzeichnis nicht gefunden # error/blob.c/OpenBlob/2701.
convert-im6.q16: no images defined `negated/s5_rechts_abgeschnitten.png' # error/convert.c/ConvertImageCommand/3258.
I don't really know what to do, so would be cool if someone got a solution.

mogrify -resize and append new filesize to filename

I would like to mogrify -resize 500 my files and append the new filesize to the filename ie: image-500*500.png
Can someone help me out please.
I don't think you can do that with mogrify because it overwrites the original filename rather than creating a new one. So, I think you will need to do it with convert.
So, for one file called input.png:
convert input.png -resize 400x500\! -set filename:f "%[t]-%[w]x%[h]" "%[filename:f].png"
and your output file will be input-400x500.png
If you wanted to do a whole directory full of PNG files on a Mac, you would make a backup and, then on a spare copy do:
for f in *.png; do
convert "$f" -resize 400x500\! -set filename:f "%[t]-%[w]x%[h]" "%[filename:f].png"
done

ImageMagick pdf to black and white pdf

I would like to convert a pdf file to a Black and White PDF file with ImageMagick. But I've got two problems:
I use this command:
convert -colorspace Gray D:\in.pdf D:\out.pdf
But this command convert only the FIRST page... How to convert all pages?
After use this command the resolution is terrible... but if I use -density 300 option the file size has increased more than double. So I would like to use the same DPI setting, but how to use?
Thanks a lot
Assuming you have all the necessary command line tools installed you can do the following:
Split and join PDF using pdfseparate and pdfunite (Poppler tools).
Extract the original density using pdfinfo plus grep/egrep and, for instance, sed. This will not guarantee the same size of the PDF file, just the same DPI.
Putting it all together you can have a series of bash commands as following:
pdfseparate in.pdf temp-%d.pdf; for i in $(seq $(ls -1 temp-*.pdf | wc -l)); do mv temp-$i.pdf temp-$(printf %03d $i).pdf; done
for f in temp-*.pdf; do convert -density $(pdfinfo $f | egrep -o 'Page size:[[:space:]]*[0-9]+(\.[0-9]+)?[[:space:]]*x[[:space:]]*[0-9]+(\.[0-9]+)?' | sed -e 's/^Page size:\s*//'| sed -e 's/\s*x\s*/x/') -colorspace Gray {,bw-}$f; done
pdfunite bw-temp-*.pdf out.pdf
rm {bw-,}temp-*.pdf
Note 1: there as a dirty workaround (for/wc/seq/printf) for a proper ordering of 10-999 pages PDFs (I did not figure out how to put leading zeros in pdfseparate).
Note 2: I guess ImageMagick treats PDFs as just another binary image file so for instance for mainly text files this will result in huge PDFs. Thus, this is a very bad method to convert text-based PDFs to B&W.

Resources