ลดขนาดรูป

Topic: 
 

ลดขนาดรูปพร้อมกับหมุนภาพอัตโนมัติ ทำลึกลงไปทุกไดเรคทอรี่

$ sudo apt-get install imagemagick jhead
$ sudo touch /usr/local/bin/d.canon5.py
$ sudo chmod 0755 /usr/local/bin/d.canon5.py
$ sudo vi /usr/local/bin/d.canon5.py

#!/usr/bin/env python

import sys, os, string

# VAR
subdir="canon3"
file_skel=[".JPG",".jpg",".jpeg"]
dir_skel=["DCIM","CANON","100EOS5D"]

# PROCEDURE
def process_file(filename):
  for fskel in file_skel:
    if fskel in filename:
      if not os.path.exists(subdir):
        os.mkdir(subdir)
      #
      print "convert %s -> %s" % (filename, os.path.join(subdir,filename))
      os.system('convert -resize 40%'+" %s %s" % (filename,os.path.join(subdir,filename)))
      os.system("jhead -autorot %s" % (os.path.join(subdir,filename)))
    #
  #


def process_dir(dirname):
  ## print "dirname=%s" % dirname
  for dskel in dir_skel:
    if dskel in dirname:
      print "enter %s" % dirname
      os.chdir(dirname)
      filelist=os.listdir(".")
      for i in filelist:
        if os.path.isdir(i):
          process_dir(i)
        else:
          process_file(i)
        #
      #
      print "exit %s" % dirname
      os.chdir("..")
    #
  #

# MAIN PROG
def main():
  if len(sys.argv) < 2:
    # PROCESS ALL FILE&DIR
    for filelist in os.listdir("."):
      if os.path.isdir(filelist):
        process_dir(filelist)
      else:
        process_file(filelist)
      #
    #
  else:
    filename = os.path.abspath(sys.argv[1])
    if os.path.isdir(filename):
      process_dir(filename)
    else:
      process_file(filename)
    #
  #
  print "finished"


if __name__=="__main__":
        main()