imageMagick convert from a folder to another command in Linux? - 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.

Related

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

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'`

change images size of all images - linux terminal

Say I wanted to change the size of a picture and keep the name the same, in terminal I would type: convert picture1.jpg -resize 1280x720! picture1.jpg Now what if I had a folder with hundred of these pictures and I wanted to change them all to 1280x720 and keep the same name. Is there any easier way then typing out that line for each picture?
cd myfolder
for file in *.jpg
do
convert "$file" -resize 1280x720! "$file"
done
In case there is tons of images you might run into trouble using an for f in *.jpg approach. In this case you might want to do something like:
find myfolder -maxdepth 1 -name *.jpg -exec convert \{\} -resize 1280x720! \{\} \;
That's exactly what mogrify is for:
mogrify -resize 1280x720! *.jpg

How to resize an image by saving the resized version under a new name?

Thanks for being a part of this amazing community.
Here I've been fighting with my script and looks like I need a hint.
I have the following file structure:
Mother_Directory/
./child1_Directory
file1.jpg
file2.JPG
file3.jpg
./child2_Directory
file1.jpg
file2.JPG
file3.jpg
./child3_Directory
file1.jpg
file2.JPG
file3.jpg
Now I want to use imagemagick's convert function to do all sorts of things (say resize) on these files in the child directories and save them in Mother_Directory_2/ (which is on an external hard drive) under a new name.
e.g. external_HD_path/Mother_Directory_2/child2_Directory/resized_file1.jpg.
Here is my code:
for f in `find . -name "*.*"`; do convert $f -resize 50% ../Mother_Directory_2/$f; done
this one was supposed to save the new files in ../Mother_Directory_2/,
but no luck (even without name-change logic). I tend to believe that what I'm trying to achieve is something trivial but I've no idea where to look for a hint. Is there a simple way to do it?
I believe the issue is with the way you are using $f in the destination directory. The $f usually will contain the full path of the file that is being converted. Instead you can try using "basename" in the script.
for f in `find . -name "*.*"`; do base=$(basename "$f") ; convert $f -resize 50% ../Mother_Directory_2/$base; done
Given that many people search for this solution here i would like to post the easiest solution i've managed to write based on all the comments and posts i've went through.
step 1. create all the folders in the new Mother_Directory_2 (get the names of the folders from the original Mother_Directory)
for d in */ ; do mkdir ~/Desktop/Mother_Directory_2/$d; done
step 2.
2.1.get the child directory names by using
parentname="$(basename "$(dirname "$f")")"
2.2. get the filenames by using
base=$(basename "$f")
3.3 Run the script below (cd Mother_Directory)
for f in `find . -name "*.*"`; do base=$(basename "$f") ; parentname="$(basename "$(dirname "$f")")"; convert -resize 1000x1000 ./$parentname/$base ../Mother_Directory_2/$parentname/resize_$base; done
hope this would be helpful - if you are a Bash specialist please suggest a more efficient way of doing the same. thanks again.

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

Resources