Page 1 of 1

Need help running imagemagick command over many files

Posted: 11 Jan 2021 21:59
by OM3
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.

Re: Need help running imagemagick command over many files

Posted: 12 Jan 2021 16:19
by rasil
- 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.

Re: Need help running imagemagick command over many files

Posted: 12 Jan 2021 17:17
by ShadowThief
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.

Re: Need help running imagemagick command over many files

Posted: 12 Jan 2021 22:09
by OM3
@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.

Re: Need help running imagemagick command over many files

Posted: 13 Jan 2021 05:03
by elzooilogico
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

Re: Need help running imagemagick command over many files

Posted: 13 Jan 2021 08:11
by OM3
@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.

Re: Need help running imagemagick command over many files

Posted: 13 Jan 2021 09:29
by elzooilogico
did you try

Code: Select all

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

Re: Need help running imagemagick command over many files

Posted: 13 Jan 2021 09:51
by OM3
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

Re: Need help running imagemagick command over many files

Posted: 13 Jan 2021 11:24
by elzooilogico
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%"
...
...

Re: Need help running imagemagick command over many files

Posted: 13 Jan 2021 17:59
by OM3

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