Convert multiple images with ImageMagick - imagemagick

I want to convert all my PNGs to PNG using ImageMagick (I need this because AndroidStudio has some issue with the original PNGs. ImageMagick is able to fix this issue by re-exporting the PNG.)
If I do: convert a.png a.png it works.
But how do I do this for many files (including files from sub directories)?

For Windows users -- you can try this in a cmd.exe window:
for %i in (*.png *\*.png *\*\*.png *\*\*\*.png) do (
convert.exe %i %~pni---repaired.png
)
This will loop through the current dir's PNGs as well as the ones in the sub directories 3 levels deep.
You have to make sure that your convert.exe really is the one from ImageMagick -- set up your environment variable %PATH% accordingly. Otherwise you may run into an error when the command wants to use the identically named disk format conversion convert.exe command.
If unsure, use the full path to the IM convert.exe, e.g.:
D:\programs\imagemagick-install-dir\convert.exe %i %~pni---repaired.png
Also, remember: If you put the above command into a *.bat file, you have to double up each occurrence of %. So %i from the direct command becomes %%i in the batch file!
If you are on Vista/Windows7/2008/8 (or on Windows XP with the Resource Kit installed) you'll have the ForFiles.exe available, which can be used to loop through files:
forfiles.exe ^
/p <path> ^
/m *.png ^
/s ^
/C "convert.exe #file #fname---repaired.png"

Try this command. Start it from the top-most directory from where you want to convert all images:
find . -name "*.png" \
| while read image; do \
convert "${image}" "${image/.png/---repaired.png}
done
Caveats: Should you have PNG files with the suffix .PNG or .pNg or similar, the command will not work for these. For such cases, the command needs some modifications...

Related

ImageMagick converts out of order

I have 100 images named img0.jpg to img99.jpg to be converted to a pdf file. problem is
convert img*.jpg out.pdf
adds pages in the order of 1,11,2,22,etc. how is order defined in imagemagick?
Either number your pages with zero-padded numbers like this so ImageMagick takes them in order:
img000.jpg
img001.jpg
img002.jpg
...
img098.jpg
Then your original command should work.
Or, have bash enumerate the files in order and feed the names into ImageMagick like this:
magick img{0..99}.jpg result.pdf
Or:
for file in img{0..99}.jpg; do echo $file; done | magick #- result.pdf
Or rename your files as per the first example above, but using Perl rename:
rename --dry-run 's/\D//g; $_=sprintf("f-%05d.jpg",$_)' f*jpg
Sample Output
'f0.jpg' would be renamed to 'f-00000.jpg'
'f1.jpg' would be renamed to 'f-00001.jpg'
'f10.jpg' would be renamed to 'f-00010.jpg'
'f11.jpg' would be renamed to 'f-00011.jpg'
'f12.jpg' would be renamed to 'f-00012.jpg'
You may have ls -v available to you, in which case you can try:
magick $(ls -v img*jpg) result.pdf

Trim command /imagemagick in command line to remove white space from iamges in /uploads folder

