I have a folder with many high-res images (in jpeg, jpg, png). I want to convert every image into images in jpeg with the width of 2500, 1440 and 640, and place them in a folder called output.
I also want to add the width of the image as a suffix to the image filename (i.e. red-ball.jpg -> red-ball-640.jpeg, red-ball-1440.jpeg, red-ball-2500.jpeg).
How can I do this with ImageMagick?
You can do it like this:
#!/bin/bash
# Make output directory
mkdir output
shopt -s nullglob
for f in *.jpg *.jpeg *.png; do
base=${f%.*}
ext=${f##*.}
echo Converting $f to output/$base -2500 -1440 -640 $ext
convert "$f" -resize 2500 -write "output/${base}-2500.${ext}" \
-resize 1440 -write "output/${base}-1440.${ext}" \
-resize 640 "output/${base}-640.${ext}"
done
Sample Output
Converting test.jpg to output/test -2500 -1440 -640 jpg
Converting z.jpg to output/z -2500 -1440 -640 jpg
Converting z2.jpg to output/z2 -2500 -1440 -640 jpg
Converting z3.jpg to output/z3 -2500 -1440 -640 jpg
Converting z4.jpg to output/z4 -2500 -1440 -640 jpg
Converting z1.jpeg to output/z1 -2500 -1440 -640 jpeg
Converting a.png to output/a -2500 -1440 -640 png
Converting black.png to output/black -2500 -1440 -640 png
Converting c.png to output/c -2500 -1440 -640 png
Converting d.png to output/d -2500 -1440 -640 png
Converting f2.png to output/f2 -2500 -1440 -640 png
Converting gantt.1.png to output/gantt.1 -2500 -1440 -640 png
Converting globe.png to output/globe -2500 -1440 -640 png
Converting h.png to output/h -2500 -1440 -640 png
Converting output.png to output/output -2500 -1440 -640 png
Converting result.png to output/result -2500 -1440 -640 png
Converting result2.png to output/result2 -2500 -1440 -640 png
The shopt -s null glob ensures that if there are no jpg files, or png files in your directory, that the glob (*.jpg or *.png) expands to nothing rather than generating an error message. Further info here.
The ext=${f##*.} finds the shortest thing following a period (full stop), which is basically the file extension. This is called "bash parameter substitution" and the best description I know of is here.
Related
Using Ghostscript 9.53 on Mac with Imagemagick 7.0.10-59.
The EPS has a Tiff Preview on it, which gets added as a frame in gif or as an extra image for PNG. It started happening after updating imagemagick and ghostscript.
Ghostscript 9.51 and imagemagick 7.0.10-0 without ghostscript delegate did not have this issue.
Command: magick convert -verbose -density 200 -channel RGBA -colorspace RGB -alpha On my.eps my.gif
the new output is as follows:
/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-3MLz7XbvUU4OJOQBAqLqB-7bteT5s-061 PNG 890x505 890x505+0+0 8-bit sRGB 23054B 0.020u 0:00.014
EPS:/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-tWS3M8-k1os9hbHqWGRzlibuupNgItzv=>/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-tWS3M8-k1os9hbHqWGRzlibuupNgItzv EPS 890x505 890x505+0+0 16-bit sRGB 23054B 0.000u 0:00.000
TIFF:~/Downloads/my.eps=>~/Downloads/my.eps TIFF 321x182 321x182+0+0 8-bit Palette sRGB 256c 148108B 0.000u 0:00.004
~/Downloads/my.eps[0] EPT 890x505 890x505+0+0 16-bit sRGB 23054B 0.010u 0:00.008
~/Downloads/my.eps[0] TIFF 321x182 321x182+0+0 8-bit Palette sRGB 256c 148108B 0.010u 0:00.000
[ghostscript library 9.52] -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r200x200' -g890x505 -dFirstPage=1 -dLastPage=1 -dEPSCrop '-sOutputFile=/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-3MLz7XbvUU4OJOQBAqLqB-7bteT5s-06%d' '-f/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-vyRuqlNsQcaG9cv4dJXLql9ChPsp95-g' '-f/var/folders/zb/ygl385yn3_ggw_bqpdp4940r0000gn/T/magick-nK22ULA5kPuCN9Yq8dKM7E8NTQxI82vH'~/Downloads/my.eps=>~/Downloads/my.gif EPT 890x505 890x505+0+0 16-bit Palette sRGB 35c 18001B 0.960u 0:00.139
convert: Invalid TIFF directory; tags are not sorted in ascending order. `TIFFReadDirectoryCheckOrder' # warning/tiff.c/TIFFWarnings/1044.
Older version ouput was as follows:
'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r200x200' -g890x505 -dEPSCrop '-sOutputFile=/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084UYHZpdfAwolz%d' '-f/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084Zug0r87QHWuH' '-f/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084JWA1MKLfJ5i7'
/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084UYHZpdfAwolz1 PNG 890x505 890x505+0+0 8-bit sRGB 23054B 0.020u 0:00.013
EPS:/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084Rxd2cgpYXsmR=>/var/folders/6d/tw_d66k92ld2dc5wvtzxr4y00000gn/T/magick-40084Rxd2cgpYXsmR EPS 890x505 890x505+0+0 16-bit sRGB 23054B 0.010u 0:00.000
~/Desktop/my.eps EPT 890x505 890x505+0+0 16-bit sRGB 23054B 0.000u 0:00.000
~/Desktop/my.eps=>~/Desktop/my.gif EPT 890x505 890x505+0+0 16-bit PaletteBilevelAlpha sRGB 3c 9061B 0.830u 0:00.121
Is there a way for me to tell imagemagick to ignore tiff preview?
EPS File here
Bad Gif here
** Updated file links
In ImageMagick 7, use magick not magick convert and not convert. Also if you have only one frame/page and want to keep on that, then add [0] to your input image. So try
magick -verbose -density 200 -channel RGBA -colorspace RGB -alpha On my.eps[0] my.gif
I have a folder of images of varying sizes and quality. Is there a way with ImageMagic that I could automatically resize them to be no bigger 1100px x 1100px and less than 160kb. And not to re-size if they are smaller than those parameters. Also not to distort the image so it fits within but to only re-size so for example an image which is 2200px by 1000px would become 1100px by 500px.
I'm working on Ubuntu with ImageMagick 6.7.7-10.
Try this on a copy of your files:
mogrify -define jpeg:extent=160k -resize 1100x1100\> *.jpg
I am assuming your files are JPEG images. The command will largely work for other image types too, but it can only enforce the 160kB limit for JPEG files - not PNG, or TIFF etc.
Example
With version: ImageMagick 6.9.1-10 Q16 x86_64 2015-08-06
convert -size 2000x2000 xc:gray +noise random a.jpg
ls -lrt a.jpg
-rw-r--r-- 1 mark staff 6969601 21 Aug 18:28 a.jpg # <--- 7MB
mogrify -define jpeg:extent=160k -resize 1100x1100\> a.jpg
ls -lrt a.jpg
-rw-r--r-- 1 mark staff 147938 21 Aug 18:28 a.jpg # <--- 160kB
identify a.jpg
a.jpg JPEG 1100x1100 1100x1100+0+0 8-bit sRGB 148KB 0.000u 0:00.000
I'm trying to convert an image with imagemagick, but I'm getting this error:
convert: no decode delegate for this image format
`/tmp/magick-XXITXcTr' # constitute.c/ReadImage/503.
I'm doing this:
convert http://images.icecat.biz/img/norm/high/12003462-2669.jpg -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 /myimagefolder/iphone.png
I have the following delegates:
DELEGATES bzlib fontconfig freetype gs jpeg jng jp2 lcms openexr
png rsvg tiff x11 xml wmf zlib
Anyone knows what the problem is?
This is happening because the image you are trying to download no longer exists and returns a 404. ImageMagick creates a temporary file before it downloads the image. But this file will remain empty because the download fails for this image. ImageMagick will try to read the image but fails because it cannot handle empty files.
When i'm running convert via command line
convert -verbose -density 300 -trim -quality 100 1565_002.pdf test_image.png
the result is good. Verbose showing output:
"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r400x400" "-sOutputFile=/var/tmp/magick-39357qEey9jeURtmJ%d" "-f/var/tmp/magick-39357DTf85TEO2pfX" "-f/var/tmp/magick-39357aIeGxsC4sZlD"
/var/tmp/magick-39357qEey9jeURtmJ1 PNG 3400x4400 3400x4400+0+0 8-bit sRGB 144KB 0.300u 0:00.289
1565_002.pdf PNG 3400x4400 3400x4400+0+0 16-bit sRGB 144KB 0.000u 0:00.000
1565_002.pdf=>test_image.png PNG 3400x4400=>2778x3582 3400x4400+311+233 8-bit sRGB 2c 39.4KB 0.410u 0:00.420
BUT when uploading pdf via rails
has_attached_file :photo,
# ...storage options...
:styles => { :pdf_thumbnail => ["", :png] },
:convert_options => {
:all => '-auto-orient',
:pdf_thumbnail => "-verbose -density 300 -trim -quality 100"
}
The result is VERY poor.
Rails (paperclip) + Verbose output:
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/3d/td4xb1s56wxb1tcrl8rgy6tw0000gn/T/1565_00220150206-39272-wftjey.pdf[0]'
Command :: convert '/var/folders/3d/td4xb1s56wxb1tcrl8rgy6tw0000gn/T/1565_00220150206-39272-wftjey.pdf[0]' -auto-orient -verbose -density 300 -trim -quality 100 -auto-orient '/var/folders/3d/td4xb1s56wxb1tcrl8rgy6tw0000gn/T/1565_00220150206-39272-wftjey20150206-39272-1qf97j3.png'
/var/folders/3d/td4xb1s56wxb1tcrl8rgy6tw0000gn/T/1565_00220150206-39272-wftjey.pdf[0]=>/var/folders/3d/td4xb1s56wxb1tcrl8rgy6tw0000gn/T/1565_00220150206-39272-wftjey20150206-39272-1qf97j3.png PNG 612x792=>500x645 612x792+56+42 8-bit sRGB 2c 4.06KB 0.010u 0:00.020
The problem is here - "PNG 612x792=>500x645 612x792+56+42" instead of "PNG 3400x4400 3400x4400+0+0".
So how this could be fixed? Or what option should i add.
I'm using rails (4.0.2), paperclip (3.5.2), rmagick (2.13.4)
Finally, i tried these solution - https://gist.github.com/bastien/3059321
There additional ghostscript setup which transform pdf with more deeper pixel per inch value (originally was r144 and i've changed it to r400) and other custom options.
I have a file I'm saving in raw GRAY format, which then gets converted to tiff. Running the command as follows works:
convert -size 1024X1024 -depth 16 -endian MSB imgTemp.gray /tmp/bla.tiff
but changing to use stdin as the input doesn't:
cat imgTemp.gray | convert -size 1024x1024 -depth 16 -endian MSB -:gray /tmp/bla.tiff
I get the following error:
convert: no decode delegate for this image format gray' # error/constitute.c/ReadImage/532.
convert: missing an image filename/tmp/bla.tiff' # error/convert.c/ConvertImageCommand/3011.
The question is why?
You just have the STDIN and format flipped. "-:gray" should be "gray:-"
cat imageTemp.gray |
convert -size 1024x1024 \
-depth 14 \
-endian MSB gray:- /tmp/bla.tiff
To answer why this is happening. We can run your previous command with a -verbose switch.
cat imgTemp.gray | \
convert -verbose \
-size 1024x1024 \
-depth 16 \
-endian MSB -:gray /tmp/bla.tiff
This will give use an additional line of information that explains what ImageMagick is trying to do
convert: unable to open image `gray':
No such file or directory # error/blob.c/OpenBlob/2638.
convert: no decode delegate for this image format
`gray' # error/constitute.c/ReadImage/550.
convert: no images defined `bla.tiff' # error/convert.c/ConvertImageCommand/3078.
Convert command becomes confused with -:gray and tries to open a blob file entitled "gray", and eventually attempts to open "bla.tiff" as a source image. Both of them non-existing on the filesystem.