Advertisement
Roukanken

Image conversion

Nov 14th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. import pygame, shutil, os
  2.  
  3. path1 = "Input"
  4. path2 = "Output"
  5.  
  6. def transform (p, path1, path2):
  7.     p, path1, path2 = os.path.abspath(p), os.path.abspath(path1), os.path.abspath(path2)
  8.     return p.replace(path1, path2)
  9.  
  10.  
  11. #count the files
  12. dirfiles = 0
  13.    
  14. for directory, dirs, files in os.walk(path1):
  15.     dirfiles += len(files)
  16.        
  17.  
  18. currfiles = 0
  19. pygame.init()
  20. per, mod = int(input().strip()), int(input().strip())
  21.  
  22.  
  23. for directory, dirs, files in os.walk(path1):
  24.  
  25.     #create directory structure in path2
  26.     if not os.path.exists(transform(directory, path1, path2)) :
  27.         os.makedirs (transform(directory, path1, path2))
  28.        
  29.     for file in files:      
  30.         #create file paths
  31.         fromp = os.path.join(directory, file)
  32.         top   = transform (fromp, path1, path2)
  33.        
  34.  
  35.         #skip already done files, not image files and files belonging to another proccess
  36.         if currfiles % per != mod or os.path.exists(top) or os.path.splitext(fromp)[1].lower() not in [".jpg", ".png"]:
  37.             currfiles += 1
  38.             continue
  39.  
  40.  
  41.         #paths have Unicode names, so "pygame.image.load(fromp)" would not work
  42.         img = pygame.image.load(open(fromp, "rb"), fromp)
  43.         x, y = img.get_rect().bottomright      
  44.         zoom = max (600/x, 600/y)
  45.        
  46.         img_new = pygame.transform.smoothscale (img, (round (zoom*x), round(zoom*y)))
  47.  
  48.         #same here, Unicode paths
  49.         pygame.image.save (img_new, "temp.jpg")
  50.         shutil.copyfile("temp.jpg", top)
  51.    
  52.         currfiles +=1
  53.  
  54.         print ("\r{}/ {}".format (currfiles, dirfiles), end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement