Advertisement
phillips321

synothumb.py - v3.0a

Sep 3rd, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # sudo mount_nfs -P 192.168.0.2:/volume1/photo /Users/phillips321/nfsmount
  3. # Author:       phillips321
  4. # License:      CC BY-SA 3.0
  5. # Use:          home use only, commercial use by permission only
  6. # Released:     www.phillips321.co.uk
  7. # Dependencies: PIL, libjpeg, libpng, dcraw, ffmpeg
  8. # Supports:     jpg, bmp, png, tif
  9. # Version:      3.0a
  10. # ChangeLog:
  11. #       v3.0a - Video support
  12. #       v2.1 - CR2 raw support
  13. #       v2.0 - multithreaded
  14. #       v1.0 - First release
  15. # ToDo:
  16. #       add movie support:
  17. #       ffmpeg-thumb -i video.MOV -y -ar 44100 -r 12 -ac 2 -f flv -qscale 5 -s 320x180 -aspect 320:180 @eaDir video.MOV/SYNOPHOTO:FILM.flv
  18. import os,sys,Image,Queue,threading,time,subprocess,shlex
  19.  
  20. try:
  21.     from cStringIO import StringIO
  22. except:
  23.     from StringIO import StringIO
  24.  
  25. NumOfThreads=8  # Number of threads
  26. startTime=time.time()
  27. imageExtensions=['.jpg','.png','.jpeg','.tif','.bmp','.cr2'] #possibly add other raw types?
  28. videoExtensions=['.mov','.m4v']
  29. xlName="SYNOPHOTO:THUMB_XL.jpg" ; xlSize=(1280,1280) #XtraLarge
  30. lName="SYNOPHOTO:THUMB_L.jpg" ; lSize=(800,800) #Large
  31. bName="SYNOPHOTO:THUMB_B.jpg" ; bSize=(640,640) #Big
  32. mName="SYNOPHOTO:THUMB_M.jpg" ; mSize=(320,320) #Medium
  33. sName="SYNOPHOTO:THUMB_S.jpg" ; sSize=(160,160) #Small
  34.  
  35. queueIMG = Queue.Queue()
  36. queueVID = Queue.Queue()
  37. try:
  38.     rootdir=sys.argv[1]
  39. except:
  40.     print "Usage: %s directory" % sys.argv[0]
  41.     sys.exit(0)
  42.  
  43. #########################################################################
  44. # Images Section
  45. #########################################################################
  46. class convertImage(threading.Thread):
  47.     def __init__(self,queueIMG):
  48.         threading.Thread.__init__(self)
  49.         self.queueIMG=queueIMG
  50.  
  51.     def run(self):
  52.         while True:
  53.             self.imagePath=self.queueIMG.get()
  54.             self.imageDir,self.imageName = os.path.split(self.imagePath)
  55.             self.thumbDir=os.path.join(self.imageDir,"@eaDir",self.imageName)
  56.             print "\t[-]Now working on %s" % (self.imagePath)
  57.             if os.path.isfile(os.path.join(self.thumbDir,xlName)) != 1:
  58.                 if os.path.isdir(self.thumbDir) != 1:
  59.                     try:os.makedirs(self.thumbDir)
  60.                     except:continue
  61.                
  62.                 #Following if statements converts raw images using dcraw first
  63.                 if os.path.splitext(self.imagePath)[1].lower() == ".cr2":
  64.                     self.dcrawcmd = "dcraw -c -b 8 -q 0 -w -H 5 '%s'" % self.imagePath
  65.                     self.dcraw_proc = subprocess.Popen(shlex.split(self.dcrawcmd), stdout=subprocess.PIPE)
  66.                     self.raw = StringIO(self.dcraw_proc.communicate()[0])
  67.                     self.image=Image.open(self.raw)
  68.                 else:
  69.                     self.image=Image.open(self.imagePath)
  70.    
  71.                 self.image.thumbnail(xlSize)
  72.                 self.image.save(os.path.join(self.thumbDir,xlName))
  73.                 self.image.thumbnail(lSize)
  74.                 self.image.save(os.path.join(self.thumbDir,lName))
  75.                 self.image.thumbnail(bSize)
  76.                 self.image.save(os.path.join(self.thumbDir,bName))
  77.                 self.image.thumbnail(mSize)
  78.                 self.image.save(os.path.join(self.thumbDir,mName))
  79.                 self.image.thumbnail(sSize)
  80.                 self.image.save(os.path.join(self.thumbDir,sName))
  81.             self.queueIMG.task_done()
  82.  
  83. # Finds all images of type in extensions array
  84. imageList=[]
  85. print "[+] Looking for images and populating queue (This might take a while...)"
  86. for path, subFolders, files in os.walk(rootdir):
  87.     for file in files:
  88.         ext=os.path.splitext(file)[1].lower()
  89.         if any(x in ext for x in imageExtensions):#check if extensions matches ext
  90.             if "@eaDir" not in path:
  91.                 if file != ".DS_Store" and file != ".apdisk" and file != "Thumbs.db": # maybe remove
  92.                     imageList.append(os.path.join(path,file))
  93.  
  94. print "[+] We have found %i images in search directory" % len(imageList)
  95. raw_input("\tPress Enter to continue or Ctrl-C to quit")
  96.  
  97. #spawn a pool of threads
  98. for i in range(NumOfThreads): #number of threads
  99.     t=convertImage(queueIMG)
  100.     t.setDaemon(True)
  101.     t.start()
  102.  
  103. # populate queue with Images
  104. for imagePath in imageList:
  105.     queueIMG.put(imagePath)
  106.  
  107. queueIMG.join()
  108.  
  109.  
  110. #########################################################################
  111. # Video Section
  112. #########################################################################
  113. class convertVideo(threading.Thread):
  114.     def __init__(self,queueVID):
  115.         threading.Thread.__init__(self)
  116.         self.queueVID=queueVID
  117.    
  118.     def run(self):
  119.         while True:
  120.             self.videoPath=self.queueVID.get()
  121.             self.videoDir,self.videoName = os.path.split(self.videoPath)
  122.             self.thumbDir=os.path.join(self.videoDir,"@eaDir",self.videoName)
  123.             if os.path.isfile(os.path.join(self.thumbDir,xlName)) != 1:
  124.                 print "Now working on %s" % (self.videoPath)
  125.                 if os.path.isdir(self.thumbDir) != 1:
  126.                     try:os.makedirs(self.thumbDir)
  127.                     except:continue
  128.                
  129.                 # Following statements converts video to flv using ffmpeg
  130.                 self.ffmpegcmd = "ffmpeg -i '%s' -y -ar 44100 -r 12 -ac 2 -f flv -qscale 5 -s 320x180 -aspect 320:180 '%s/SYNOPHOTO:FILM.flv'" % (self.videoPath,self.thumbDir)
  131.                 self.ffmpegproc = subprocess.Popen(shlex.split(self.ffmpegcmd), stdout=subprocess.PIPE)
  132.                 self.ffmpegproc.communicate()[0]
  133.            
  134.                 # Create video thumbs
  135.                 self.tempThumb=os.path.join("/tmp",os.path.splitext(self.videoName)[0]+".jpg")
  136.                 self.ffmpegcmdThumb = "ffmpeg -i '%s' -y -an -ss 00:00:03 -an -r 1 -vframes 1 '%s'" % (self.videoPath,self.tempThumb)
  137.                 print "DEBUG: "+self.ffmpegcmdThumb
  138.                 self.ffmpegThumbproc = subprocess.Popen(shlex.split(self.ffmpegcmdThumb), stdout=subprocess.PIPE)
  139.                 self.ffmpegThumbproc.communicate()[0]
  140.                 self.image=Image.open(self.tempThumb)
  141.                 self.image.thumbnail(xlSize)
  142.                 self.image.save(os.path.join(self.thumbDir,xlName))
  143.                 self.image.thumbnail(mSize)
  144.                 self.image.save(os.path.join(self.thumbDir,mName))
  145.            
  146.             self.queueVID.task_done()
  147.  
  148.  
  149. # Finds all videos of type in extensions array
  150. videoList=[]
  151. print "[+] Looking for videos and populating queue (This might take a while...)"
  152. for path, subFolders, files in os.walk(rootdir):
  153.     for file in files:
  154.         ext=os.path.splitext(file)[1].lower()
  155.         if any(x in ext for x in videoExtensions):#check if extensions matches ext
  156.             if "@eaDir" not in path:
  157.                 if file != ".DS_Store" and file != ".apdisk" and file != "Thumbs.db": #maybe remove?
  158.                     videoList.append(os.path.join(path,file))
  159.  
  160. print "[+] We have found %i videos in search directory" % len(videoList)
  161. raw_input("\tPress Enter to continue or Ctrl-C to quit")
  162.  
  163. #spawn a pool of threads
  164. for i in range(NumOfThreads): #number of threads
  165.     v=convertVideo(queueVID)
  166.     v.setDaemon(True)
  167.     v.start()
  168.  
  169. # populate queueVID with Images
  170. for videoPath in videoList:
  171.     queueVID.put(videoPath) # could we possibly put this instead of videoList.append(os.path.join(path,file))
  172.  
  173. queueVID.join()
  174.  
  175. endTime=time.time()
  176. print "Time to complete %i" % (endTime-startTime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement