LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices



Reply
 
Search this Thread
Old 09-11-2010, 10:06 PM   #1
JamesGT
Member
 
Registered: Oct 2009
Location: St. Louis, MO, USA
Distribution: Slackware64 14.0
Posts: 195

Rep: Reputation: 40
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?
 
Old 09-11-2010, 10:16 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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
import Image
import sys
import os
root="/home"
for r,d,f in os.walk( os.path.join(root,"path","to","images") ):
    for files in f:
        if files[-3:].lower() == "png":
            filename=os.path.join(r,files)
            img = Image.open(filename)
            width,height=img.size
            img.thumbnail( (width/2,height/2), Image.ANTIALIAS)
            img.save(filename[:-3]+"jpg","JPEG")

Last edited by ghostdog74; 09-11-2010 at 10:59 PM.
 
Old 09-11-2010, 10:22 PM   #3
rob.rice
Member
 
Registered: Apr 2004
Distribution: slack what ever
Posts: 796

Rep: Reputation: 119Reputation: 119
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 !!!!!!
 
Old 09-11-2010, 11:05 PM   #4
T3slider
Senior Member
 
Registered: Jul 2007
Distribution: Slackware64-14.1
Posts: 2,298

Rep: Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722
Code:
find /path/to/top/level -iname "*.png" -exec bash -c 'convert "{}" -resize 50% `dirname "{}"`/`basename "{}" .png`.jpg' \;
That should do the trick. Had to use `bash -c` since otherwise {} will not be replaced in subshells. This will not remove the original .png files.
 
Old 09-11-2010, 11:58 PM   #5
JamesGT
Member
 
Registered: Oct 2009
Location: St. Louis, MO, USA
Distribution: Slackware64 14.0
Posts: 195

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by T3slider View Post
Code:
find /path/to/top/level -iname "*.png" -exec bash -c 'convert "{}" -resize 50% `dirname "{}"`/`basename "{}" .png`.jpg' \;
That should do the trick. Had to use `bash -c` since otherwise {} will not be replaced in subshells. This will not remove the original .png files.
Thanks Tslider

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.
 
Old 09-12-2010, 02:59 AM   #6
T3slider
Senior Member
 
Registered: Jul 2007
Distribution: Slackware64-14.1
Posts: 2,298

Rep: Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722
Quote:
Originally Posted by JamesGT View Post
Thanks Tslider

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.
You should make sure you have ImageMagick installed (`which convert` should return something). It will appear as though it is doing nothing -- that command doesn't echo anything to the display. With so many pictures it will take quite a long time. This might be better for tracking progress:
Code:
find /path/to/top/level -iname "*.png" -exec bash -c 'convert "{}" -verbose -resize 50% `dirname "{}"`/`basename "{}" .png`.jpg' \;
That will display information about the conversion. Make sure to adjust /path/to/top/level, of course.
 
Old 09-12-2010, 10:28 AM   #7
JamesGT
Member
 
Registered: Oct 2009
Location: St. Louis, MO, USA
Distribution: Slackware64 14.0
Posts: 195

Original Poster
Rep: Reputation: 40
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.
 
Old 09-12-2010, 01:42 PM   #8
qweasd
Member
 
Registered: May 2010
Posts: 484

Rep: Reputation: Disabled
Quote:
Originally Posted by JamesGT View Post
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.
Hi! The script you were using may fail if some paths contain spaces or other fancy characters. Try this:

Code:
find /your/path -iname "*.png" | while read fname ; do convert "$fname" -verbose -resize 50% "`echo "$fname" | sed 's/\.png$/\.jpg/i'`" ; done
 
Old 09-12-2010, 04:04 PM   #9
JamesGT
Member
 
Registered: Oct 2009
Location: St. Louis, MO, USA
Distribution: Slackware64 14.0
Posts: 195

Original Poster
Rep: Reputation: 40
Ok, can you explain to me why that works?
 
Old 09-12-2010, 06:23 PM   #10
T3slider
Senior Member
 
Registered: Jul 2007
Distribution: Slackware64-14.1
Posts: 2,298

Rep: Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722Reputation: 722
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"' \;
since it quotes the filename being passed to convert (before the filename was being quoted when passed to dirname and basename but the final filename was unquoted and thus did not work).

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]

Last edited by T3slider; 09-12-2010 at 06:26 PM.
 
Old 09-12-2010, 07:43 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by qweasd View Post
Hi! The script you were using may fail if some paths contain spaces or other fancy characters. Try this:

Code:
find /your/path -iname "*.png" | while read fname ; do convert "$fname" -verbose -resize 50% "`echo "$fname" | sed 's/\.png$/\.jpg/i'`" ; done
Code:
find /your/path -iname "*.png" | while read -r fname 
do 
   convert "$fname" -verbose -resize 50% "${fname%.png}.jpg" 
done
 
Old 09-12-2010, 09:59 PM   #12
qweasd
Member
 
Registered: May 2010
Posts: 484

Rep: Reputation: Disabled
Quote:
Originally Posted by JamesGT View Post
Ok, can you explain to me why that works?
Code:
find /your/path -iname "*.png" | while read fname ; do
  convert "$fname" -verbose -resize 50% "`echo "$fname" | sed 's/\.png$/\.jpg/i'`"
done
find finds all files ending in .png, regardless of case, and passes it to bash. while read fname reads each line until the end of the stream into a variable fname. Double quotes around "$fname" prevent it from breaking up into multiple arguments. The sed command is a suffix replacement: s for substitution, i for ignoring case.

"${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"
 
Old 09-13-2010, 08:16 PM   #13
JamesGT
Member
 
Registered: Oct 2009
Location: St. Louis, MO, USA
Distribution: Slackware64 14.0
Posts: 195

Original Poster
Rep: Reputation: 40
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Processing ankan Linux - Software 3 10-11-2013 11:52 AM
A faster batch processing method Rick069 Linux - Software 1 06-26-2007 12:23 AM
script help for batch processing mma8x Linux - General 5 12-08-2006 02:31 AM
Image batch processing cvzyl Linux - Software 3 03-20-2006 03:45 AM
how to do batch processing with time out? Franziss Programming 4 11-30-2005 11:17 PM


All times are GMT -5. The time now is 05:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration