xah

webtoon stitch v0.0.2

xah
Mar 3rd, 2017 (edited)
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. #This code does everything in subdirectories only.
  2.  
  3. from PIL import Image
  4. import os
  5.  
  6. def getFolders(dir):
  7.     return [name for name in os.listdir(dir)
  8.             if os.path.isdir(os.path.join(dir, name))]
  9.  
  10. def getFiles(dir):
  11.     files = []
  12.     for c in os.listdir(dir):
  13.         _, ext = os.path.splitext(c)
  14.  
  15.         if ext == '.png':
  16.             files.append(c)
  17.  
  18.     return files
  19.  
  20. def merge_images(dir, file):
  21.     image = Image.open(os.path.join(dir, file))
  22.     base = Image.open(out_file)
  23.  
  24.     (width1, height1) = image.size
  25.     (width2, height2) = base.size
  26.  
  27.     result_width = max(width1, width2)
  28.     result_height = height1 + height2
  29.  
  30.     out = Image.new('RGB', (result_width, result_height))
  31.     out.paste(im=base, box=(0, 0))
  32.     out.paste(im=image, box=(0, height2))
  33.  
  34.     out.save(out_file,"PNG")
  35.     return out
  36.  
  37. def createTarget():
  38.     result = Image.new('RGB', (1, 1))
  39.     result.save(out_file,"PNG")
  40.  
  41. def name(loop):
  42.     loop = int(loop) + 1
  43.     if  loop < 10:
  44.         loop = '00' + str(loop)
  45.  
  46.     elif loop < 100:
  47.         loop = '0' + str(loop)
  48.  
  49.     else:
  50.         loop = str(loop)
  51.  
  52.     return str(loop)
  53.    
  54. dir = '/Users/user/Documents/[raws]All Raws/'
  55. rootdir = dir
  56. folder = getFolders(dir)
  57.  
  58. imgNum = '001'
  59.  
  60. for i in range(len(folder)):
  61.     subfolder = getFolders(rootdir + folder[i])
  62.     if subfolder != []:
  63.         for x in range(len(subfolder)):
  64.             subfolderPath = rootdir + folder[i] + '/' + subfolder[x]
  65.             images = getFiles(subfolderPath)
  66.             out_file = folder[i] + '_' + imgNum + '.png'
  67.             createTarget()
  68.             for im in images:
  69.                 merge_images(subfolderPath, im)
  70.             imgNum = name(imgNum)
  71.  
  72.         print(folder[i] + ': Done.')
  73.     else:
  74.         break
  75. print('\n\nAll done!')
Advertisement
Add Comment
Please, Sign In to add comment