Batch processing images.
I have 15,000 PNG images in 6000 folders and subfolders.
What would be the best way to batch process them to scale them all down 50% and save them as a jpg? |
if you have imagemagick, you can use the convert command. see man convert.
You can also fire up your favourite language and do image editing yourself using Imaging libraries. Code:
#!/usr/bin/env python |
http://www.imagemagick.org/script/index.php
this is a collection of command line image processing utils so you will have to write a script to do what you want and most likely let the computer work on it over night don't forget to make a backup REMEMBER TO MAKE BACKUP COPES !!!!!! |
Code:
find /path/to/top/level -iname "*.png" -exec bash -c 'convert "{}" -resize 50% `dirname "{}"`/`basename "{}" .png`.jpg' \; |
Quote:
I tried it, but it doesn't seem to work. It just sits there. I tried just the find portion and it does find all the PNG images, but it doesn't actually process them. |
Quote:
Code:
find /path/to/top/level -iname "*.png" -exec bash -c 'convert "{}" -verbose -resize 50% `dirname "{}"`/`basename "{}" .png`.jpg' \; |
ImageMagick is installed. I have been able to convert images within each folder, one by one, without a problem.
I let it run all night, there were no jpg images created. Using just the find command, it displays all the images in their subdirectories just fine. Something with the "-exec bash -c" is not working properly on my system. |
Quote:
Code:
find /your/path -iname "*.png" | while read fname ; do convert "$fname" -verbose -resize 50% "`echo "$fname" | sed 's/\.png$/\.jpg/i'`" ; done |
Ok, can you explain to me why that works?
|
Yep, sorry about that, it was late and obviously I neglected the spaces. This would have worked:
Code:
find /path/to/top/level -iname "*.png" -exec bash -c 'convert "{}" -verbose -resize 50% "$(dirname "{}")/$(basename "{}" .png).jpg"' \; qweasd's solution should work as well, and just spawns a subshell instead of using find's -exec option (and since my example spawned a subshell with `bash -c` anyway there isn't really an advantage to sticking 100% with find). `| while read variable` stores the entire contents of the line into $variable instead of breaking the line up into spaces. As long as you quote "$variable" then it will pass the entire line instead of just the first element. My solution relies on find directly launching a process for conversion, while qweasd's solution relies on getting the output from find and passing it through a loop. `| while read variable` was my favourite syntax in bash scripting for a while; unfortunately any variables set within the loop are unset outside of the loop because a new subshell is spawned, so I've tried to script around that syntax as of late. In this example it makes no difference, of course, so it is a viable solution. [edit] I've also noticed that basename is case-sensitive and I don't believe there's a way around it, so using sed is probably a better solution to avoid .png.jpg names. [/edit] |
Quote:
Code:
find /your/path -iname "*.png" | while read -r fname |
Quote:
Code:
find /your/path -iname "*.png" | while read fname ; do "${fname%.png}.jpg" by ghostdog74 is an even bashier way to do the substitution (good to know : ), although if I was worried about the case I could write "${fname%.[Pp][Nn][Gg]}.jpg" |
Thank you very much! 10 hours later, all the files had been resized and convert to JPG at 70% quality.
I was also able to remove all the PNGs(they are backed up on DVDs) You guys are awesome! |
All times are GMT -5. The time now is 09:54 AM. |