Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, os, io
- import json
- import re
- import pathlib
- import urllib.request
- from PIL import Image
- from bs4 import BeautifulSoup
- source = sys.argv[1]
- page = urllib.request.urlopen(source).read()
- soup = BeautifulSoup(page, 'html.parser')
- item = soup.find('',{'class':'pages'})
- last_item = item.find_all('div')[-2]
- cPage = int(last_item['data-ptimg'][5:9])
- p = pathlib.Path(soup.title.string + os.sep)
- p.mkdir(parents=True, exist_ok=True)
- for c in range(1, cPage + 2):
- #Open the image at given URL plus the image
- with urllib.request.urlopen(f'{source}/data/{c:04}.jpg') as url:
- f = io.BytesIO(url.read())
- im = Image.open(f)
- #Open the corresponding JSON
- with urllib.request.urlopen(f'{source}/data/{c:04}.ptimg.json') as url:
- j = io.BytesIO(url.read())
- data = json.load(j)
- #Create the new image
- out = Image.new('RGB', (data['views'][0]['width'], data['views'][0]['height']))
- #Descramble based off of JSON
- for i in data['views'][0]['coords']:
- src = re.split(r'(?<=\d)(?=\D)|(?<=\D)(?=\d)', i[2:])
- 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])))
- #Give status updates
- print(f'{c:04}.png')
- #Modify this line if you want to save in a different location or namespace
- out.save(p.joinpath(f'{c:04}.png'), format='PNG', optimize=True)
- print("Complete")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement