Windows batch file to crop images in folder - windows

I have a lot of images in a folder. I want to crop all the images at specific coordinates and save the converted folder to sub-folder with the name of the file suffixed with crop. I am using ImageMagick command line option convert to crop single file with the help of below given command:
"C:\Program Files\ImageMagick-7.0.8-Q16\magick" convert -crop 60x40+525+240 "C:\temp\rose.png" "C:\temp\temp\rose-crop.png"
I want to do the same process for every file present in that folder and the converted file should follow the same naming convention. I tried this:
for /r %%i in (*) do "C:\Program Files\ImageMagick-7.0.8-Q16\magick" convert -crop 60x40+525+240 %%i %%i
But it is overriding the existing images. How can I achieve the desired result?

You can process a whole folder of images using the magick mogrify command in ImageMagick 7. Create a new empty folder for the output (or backup your input). Cd to the directory with your images, then
magick mogrify -path path_to/new_folder -format png -crop 60x40+525+240 +repage *.png
That will crop every png image in your current directory and place the cropped results in the new_folder directory. See https://imagemagick.org/Usage/basics/#mogrify
Supply your path for my placeholder path_to. Change to using Windows paths with \ rather than my unix syntax of /

Related

ImageMagick mogrify stopped working with a great amount of images

When using
mogrify -format png *.ppm
with a couple of images it works, but when I tried with a great amount of files(Around 20 million) it does not show an error message but after a some minutes the CLI will appear as if it had finished the task but when checking my folder I do not have a single png, I work in windows and have 8 Gigabytes of RAM, so I was wondering:
Does ImageMagick has a limit for the quantity of images?
Or is it just that my computer is not powerful enough for the task?
The files have around 400 Megabytes in total.
Also if there is any other way to get the images into png format even if losing the ppm version please let me know.
Did you check your ImageMagick policy.xml file?
It should be at homeDirectory/.config/ImageMagick/policy.xml
There are limitations that you can set in that file.
Might want to check Imagemagick security policy page
Two ideas...
Firstly, use:
mogrify -verbose -format png *.ppm
so you see what's going on.
Secondly, you could try creating a list of files to process like this:
DIR *.ppm /S /B > filelist.txt
Then tell ImageMagick to process that list of files with:
mogrify -format png #filelist.txt
Another idea... try using double quotes:
mogrify -format png "*.ppm"

Issue with Basic Image Magick Convert and Mogrify Functions

I am attempting to convert all .jpg images in a folder to .png format using Image Magick. This functionality is described as a feature of Image Magick here using the mogrify tool. Here's what I type into the command line, followed by the error:
C:\Users\holde\Desktop\Photos and Videos>magick mogrify -format jpg *.png
mogrify: unable to open image '*.png': Invalid argument # error/blob.c/OpenBlob/3527.
Any help is appreciated! If it's useful, I installed the Windows Binary release from this page, and installed via the ImageMagick-6.9.12-32-Q16-HDRI-x64-dll.exe executable on Windows 10.
Edit: Fixed, I had the syntax backwards. I should have used mogrify -format png *.jpg rather than mogrify -format jpg *.png
With this change, the operation now completes. Thanks for the assistance!
Convert all from png to jpg
mogrify -format jpg *.png

imagemagick and automator DPI change

I am trying to create a workflow in automator to change DPI from 72 to 300, so that I can use it on any image in Finder (when i right click on the image, then I can run the action).
When I run the command in terminal, it works fine:
mogrify -units PixelsPerInch *.jpg -density 300 *.jpg
However, when I use it in shell script, it does not work, and I can't figure out why. This is what I have so far:
for f in "$#"
do
/opt/ImageMagick/bin/mogrify mogrify -units PixelsPerInch *.jpg -density 300 *.jpg
done
Screenshot from Automator
Any help will be highly appreciated.
mogrify is for batch conversion (thus your *.jpgin the working version) but in your example, you have created a loop to step through files. You don't seem to be using your f variable at all.
You should get the command to work on individual files using the convert command instead of mogrify and then plug in$fwherever the filename would occur.

Using separate folders in powershell command

I'm using Powershell and imagemagick to create a simple code to append images. I need to Powershell to be run from one folder, the input images to be take from a sub-folder, and the output images to be taken from a subfolder of that folder.
For example, the folder structure goes:
\image-converter\Append-images\Appended
So Powershell will be run from the "image-converter" folder, and will open with the directory:
PS
C:\Users\name\Downloads\image-converter>
The input images to be appended will be stored in the folder "Append-images", and the output images will be saved to the folder "Appended".
An example of the command I will be using is:
convert +append input.png input.png output.png
So I would need to modify the command to take the input images from \Append-images and save the output images to \Append-images\Appended.
P.S. I can't use the full "C:\..." path as this will be used by different people and the image-convert folder will be saved in different places. So I need the command to build on the "PS C:\...\image-converter>" path that Powershell will be run from.
How about this.
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
Get-Content -path "$scriptPath\Append-images\"
That should give you the location you need to browse the subsequent files.
Use environment variables to get the userprofile, and then your subdirectorys:
$env:userprofile
Will give you your C:\Users\User1
If you want to make the folders, do the following:
mkdir $env:userprofile\images
mkdir $env:userprofile\images\AppendImages
mkdir $env:userprofile\images\Output
And then work with those.

Convert multiple images in Imagemagick (Windows)

I'm trying to convert multiple .tif files into .png files using Imagemagick on the Windows command line. I tried the following, which didn't work:
convert -format tif *.png
I then tried a loop
for %a in (*.tif) do convert %a %a.png
which did work but now all my images are named as [something].tif.png, which is annoying.
So why didn't the first command work, and if there's no way to get the first command to work, is there a way to improve the second command so I won't have to deal with the .tif in the .png image name?
Edit It seems that I got the first command wrong. First of all, convert doesn't work but mogrify does. I had read that mogrify replaced the files of the old format, but apparently it isn't true because it created new images for me without deleting the old ones. Secondly, it seems that the destination file type comes first, so the command is
mogrify png *.tif
which works perfectly.
I'd still like to know how the second command could be improved.
Why don't you use mogrify?
mogrify -format tif *.png
will create 1.tif from 1.png ... N.tif from N.png.

Resources