I can't work out the syntax.
Client has loaded 8000 images, all with huge whitespace around them.
how do i run a imagemagick trim command from the command line, that will trim all images in the /uploads folder and also from all files in subdirectories
Is the whitespace always the same size? If yes determine it and adapt the crop options. Moreover you will probably need to filter on file name.
find . -name *.jpg -type f -print0 | xargs -0 -I{} convert {} -crop 40x30+10+10 {}
Use -trim argument with the ImageMagick's mogrify utility.
mogrify -trim /uploads/*
Repeat for sub-directories, or use find utility.

.CDR to .SVG Convert Using ImageMagick

I am on CentOS 6.4 and trying to convert .CDR to .SVG Convert Using ImageMagick using SSH command.
my 1.cdr file is in /var/www/vhosts/website.com/httpdocs/test/1.cdr
once converted to SVG it should be created in the same folder
Tried the following command:
convert /var/www/vhosts/website.com/httpdocs/test/1.cdr image.svg
The Error I am getting is:
sh: mplayer: command not found convert: Delegate failed "mplayer"
"%i" -really-quiet -ao null -vo png:z=3' #
delegate.c/InvokeDelegate/1032. convert: missing an image filename
image.svg' # convert.c/ConvertImageCommand/2800.
Not sure what does that mean ?
In order to convert CDR files you need to install uniconvertor for CDR delegate.
List of all delegates:
convert -list delegate
By default it outputs:
cdr => "uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o"
Install uniconvertor. For example, on Ubuntu it’s:
sudo apt-get install python-uniconvertor
Then run:
convert image.cdr -flatten -thumbnail '512x512' image.png
Or, with zoom cropping:
convert image.cdr -flatten -thumbnail '512x512^' -gravity center -crop 512x512+0+0 +repage image.png
And you’re done.
I convert to PNG here but you may use your own output format.
python-uniconvertor is part of inkscape.
It does not exist by itself.
Ubuntu/Mint recently removed all the old Python stuff, for Corel Draw I have to fire up the WinXP VM & Corel and export something Linux understands, usually PNG, a favourite
CDR & WMF files are pretty much dead to Linux, ImageMagick can still handle WMF though.

Can't install GhostScript in ImageMagick

Download latest version of ImageMagick. Unpacked it. Installing Ghostscript like this:
$ sudo apt-get install ghostscript
After that try to configure ImageMagick:
$ ./configure --with-gslib
$ make
$ make install
After that i try to conver PDF to jpg
$ sudo /usr/local/bin/convert in.pdf out.jpg
And i see this mistake
convert: no decode delegate for this image format `/tmp/magick-BzHdr4Kp-00000001' # error/constitute.c/ReadImage/544.
convert: Postscript delegate failed `in.PDF': Нет такого файла или каталога # error/pdf.c/ReadPDFImage/678.
convert: no images defined `out.jpg' # error/convert.c/ConvertImageCommand/3044.
What i'm doing wrong?
Try the following convert commands to see more precisely what's possibly going wrong:
convert a.pdf -debug coder a.jpg
convert a.pdf -debug all a.jpg
There will possibly be a lot of output going to stderr. Amongst the lines you may see where IM is looking for Ghostscript. Also, try
convert -list delegate
convert -list delegate | grep --color -E '(eps|pdf)'
to find with which exact commandlines ImageMagick tries to run Ghostscript (it may call gsx instead of gs, or it may look for it in /usr/local/bin/...). If you find any deviations from your real Ghostscript installation, you can possibly fix it by editing delegates.xml.
convert -list configure
will show you how ImageMagick is configured (and if, for example, gs was during compile-time in the list in DELEGATES variables). Here you also find where to look for delegates.xml:
convert -list configure | grep CONFIGURE_PATH
should list the directory where this (as well as some more) *.xml settings files are located which control how convert et al. behave...

How to find the path to ImageMagick config files

I'm attempting to load a ICM color profile file that is stored with the main ImageMagick config files (colors.xml, etc.) using RMagick. I want to make sure that I always provide the correct path to the file across deployments using slightly different ImageMagick setups and/or versions. Is there a way to find to get this location from ImageMagick (something like Magick-config)?
This is what I have now, but it seems brittle:
MAGICK_PREFIX = `Magick-config --prefix`.strip!
MAGICK_VERSION = `Magick-config --version`.strip!.split(/\s/).first
RGB_COLOR_PROFILE = "#{MAGICK_PREFIX}/share/ImageMagick-#{MAGICK_VERSION}/config/sRGB.icm"
I have no perfect anwser, but you can
$ convert -list color | grep Path: | awk '{print $2}'
/usr/lib/ImageMagick-6.3.7/config/colors.xml
If response is [built-in] try another -list value.
try these and parse stdout / stderr. the first one should work always
convert -debug configure rose: info:
convert -list configure

Resources