imagemagick - run command on all files in folder and output results to their own sub-folders - command-line

I am on windows using this command to split a sprite sheet into multiples files (frames) -
magick convert images\1.png -crop 1024x1024 images\1.png
Which works fine, problem is that I need to do this to 5k+ sprite sheets and create a sub-folder for each set of frames. I tried searching for an answer and found out about mogrify but I'm unsure how to use it for my situation?
EDIT:
Input images are all named sequentially 1 to 5000. Output folder will be name respectively and output files will also be named in sequence 1-30.

Ok so I figured out how to do on a small batch of files. Haven't tested on the full set yet.
I ended up creating a .bat file and running a loop on my commands. With the use of the counter I was able to get the names to be numeric and sequentially.
#echo off
set count=10
:loop
mkdir %count%
mkdir %count%\sequence-%count%
magick %count%.png -crop 1024x1024 %count%/sequence-%count%/%count%.png
set /a count=count-1
if %count%==0 goto exitloop
goto loop
:exitloop
pause

Related

How to concatenate video files from different sub directories using ffmpeg in win10 command prompt?

How can I merge 1200 small mp4 files scattered in 60 subfolder, into one big file?
My IP Camera records and stores videos in 3 second video files.
An hour long recording is stored into 60 subfolders (one for each minute). And in each of those subfolder are 20 mp4 files. (one mp4 is 3 seconds)
I found a way of doing it by having to copy each and every one of those small mp4 files into one main folder. Then use command prompt:
(for %i in (*.mp4) do #echo file '%i') > mylist.txt
to make a list file
then run:
ffmpeg -f concat -i mylist.txt -c:v copy output2.mp4
And this gets the job done. But is there any graceful way of doing it without having to copy paste all of the video files?
With...
DIR *.mp4 /B /S
...you should get a list of all mp4-files in the current- and sub-directories.
It defaults to alphabetic filename sorting. Please consult DIR /? for other sorting options.
For your ffmpeg concat-list that would be:
(FOR /F "delims=" %A IN ('DIR *.mp4 /B /S') DO #ECHO file '%A') > mylist.txt
("delims=" to preserve spaces in filenames)

Command line will not run MAXScript

I have a command line code as follows -
for /r %%v in (*.max) do start %%v
It opens any Max file in the same folder - great.
I want it to also tell max to run any number of scripts when the file has opened, there are guides on how to do this on the 3dsMax help for eg:
-U MAXScript = (this will open MAXScript and run a certain script on the end of a fresh 3dsmax command load.
However this does not work on the end of the initial code I need to use.
I have been researching how this could work for 2 days but keep going in circles.
Please help.
Adam
try for /r %%v in (*.max) do START cmd.exe /C %%v
You might want to take a look at using 3dsmaxbatch.exe instead of 3dsmax.exe
Here is a link to the 2019 documentation:
https://knowledge.autodesk.com/support/3ds-max/learn-explore/caas/CloudHelp/cloudhelp/2019/ENU/3DSMax-Batch/files/GUID-48A78515-C24B-4E46-AC5F-884FBCF40D59-htm.html
The command line to load a max file then execute a script should look like this
3dsmaxbatch.exe -sceneFile C:/some/path/to/maxfile.max C:/some/path/to/script.ms

Strip image metadata and rename it using a text file as source of input

I am ok with PowerShell or cmd-based suggestions. This is what I am trying to do:
I have this folder structure:
C:\Parent\Child01\ - Lots of JPG images
C:\Parent\Child02\ - Lots of JPG images
C:\Parent\Child03\ - Lots of JPG images
so on and so forth.
What I am doing currently manually:
I will be inside the Child 01 folder and run this command to strip meta data from all images
exif -r -all= -ext jpg -ext gif -ext png
I have a txt file with keywords one per line. I want the images inside my current directory (Child01) to be replace with the names I gave in the text file.
How I am doing this right now is by using an excel sheet with 3 columns :
Original File Name | New File Name | Rename Command
Original File Name has the content of all files names (I get this by running dir /b /a-d)
New File Name has my keywords
The Rename Command is a formula =concatenate("ren ",A2,".jpg ",B2, ".jpg")
This helps me generate the formula which I copy paste from command line to bulk rename.
Can some help me with a batch file or powershell script so I can get it all done in one go, please?
No need to bother Excel. It renames each .JPG to the next name of the file (it should just contain one column ("NewFileName" (without extension)). If it runs out of either files or lines, it stops.
#echo off
setlocal enabledelayedexpansion
<file.txt (
for /f "delims=:" %%a in ('dir /b *.jpg') do (
set "x="
set /p x=
if not defined x goto :eof
ECHO ren "%%a" "!x!%%~xa"
)
)
Check the output before you remove the ECHO command to actually enable the ren command.

How to reduce the file size on JPEG images in batch (/Mac)?

I have a list of .JPG files on a Mac. I want to export them to a format taking less than 500 kilobytes per image.
I know how to do that using the Preview application one image at a time; but I want to be able to do the same in batch, meaning on several files at once. Is there a command line way to do it so I could write a script and run it in the terminal?
Or some other way that I could use?
This is an example from the command line using convert (brew info imagemagick) converting all *.jpg images in one directory to .png:
$ for i in *.jpg; do
convert "$i" "${i%.jpg}.png"
done
To test before (dry-run) you could use echo instead of the <command>:
$ for i in *.jpg; do
echo "$i" "${i%.jpg}.png"
done
This will search for files within the directory having the extension .jpg then execute the command convert passing as arguments the file name $i and then using as an output the same file name removing the extension and adding the new one .png, this is done using:
"${i%.jpg}.png"
The use of double quotes " is for the case file could contain spaces, check this for more details: shell parameter expansion
For example, to just change the quality of the file you could use:
convert "$i" -quality 80% "${i%.jpg}-new.jpg"
Or if no need to keep the original:
mogrify -quality 80% *.jpg
The main difference is that ‘convert‘ tends to be for working on individual images, whereas ‘mogrify‘ is for batch processing multiple files.
Install ImageMagick. (Really.. it's lightweight and amazing) It's nice to install using Homebrew. Then...
Open terminal.
cd [FilepathWithImages] && mogrify -define jpeg:extent=60kb -resize 400 *.JPG
Wait until the process is complete (may take a few minutes if you have many images)
To check file sizes, try du -sh * to see the size of each file in the directory you're in.
NOTE: *.JPG must be uppercase for it to work
How this works:
cd [yourfilepath] will naviage to the directory you want to be in
&& is used for chaining commands
mogrify is used when you want to keep the same filename
-define jpeg:extent=60kb sets the maximum filesize to 60kb
-resize 400 will set the width
*.JPG is for all files in the directory you're in.
There are many additional commands you can use with imagemagick convert and mogrify. After installing it, you can use man mogrify to see the commands you can chain to it.
According to the docs, "Restrict the maximum JPEG file size, for example -define jpeg:extent=400KB. The JPEG encoder will search for the highest compression quality level that results in an output file that does not exceed the value. The -quality option also will be respected starting with version 6.9.2-5. Between 6.9.1-0 and 6.9.2-4, add -quality 100 in order for the jpeg:extent to work properly. Prior to 6.9.1-0, the -quality setting was ignored."
Install ImageMagick from Homebrew or MacPorts or from https://imagemagick.org/script/download.php#macosx. Then use mogrify to process all files in a folder using -define jpeg:extent=500KB saving to JPG.
I have two files in folder test1 on my desktop. Processing will put them into folder test2 on my desktop
Before Processing:
mandril.tif 3.22428MB (3.2 MB)
zelda.png 726153B (726 KB)
cd
cd desktop/test1
mogrify -path ../test2 -format jpg -define jpeg:extent=500KB *
After Processing:
mandril.jpg 358570B (359 KB)
zelda.jpg 461810B (462 KB)
See https://imagemagick.org/Usage/basics/#mogrify
The * at the end means to process all files in the folder. If you want to restrict to only jpg then change it to *.jpg. The -format means you intend the output to be jpg.
DISCLAIMER: BE CAREFUL BECAUSE THE FOLLOWING SOLUTION IS A "DESTRUCTIVE" COMMAND, FILES ARE REPLACED WITH LOWER QUALITY DIRECTLY
Now that you have read my disclaimer, I would recommend to get cwebp that you can download here.
You will also need parallel sudo apt-get install -y parallel and then I coined the following script:
parallel cwebp {} -resize 0 640 -m 6 -sns 100 -q 80 -preset photo -segments 4 -f 100 -o {} ::: *.jpg && /
find -name "*.jpg" | parallel 'f="{}" ; mv -- {} ${f:0:-3}webp'
640 is the resulting file height in pixels and 0 before means that the width will adapt to the ratio between width and height.
I reduced quality to 80% (-q 80), you will not notice much difference.
The second line find all the files that have been converted but still have the wrong extension file (.jpg), so it removes the last 3 characters (jpg) and add webp instead.
I went from 5 Mb to about 50k per file (.jpg images were 4000x4000 pixels) , just saved 20 Gb of storage. I hope you enjoy it !
If you don't want to bother with webp format you can use the following instead (you will need to install imageMagick perhaps):
parallel convert {} -resize x640 -sampling-factor 4:2:0 -strip -quality 85 \
-interlace JPEG -colorspace RGB -define jpeg:dct-method=float {} ::: *.jpg

count number of times a string appears in a text file using script

I need to write a .bat or .cmd script that will find all instances of file type .log in the directory it is run from, and for each of those search it for "searchstring", counting how many times it appears. Then I need to rename the file (original name: "[name].log") to "name.log". This is to enable me to get a very quick visual count of the number of errors in a file (which is part of what the log contains).
I've already got the for loop that locates all *.log files, but how do I count instances of a particular string?
try this:
for /f "tokens=2delims=:" %a in ('find /c "string" *.log') do #set /a count+=%a
echo %count%
Code is for shell prompt. For shell file replace %a with %%a.