ImageMagick - How do you convert files recursively in the shell? - linux

I have a folder tree of several subfolders within subfolders of BMP files I'd like to convert to JPG format, but I can't figure out a working command to get mogrify/imagemagick to go through every folder recursively.
Any solutions?

Easiest way to pass list of files would be probably by using backticks and recursive find command:
mogrify -format jpg `find . -name '*.bmp'`

Related

How to list down the full paths to all files in multiple directories of a certain file format?

I am looking for a Bash solution to my dilemma here. I have directory project, and within that I have project/Batch1 project/Batch2 project/Batch3 and so on.
Within each Batch folder, I have a mix of files but what I am interested in are files in the .txt format, for example. I am able to recursively acquire the names of every file in my project directory using the ls -LR command. However, how should I get full paths including the file names, and also only for .txt files?
Thanks!
Use find command. Open man 1 find and check possible option you need for above task.
Here is the simple example. Open terminal and run
find pwd -type f -name "*.txt"
Or
find pwd -name "*.txt"
Or you can do same using bash script also.

Eliminating subfolders to move all files into one folder

I have a folder that contains 32 folders, each with several image files. I would like to move all of these image files into one main folder. I know how to do that manually, folder by folder. Is there an automated command-line way to do that? I have Crunchbang Waldorf, and usually use PCmanFM as a file manager.
/*/ stands for directories.
mv /path/from/*/*.jpg /path/main/
if all these images have one extension, for instance .jpg:
find /directory/You/Want/To/Search -name "*.jpg" -exec cp -t /destination/directory {} +
Note: just make sure that all these images have one unique name otherwise this command would break
UPDATE:
if you don't know what are the images extensions you could just do that one:
find /directory/You/Want/To/Search -regex ".*\.\(jpg\|gif\|png\|jpeg\)" -exec cp -t /destination/directory {} +

convert images to pdfs in subdirectories

I have a bunch of sub-folders with images in them. I am trying to recursively convert them into pdf files using the directory names as the pdf name. With the help of some Google searches I tried using this script I wrote:
#!/bin/bash
for D in `find . -type d` do
convert *.jpg ${PWD##*/}.pdf
end
It did not work. How can I get this to work?
Each folder has several .JPGs in it with a number 01-10 etc. Using convert *.jpg name.pdf converts all the images into one pdf file. I want to make a script to do this and for the PDF files to have the directory name.
I would also like the script to then grab the converted pdf and move it up a level into the parent directory.
A while loop is more appropriate here. Try this:
find . -type d | while read d; do convert "${d}"/*.jpg ./"${d##*/}.pdf"; done
find will return all directories in current directory.
while read d will read each directory path into variable $d.
convert ${d}/*.jpg performs the conversion on all .jpg images in directory $d.
./${d##*/}.pdf replaces the whole directory path with just the directory name, appends .pdf, and ensures that PDF file is created in parent directory.
Thanks to #savanto for the answer. I modified it a bit for my needs.
Read through the files in a directory's subdirectories. Convert one file type to a different file types. Name the file and save it to a directory (using OSX 10.10, terminal bash-3.2).
cd /parent_directory
find . -type d | while read d; do mogrify -format png *.eps; done
find . -name '*.eps' -execdir mogrify -format png {} \;
Here's a version of savanto's answer that uses img2pdf, which is faster, lossless, and makes smaller file sizes than ImageMagick.
`find . -type d | while read d; do img2pdf "${d}"/*.jp2 --output ./"${d##*/}.pdf"; done`
Easy:
find path/to/images -iname "*.jpg" | xargs -I %this convert %this %this.pdf
Very simple and clean!

How to convert jpg files into png files with linux command? + Difficulty = Subfolders

I want to convert several jpg files into png files. As far as I know, one can use this command
mogrify -format png *.*
I have one problem, I have a lot of subfolders. Let's say a is my main folder and b,c and d are subfolders. The images are in the subfolders.
How can I convert all images without having to open every folder manually?
-> I would like to write a command that works, when I am in folder a, but works for all files in the subfolders.
Assuming you're in folder a the following might work for you
find . -name "*.jpg" -exec mogrify -format png {} \;
You can use the find command to get all the jpg files in all the subfolders and pass your command as an argument to find

imageMagick convert from a folder to another command in Linux?

I have the two folders under the same level directory
folder1 and folder 2
now I try to convert all images in folder1 to folder2 and with same file name.
here is what i have now:
for f in folder1/*.jpg
do
convert $f -resize 80%X80% +profile "*" -quality 85 folder2/$f
done
and it throws the following message from each file it tried to convert:
convert: unable to open image `folder1/folder2/st-3474827-1.jpg': No such file or directory # blob.c/OpenBlob/2440.
and I know its directory problem but google for two days already still dont know how to fix it. Can you help me with this?
There's two ways you could deal with it.
Use mogrify:
mogrify -path folder2 -thumbnail 50x50 folder1/*.jpg
Use basename:
for filename in folder1/*.jpg; do
basename="$(basename "$filename" .jpg)"
convert "folder1/$basename.jpg" -thumbnail 50x50 "folder2/$basename.jpg"
done
The former option is probably better, but the latter may be clearer.
I posted a couple of wrong answers. Sorry I just assumed that mogrify or convert would behave well with outputting to a different path. Shouldn't assume. I don't know if Imagemagick can output to a different directory. You may have to just mv the output(no suggestions here) to the new directory. I tried this( I was trying to be as simple as possible without any loops...
convert -resize 500x500 * ../test2/ #output was 2 files named -0 -1 in the new dir
mogrify -resize 200x150 ~/Pictures/rome/test/*.jpg -path ~/Pictures/rome/test2/ # gotta *.jpg in new dir YAY!
mogrify -resize 200x150 -path ~/Pictures/rome/test2/*.jpg ~/Pictures/rome/test/*.jpg # blob error
The manpages don't mention output to a new directory and I searched around a little. The exception codes for ImageMagick are here Good luck and sorry for the faulty gouge. I will edit if I find anything new.

Resources