ImageMagick script to resize folder of images - imagemagick

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

Related

Combine multiple command line commands into one single command (pipeline)

How do I combine the following ImageMagick command line commands into a single instruction:
convert -trim "C:\test\test.webp" -quality 95 "C:\test\testMaxNew.webp"
convert "C:\test\testMaxNew.webp" -resize 750x750 "C:\test\testMediumNew.webp"
convert "C:\test\testMediumNew.webp" -thumbnail 100x100^ "C:\test\testSmallNew.webp"
After some trial and error I came up with this:
convert -trim C:\test\test.webp -quality 95 -write mpr:XY +delete \( mpr:XY +write C:\test\testLargeNew.webp +delete \) \( mpr:XY -resize 750x750 +write C:\test\testMediumNew.webp +delete \) \( mpr:XY -resize 100x100^ -gravity center -extent 100x100 +write C:\test\testSmallNew.webp +delete \)
This does the trick but the following errors are reported in the command line prompt:
> convert: unable to open image '\(': No such file or directory #
> error/blob.c/OpenBlob/2695. convert: no decode delegate for this image
> format `' # error/constitute.c/ReadImage/508. convert: unable to open
> image '\)': No such file or directory # error/blob.c/OpenBlob/2695.
> convert: no decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\(':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\)':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: unable to open image '\(':
> No such file or directory # error/blob.c/OpenBlob/2695. convert: no
> decode delegate for this image format `' #
> error/constitute.c/ReadImage/508. convert: no images defined `\)' #
> error/convert.c/ConvertImageCommand/3235.
Can someone explain why I am getting these errors and if my code is correct?
The reason I am trying to combine multiple commands is to minimize processing time.
Version: ImageMagick 7.0.2-0 Q16 x64 on Windows 10
These should do what you want.
Best to read the input right after convert.
I am not sure why you need to resize and thumbnail.
So in Imagemagick try one of the following:
convert "C:\test\test.webp" -trim +repage -quality 95 -resize 750x750 -thumbnail 100x100^ "C:\test\testSmallNew.webp"
or just
convert "C:\test\test.webp" -trim +repage -quality 95 -thumbnail 100x100^ "C:\test\testSmallNew.webp"
Just adding an answer to clarify a couple of things that are not a good fit for comments.
You already have some excellent advice from Fred (#fmw42) as regards reading your input image immediately after convert because that way your command will continue to work when you upgrade to ImageMagick v7 which has already been available a couple of years.
You have added a command that works to your question, but that is a bit clumsy inasmuch as it creates an MPR which you don't need and also creates and destroys images unnecessarily - adding to system load which is undesirable if you have many images to process. I think you can see the following is simpler to understand and maintain, makes fewer copies and deletes and demands on memory, and should achieve the same effect as your command:
convert INPUT.webp -trim +repage -gravity center -quality 95 ^
+write LARGE.webp ^
-resize 350x350 +write MEDIUM.webp ^
-resize 100x100^ -extent 100x100 SMALL.webp

Animation is lost after converting png to webp

I have an animated PNG image but after I convert it to webP - I get the static image without animation.
I've tried cwebp
$ cwebp -q 100 1.png -o 1.webp
$ cwebp -version
1.0.0
and the same with imagemagick
$ magick 1.png -quality 100 -define webp:lossless=true 1.webp
$ magick --version
Version: ImageMagick 7.0.10-29 Q16 x64 2020-09-05 http://www.imagemagick.org
How can I convert to save the animation?
As discovered, cwebp does not support conversion with animation. Possible solution is to extract the individual frames (using ffmpeg or imagemagick for instance) and use img2webp to generate the animation.

imagemagic: how to split an image vertically based on aspect ratio and also with a vertical overlap

I have lot of images and not i want to split them w.r.t aspect ratio.
What is presently working is
convert -crop 2:1 input.png cropped_%d.png
The above command split the image. Assuming i have 900px x 2000px then it creates 5 images.
But now the problem is i want to keep some overlap
I tried
convert -crop 2:1+0+40 input.png cropped_%d.png
Its only giving one image.
I tried
convert -crop 1x4+0+40# Settings_commit1.png cropped_%d.png
This works well. But here i have to mention 1x4 (grid). But I want to do it by aspect ratio
I did the following way
$ convert --version
Version: ImageMagick 7.0.8-35 Q16 x86_64 2019-03-25 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP
Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jbig jng jp2 jpeg lcms lqr ltdl lzma openexr pangocairo png ps raw rsvg tiff webp wmf x xml zlib
The two commands split the image:
Based on aspect ratio split the image file (but overlap does not work 2:1+0+40)
convert -crop 2:1 input.png cropped_%d.png
Based on grid split the image file and overlap works (+0+40)
convert -crop 1x4+0+40# Settings_commit1.png cropped_%d.png (with 40px vertical overlap)
Logic is we get the number of split images from aspect ratio and then use it in grid
This is the scrip which worked
filename="someimage.png";
# just creating a dir and empty it
mkdir ~/croped
rm -rf ~/croped/*
# split the image base on aspect ratio and save them in croped folder
convert -crop 903:600 ${filename} ~/croped/crop_aspect_%d.png;
# count the number of files in the croped folder
number_files=`ls -l ~/croped | grep -E "crop_aspect_*" | wc -l`
# based on this number apply the grid
convert -crop 1x${number_files}+0+8# ${filename} ~/croped/crop_grid_%d.png

ffmpeg resize large image and high resolution

I tried to resize a very big image (457 MB and 21600x21600) with the following command
-i test.png -vf scale=320:-1 out.png
but it throws exception saying "Picture size 21600x21600 is invalid". How can I find out the biggest supported resolution by ffmpeg? Is there a way to resize this high resolution image with ffmpeg?
If you want to use ImageMagick it is included in most Linux distros and is available for macOS and Windows.
Your command becomes:
convert test.png -resize 320x result.png
If you are running v7 or newer, use:
magick test.png -resize 320x result.png
If you have lots to do, and you want all the resized images written in a directory called thumbs you can use:
mkdir thumbs
magick mogrify -path thumbs -resize 320x *.png
Alternatively, you may find vips is a lighter-weight installation and does a faster conversion using less memory:
mkdir thumbs
vipsthumbnail -s 320 -o "thumbs/%s.png" image.png

ImageMagick convert pdf to jpeg has poor text quality after upgrading ImageMagick version to 6.7.8

After upgrading ImageMagick text quality got degraded when convert pdf to jpeg:
Old image
New Image
Conversion command: convert foo.pdf foo.jpeg
Old ImageMagick version:
[root#home]# convert -version
Version: ImageMagick 6.2.8 05/07/12 Q16 file:/usr/share/ImageMagick-6.2.8/doc/index.html
Copyright: Copyright (C) 1999-2006 ImageMagick Studio LLC
generated files size:
-rw-r--r-- 1 root root 139K Apr 2 16:11 foo-0.jpeg
-rw-r--r-- 1 root root 130K Apr 2 16:11 foo-1.jpeg
-rw-r--r-- 1 root root 334K Mar 24 14:27 foo.pdf
After upgrading ImageMagick
[root#home]# convert -version
Version: ImageMagick 6.7.8-10 2012-08-17 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
generated files size:
-rw-r--r-- 1 root root 60K Apr 2 16:11 foo-0.jpeg
-rw-r--r-- 1 root root 55K Apr 2 16:11 foo-1.jpeg
-rw-r--r-- 1 root root 334K Mar 24 14:27 foo.pdf
I've tried using antialias flag:
convert -antialias foo.pdf foo.jpeg
Which did nothing, I've tried setting an higher quality:
convert -quality 100 foo.pdf foo.jpeg
and super sampling:
convert -density 288 -background white -alpha off foo.pdf -resize 25% foo.jpeg
both gave bigger files and better results, but ran more time and had lower quality that the old ImageMagick version.
any advises?
Link to the file
I see the same problem with your sample file. It looks like ImageMagick's delegates for the PDF conversion may have changed with the new install.
If you try convert -verbose foo.pdf foo.jpeg, do you see -sDEVICE=pngalpha in the command that gets sent to gs? The pnmraw device has been used in the past, and switching back to that seems to fix the problem for me.
In ImageMagick's delegates.xml file (which may be in /etc/ImageMagick, but could be somewhere else depending on your setup), look for the decode="ps:alpha" delegate line and change -sDEVICE=pngalpha in the command to -sDEVICE=pnmraw. (You can probably just search for pngalpha in the file.)
it seem that problem at DPI. when convert pdf, imagemagick using Ghostscript. you can skip using imagemagick.
$ gs -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dGridFitTT=2 -dUseCropBox -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r200x200 -sDEVICE=jpeg -dJPEGQ=100 -sOutputFile=foo-%05d.jpg foo.pdf
set -r option higher value. Ghostscript have default value is 100DPI.
or using convert option -density. this option set pdf converted DPI.
$ convert -density 200x200 foo.pdf foo.jpg
PDF files are vector files and have no specific size. Their size is controlled by defining the density and units before reading in the PDF file. You can get better quality for the same desired output file size by supersampling. That means rasterize the PDF to a large size and then resize to your desired actual size. For example in ImageMagick:
convert -units pixelsperinch -density 288 image.pdf -resize 25% output.jpg
The nominal density if left off is 72 dpi. So 72*4=288. Then resize by 1/5=25% gets back to the same default size, but should look much better. Change the density or resize to deal with quality and final size as desired.

Resources