How can I run this bat file? - batch-file

This code works properly if I put it directly on cmd:
magick img.jpg -alpha Set -virtual-pixel transparent -distort Perspective '0,0,0,0 0,1900,0,1900 900,0,900,200 900,900,900,900' img_x.jpg
However, if I put this code in a bat file, it doesn't work.
#echo off
Setlocal EnableDelayedExpansion
magick img.jpg -alpha Set -virtual-pixel transparent -distort Perspective '0,0,0,0 0,1900,0,1900 900,0,900,200 900,900,900,900' img_x.jpg
It gives this error:
magick: invalid list of numbers '-distort'
How can I do this. Thanks...

The argument for the -distort option needs to be enclosed in quotes.
#echo off
Setlocal EnableDelayedExpansion
magick img.jpg -alpha Set -virtual-pixel transparent -distort Perspective "0,0,0,0 0,1900,0,1900 900,0,900,200 900,900,900,900" img_x.jpg

Related

Get an imagemagick script (with brackets for a mask) to run as a script for every file in a folder?

I have the script:
convert a.jpg ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50% -morphology open square:4 +mask c.jpg
which happily takes my image, makes a mask, and does what I need it to do, on a per image basis, using an original filename for the input, and a new filename for the output.
However, I'm trying to get this to run on every image in a folder, and I'm having zero luck...
I have tried many .bat files, such as:
#echo on
setlocal enabledelayedexpansion
set img_folder=C:\me\pics\
set output_folder=C:\me\pics\cropped
for /f "delims=" %%i in ('dir /b "%img_folder%\*.jpg"') do (
set input_file=%img_folder%\%%i
set output_file=%output_folder%\%%i
convert %input_file% ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50% -morphology open square:4 +mask %output_file%
)
pause
However, something about the brackets seems to be messing with everything else, as the bracket after +delete is pairing up in sublimetext with the bracket after "do" in the for loop.
I'm really stumped, I've tried everything I can think of, and could really use some help, if anyone can offer a simple solution, I'd be very much appreciative!
Instead of having a batch file looping on convert calls, you can have convert loop on the files. This requires a special syntax to create the output file name from the input file.
(untested, I don't run Windows)
convert -verbose C:\me\pics\*.jpg -set filename:out "c:\me\pics\cropped\%%[basename]" {your_filter_goes_here} "%%[filename:out].jpg"
What about this simpler version, which makes sure your command is not nested within parentheses?
#SetLocal EnableExtensions DisableDelayedExpansion
#Set "img_folder=C:\me\pics"
#Set "output_folder=C:\me\pics\cropped"
#For /F "EOL=? Delims=" %%G In ('Dir "%img_folder%\*.jpg" /A:-D /B 2^>NUL'
) Do #convert "%img_folder%\%%G" ( -clone 0 -fill white -colorize 100 -fill black -draw "polygon 500,300 500,1500 1300,1500 1300,300" -alpha off -write mpr:mask +delete ) -mask mpr:mask +repage -threshold 50%% -morphology open square:4 +mask "%output_folder%\%%G"
#Pause

Exiftool via batch file: redirecting output to original console

I'm trying to get the dimensions width/height from a webp image, using exiftool via a batch file and saving those values as variables. I can get the bat file to output the dimensions (in this example it outputs "400x222" in the exiftool window), however I'm not too sure how to redirect those values sucessfully back inside the bat file console window as variables.
Below code gives me what I want, but in the exiftool window:
#echo off
setlocal EnableDelayedExpansion
echo =============== WEBP EXIF ===============
echo.
for %%f in ("C:\ffmpegConvert\*.webp") do (
exiftool -k -s -s -s -ImageSize %%f
)
endlocal
pause
exit
Below is what I tried, but I'm not able to echo back those values in my initial bat file console window (I get "echo is off, once for each failed echo")
#echo off
setlocal EnableDelayedExpansion
echo =============== WEBP EXIF ===============
echo.
for %%f in ("C:\ffmpegConvert\*.webp") do (
for /f "tokens=1,2 delims=x" %%a in ('exiftool -s -s -s -ImageSize %%f') do (
set sW=%%a
set sH=%%b
)
#echo !sW! >CON:
#echo !sH! >CON:
)
endlocal
pause
exit
Ok, so after checking out all your feedback and doing some extra googling, I noticed that my exiftool.exe in c:\Windows had "run as admin" checked in its properties. Unticking this option now returns the variables in my original console...To be sure, I re-checked it, and it went back to not displaying the info properly. I don't understand why that is, but now it works as I wanted:) Thanks again for your responses!

