Advertisement
Guest User

WebComic 6

a guest
Mar 20th, 2019
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. import sys, os, io
  2. import json
  3. import re
  4. import pathlib
  5. import urllib.request
  6. from PIL import Image
  7. from bs4 import BeautifulSoup
  8.  
  9. source = sys.argv[1]
  10.  
  11. page = urllib.request.urlopen(source).read()
  12. soup = BeautifulSoup(page, 'html.parser')
  13. item = soup.find('',{'class':'pages'})
  14. last_item = item.find_all('div')[-2]
  15. cPage = int(last_item['data-ptimg'][5:9])
  16.  
  17. p = pathlib.Path(soup.title.string + os.sep)
  18. p.mkdir(parents=True, exist_ok=True)
  19.  
  20. for c in range(1, cPage + 2):
  21.  
  22.     #Open the image at given URL plus the image
  23.     with urllib.request.urlopen(f'{source}/data/{c:04}.jpg') as url:
  24.         f = io.BytesIO(url.read())
  25.     im = Image.open(f)
  26.    
  27.     #Open the corresponding JSON
  28.     with urllib.request.urlopen(f'{source}/data/{c:04}.ptimg.json') as url:
  29.         j = io.BytesIO(url.read())
  30.     data = json.load(j)
  31.    
  32.     #Create the new image
  33.     out = Image.new('RGB', (data['views'][0]['width'], data['views'][0]['height']))
  34.    
  35.     #Descramble based off of JSON
  36.     for i in data['views'][0]['coords']:
  37.         src = re.split(r'(?<=\d)(?=\D)|(?<=\D)(?=\d)', i[2:])
  38.         out.paste(im.crop((int(src[0]), int(src[2]), int(src[0]) + int(src[4]), int(src[2]) + int(src[6]))), (int(src[8]), int(src[10])))
  39.    
  40.     #Give status updates
  41.     print(f'{c:04}.png')
  42.    
  43.     #Modify this line if you want to save in a different location or namespace
  44.     out.save(p.joinpath(f'{c:04}.png'), format='PNG', optimize=True)
  45. print("Complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement