forked from dword4/imgur2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgur2pdf.py
More file actions
executable file
·71 lines (63 loc) · 2.19 KB
/
imgur2pdf.py
File metadata and controls
executable file
·71 lines (63 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python
from imgurpython import ImgurClient
from PIL import Image
from creds import *
import PIL
from reportlab.pdfgen import canvas
from reportlab.platypus import SimpleDocTemplate, Paragraph, Image, Spacer, PageBreak
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.lib.enums import TA_JUSTIFY
import requests
import shutil
import os
# trying to fix the error messages
import urllib3
urllib3.disable_warnings()
album = raw_input('Album ID:')
client = ImgurClient(client_id, client_secret)
album_data = client.get_album(album)
album_file = album_data.title.replace(' ','_')+".pdf"
doc = SimpleDocTemplate(album_file,pagesize=letter,
rightMargin=25,leftMargin=25,
topMargin=25,bottomMargin=25)
ParagraphStyle(name = 'Normal',
fontName = "Verdana",
fontSize = 11,
leading = 15,
alignment = TA_JUSTIFY,
allowOrphans = 0,
spaceBefore = 20,
spaceAfter = 20,
wordWrap = 1)
Story=[]
styles=getSampleStyleSheet()
items = client.get_album_images(str(album))
for item in items:
#print(str(item.title))
response = requests.get(item.link, stream=True)
name = str(item.id)+".jpg"
with open(name, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
sc = PIL.Image.open(name)
width, height = sc.size
# time to look at the size and apply a resize ratio
if height <= 600 and width <= 800:
resize_ratio = 0.50
else :
resize_ratio = 0.15
scaled_width = width * resize_ratio
scaled_height = height * resize_ratio
im = Image(name, scaled_width, scaled_height)
title = str(item.title)
if item.title:
Story.append(Paragraph(item.title, styles["Normal"]))
Story.append(im)
if item.description:
Story.append(Paragraph(item.description, styles["Normal"]))
Story.append(PageBreak())
doc.build(Story)
print("file created -> "+str(album_file))
os.system("rm *.jpg")