Need help running imagemagick command over many files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 31
Joined: 17 Mar 2014 08:43

Need help running imagemagick command over many files

#1 Post by OM3 » 11 Jan 2021 21:59

I want to run this code on many tiles:

convert input.jpg -resize 300 output.jpg

I had some old code that I thought worked:

Code: Select all

:loop
convert "%~1" -resize 300 "%~1.jpg"

shift
if not "%~1"=="" goto :loop
pause
The code doesn't work 😕

Not sure what I'm doing wrong.
It just goes on a loop forever. I'm sure the code worked when I used it previously for a similar imagemagick command.

Also:

- I will run the code on jpg and png files.
When running on PNG, I need to give jpg as the output file name, like: convert input.png -resize 300 output.jpg
Else, I will get PNG output. How do I do this in the bat file?

- I want the output to be changed from 'original filename.jpg' to 'original filename optimised.jpg'
Not sure where to start with this - guessing it's related to the above?

- I want to output into a folder called 'Output'
But... want to first check if it exist. If exist, delete contents (no prompt needed). If it doesn't exist, then create and output.
Is this getting too complex for a bat file?

Any help would be appreciated.

Thanks.

rasil
Posts: 31
Joined: 23 Apr 2020 13:05

Re: Need help running imagemagick command over many files

#2 Post by rasil » 12 Jan 2021 16:19

- I will run the code on jpg and png files.
When running on PNG, I need to give jpg as the output file name, like: convert input.png -resize 300 output.jpg
Else, I will get PNG output. How do I do this in the bat file?
You can run a normal PNG to PNG conversion after that is done you can simply just change its file type

Code: Select all

ren *.png *.jpg
Also you can replace * with a variable or a filename if you want to change a specific file.
- I want to output into a folder called 'Output'
But... want to first check if it exist. If exist, delete contents (no prompt needed). If it doesn't exist, then create and output.
Is this getting too complex for a bat file?
If I understood properly this can be done fairly easily

Code: Select all

@echo off

::Checking if the folder "Output" exist else 
::Deleting all of its contents
if not exist Output md Output&goto resize
@RD /S /Q "Output"&md Output

:: Your resizeing code goes below
:resize
cd Output
you can place your resizing code on the last line but make sure to do a cd .. if you want to revert the directory to where your batch file is located.

ShadowThief
Expert
Posts: 1160
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Need help running imagemagick command over many files

#3 Post by ShadowThief » 12 Jan 2021 17:17

The fact that it's "looping forever" suggests to me that you've called your script convert.bat, which would mean that when you run the line convert "%~1" -resize 300 "%~1.jpg", you're telling the script to run itself instead of running imagemagick's convert command. Either give the full path to convert.exe or call your script something else.
- I will run the code on jpg and png files.
When running on PNG, I need to give jpg as the output file name, like: convert input.png -resize 300 output.jpg
Else, I will get PNG output. How do I do this in the bat file?
- I want the output to be changed from 'original filename.jpg' to 'original filename optimised.jpg'
Not sure where to start with this - guessing it's related to the above?
I'm not sure what the issue is since either way you're ending the output filename with .jpg, but you can check the file extension of %1 with the ~x modifier (while you're modifying the file name, you can add the word "optimised"):

Code: Select all

@echo off
:loop
if /I "%~x1"==".png" (
    REM %~n1 refers to the filename without the extension
    set "output_name=%~n1 optimised.jpg"
) else (
   REM If you wanted files that don't end in .png to have a different extension, you'd put that here
   set "output_name=%~n1 optimised.jpg"
)
convert "%~1" -resize 300 "%output_name%"

shift
if not "%~1"=="" goto :loop
pause
- I want to output into a folder called 'Output'
But... want to first check if it exist. If exist, delete contents (no prompt needed). If it doesn't exist, then create and output.

Code: Select all

@echo off
if exist .\Output\ (
    del /s .\Output
) else (
    mkdir Output
)

:loop
if /I "%~x1"==".png" (
    REM %~n1 refers to the filename without the extension
    set "output_name=%~n1 optimised.jpg"
) else (
   REM If you wanted files that don't end in .png to have a different extension, you'd put that here
   set "output_name=%~n1 optimised.jpg"
)
convert "%~1" -resize 300 "Output\%output_name%"

shift
if not "%~1"=="" goto :loop
pause
rasil wrote:
12 Jan 2021 16:19
You can run a normal PNG to PNG conversion after that is done you can simply just change its file type

Code: Select all

ren *.png *.jpg
Also you can replace * with a variable or a filename if you want to change a specific file.
This is wrong, and you'll just end up with a misnamed .png file instead of an actual .jpg file.

OM3
Posts: 31
Joined: 17 Mar 2014 08:43

Re: Need help running imagemagick command over many files

#4 Post by OM3 » 12 Jan 2021 22:09

@ShadowThief
You are simply amazing! :)
Thank you.

I added /Q so the deleting won't prompt me.

Naming the file convert caused a problem? OMG. I think I would have been stuck here for weeks trying to debug that one. :oops:

Question...
@echo off is great...
Don't really need to see the output on screen...
In this case, it's simple... doesn't really matter...
But is it possible to have errors reported at least? But NOT the general output?

Let me know.
Thanks.

@rasil, thanks for trying to help as well - much appreciated.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Need help running imagemagick command over many files

#5 Post by elzooilogico » 13 Jan 2021 05:03

you may run your script

Code: Select all

1>nul convert input.jpg -resize 300 output.jpg
this will suppres any normal output while errors should be displayed

OM3
Posts: 31
Joined: 17 Mar 2014 08:43

Re: Need help running imagemagick command over many files

#6 Post by OM3 » 13 Jan 2021 08:11

@elzooilogico
Tried that
Didn't work
Still seeing the output
If I can't find an answer, I'll either live with the output or just use echo off. But a shame if there isn't a solution.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Need help running imagemagick command over many files

#7 Post by elzooilogico » 13 Jan 2021 09:29

did you try

Code: Select all

1>nul scriptname input.jpg -resize 300 output.jpg
where scriptname is you actual script?

OM3
Posts: 31
Joined: 17 Mar 2014 08:43

Re: Need help running imagemagick command over many files

#8 Post by OM3 » 13 Jan 2021 09:51

Oh... no, I didn't do that

I did as you suggested:

Code: Select all

1>nul convert input.jpg -resize 300 output.jpg
I'm not sure how I could call the bat file
The bat file receives arguements
I'm not sure how I can pass those arguements in?
Can you suggest anything?

I put the bat file in the SendTo folder.
I select images and right click and choose the file from the SendTo folder.

Hoping that all makes sense?
Let me know

Thanks

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Need help running imagemagick command over many files

#9 Post by elzooilogico » 13 Jan 2021 11:24

well, if you run from explorer, then place

Code: Select all

1>nul
in front of

Code: Select all

convert "%~1" -resize 300 "%output_name%"
inside your script, so

Code: Select all

@echo off
...
...
1>nul convert "%~1" -resize 300 "%output_name%"
...
...

OM3
Posts: 31
Joined: 17 Mar 2014 08:43

Re: Need help running imagemagick command over many files

#10 Post by OM3 » 13 Jan 2021 17:59

Code: Select all

1>nul convert "%~1" -resize 300 "%output_name%"
Yes, that's what I did in the first place after you suggested
No joy 😕
I'm sure it's only a small tweak away from working
Let me know if you can suggest anything else
Thanks

Post Reply