Contrast feature of ImageMagick convert - imagemagick

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.

Related

ImageMagick - Can %d be used together with image property percent escapes?

I'm encountering behavior using ImageMagick's image property percent escapes that I can't explain. Consider this minimal example that exhibits the problem:
convert rose: 'rose_%d.png'
# Resulting filename: rose_0.png (as expected)
convert rose: -set filename:mysize "%wx%h" 'rose_%[filename:mysize]_%d.png'
# Resulting filename: rose_70x46_%d0.png
# Why is '%d' in the output filename?
# Expected: rose_70x46_0.png
convert rose: -set filename:mysize "%wx%h" 'rose_%d_%[filename:mysize].png'
# Resulting filename: rose70x46.png
# Expected: rose_0_70x46.png
Relevant documentation:
https://www.imagemagick.org/Usage/files/#save_escapes
Can %d be used together with image property percent escapes?
Can %d be used together with image property percent escapes?
Only if it comes before %[filename:. Looking at the source code; it appears to be a case statement. The order of precedence allow for field width & %d, but once %[ is trapped, it stops checking on closing brace ] -- or error.
Might be worth jumping over to the developer forums to discuss this behavior.
You can work around it by moving the index order over to the filename:mysize placeholder.
$ convert rose: -set filename:mysize "%wx%h_%p" rose_%[filename:mysize].png
#=> rose_70x46_0.png
$ convert rose: wizard: -set filename:mysize "%wx%h_%p" rose_%[filename:mysize].png
#=> rose_70x46_0.png rose_480x640_1.png

Can ImageMagick be prevented from overwriting an existing image?

When converting an image, ImageMagick's default behavior seems to be to overwrite any existing file. Is it possible to prevent this? I'm looking for something similar to Wget's --no-clobber download option. I've gone through ImageMagick's list of command-line options, and the closest option I could find was -update, but this can only detect if an input file is changed.
Here's an example of what I'd like to accomplish: On the first run, convert input.jpg output.png produces an output.png that does not already exist, and then on the second run, convert input.jpg output.png detects that output.png already exists and does not overwrite it.
Just test if it exists first, assuming bash:
[ ! -f output.png ] && convert input.png output.png
Or slightly less intuitively, but shorter:
[ -f output.png ] || convert input.png output.png
Does something like this solve your problem?
It will write to output.png but if the file already exists a new file will be created with a random 5 character suffix (eg. output-CKYnY.png then output-hSYZC.png, etc.).
convert input.jpg -resize 50% $(if test -f output.png; then echo "output-$(head -c5 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c5).png"; else echo "output.png"; fi)

Problems converting PDF to BMP using 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...

ImageMagick error with rails

I use MiniMagick (A ruby wrapper for ImageMagick) with rails-4.2 (ruby 2.2.0) on a linux server.
I generate images via ttf. Each image has from 1 to 10 layers I then merged to create an image. I get an error which I can not find the solution.
MiniMagick::Error (`convert -background none -fill #000000 -font ttf/SELIS006N.ttf -pointsize 300 label: S public/pictogram_images/873-layer-2.png` failed with error:
convert.im6: invalid geometry `+3.35542e+07+300.5' # error/geometry.c/ParsePageGeometry/1407.
convert.im6: unable to clone cache `/tmp/magick-1r313oY0': No space left on device # error/cache.c/CloneDiskToDiskPixelCache/852.
convert.im6: Image width exceeds user limit in IHDR `public/pictogram_images/873-layer-2.png' # warning/png.c/MagickPNGWarningHandler/1754.
convert.im6: Invalid IHDR data `public/pictogram_images/873-layer-2.png' # error/png.c/MagickPNGErrorHandler/1728.
):
SELIS006N.ttf is my ttf files, the character is "S" and the image path is public/pictogram_images/873-layer-2.png
I test the character with the same .ttf character and it's a completely normal without particuliarity.
Part of the error you received is this line:
unable to clone cache `/tmp/magick-1r313oY0': No space left on device
It seems that the filesystem containing your /tmp directory is just full.
If this doesn't seem to actually be the case and the image you try to convert has a reasonable file size, this might also be an artefact of a corrupt source file. ImageMagick might try to expand compressed parts of the file until it runs out of space and fails.

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