Advertisement
Guest User

WebComic 3

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