How to move accented files using `Batch`?

If I move an accented file to the same directory using the command prompt, it works correctly.
move ó.mp3 ó.mp3
However, when I use the same command in a .bat file, the following error appears: the system cannot find the file specified.
#echo off
move ó.mp3 ó.mp3
pause
A flagrant mojibake case. I guess that you save the script using Windows' native notepad editor using co-called ANSI encoding. The following commented code snippet could enlighten the problem:
#ECHO OFF
SETLOCAL EnableExtensions
REM save current code page (optional)
for /F "tokens=1* delims=:" %%G in ('CHCP') do set "_currentCP=%%H"
REM find notepad's ("ANSI") code page
for /F "tokens=1,2*" %%G in ('
REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Nls\CodePage" -v ACP^|find "ACP"
') do set "_notepadCP=%%I"
REM show flagrant mojibake case
echo %_currentCP%: move ó.mp3 óó.mp3
REM change the active console code page and show right result
>NUL CHCP %_notepadCP%
echo %_notepadCP%: move ó.mp3 óó.mp3
REM reurn the active console code page to saved value (optional)
>NUL CHCP %_currentCP%
pause
Result:
D:\bat\SO\61743331.bat
852: move ˇ.mp3 ˇˇ.mp3
1250: move ó.mp3 óó.mp3
I see above output on my (Central European) Windows. On Western Windows, you could see something like
850: move ¾.mp3 ¾¾.mp3
1252: move ó.mp3 óó.mp3
You should change the code page with this command CHCP /?
#echo off
CHCP 65001>nul
move ó.mp3 ó.mp3
pause

batch - Reading lines/var from config file without for command

Can you read lines from a config/txt file without the for /f or any for?
Anyways, I wanted to have a config file what determines the color of the prompt.
I have the following batch code:
#echo OFF
color %color%
#echo
cmd
and this .config file:
---------type color here----------
a
Expected output:
color a
And,
set /p color<main.config
Only reads the first line of the file.
It's so easy with a for:
for /f "eol=-" %%a in (1.config) do color %%a
assuming your config file is like:
---------type color here----------
a
I'd recommend to change your config file a bit (so more than one value can be stored):
---------type color here----------
foreground=e
background=1
and read it with:
for /f "eol=- delims=" %%a in (1.config) do set "%%a"
color %background%%foreground%
Learn more about for /f. It's the most powerful command in batch and worth every minute you spend learning it.
assuming you have your files 1.bat (your batch) and 1.config (configfile)
you can do this if you really dont want to use the for command.
#echo OFF
findstr /V "#" 1.config >1.tmpcfg
set /p COL= < 1.tmpcfg
del 1.tmpcfg
color %COL%
cmd
But I'd prefer using 'for'...
:-)

Converting PDF to TIFF with ImageMagick shows UnableToOpenBlob

I am trying to convert a PDF to a TIFF file for OCR with Tesseract. As of now, because I am running on Windows, I am trying to write the conversion as a batch file. This is what I have so far:
#echo off
:start
SETLOCAL EnableDelayedExpansion
SET IMCONV="C:\Program Files\ImageMagick\convert.exe"
set /p INPUTF=Input file(enter done if done)?
if %INPUTF%==done goto end
set /p OUTPUTF=Output file?
%IMCONV% -density 300 %INPUTF% -depth 8 %OUTPUTF%
goto start
:end
When I run this I get convert.exe:UnableToOpenBlob and that there is no file in directory #error/blob.c/OpenBlob/2695. Any idea what's wrong?
After taking the advice from Mark Setchell the above problem has been fixed however now I receive the error convert.exe: UnableToOpenConfigureFile 'delegates.xml' #warning/configure.c/GetConfigureOptions/706

Resources