-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetCdrImage.py
138 lines (126 loc) · 4.05 KB
/
GetCdrImage.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# ----------------------------------------------------------------------
# Send JPEG version of a CDR image to the browser, possibly resized.
# BZIssue::5001
# ----------------------------------------------------------------------
import sys
import cdr
from io import BytesIO
from PIL import Image, ImageEnhance
from cdrapi import db
from cdrcgi import Controller, FieldStorage
message = "GetCdrImage: No doc ID found"
fields = FieldStorage()
width = fields.getvalue("width")
res = fields.getvalue("res")
fname = fields.getvalue("fname")
quality = fields.getvalue("quality") or "85"
sharpen = fields.getvalue("sharpen")
pp = fields.getvalue("pp") or ""
cdrId = fields.getvalue("id") or Controller.bail(message)
conn = db.connect()
cursor = conn.cursor()
# ----------------------------------------------------------------------
# Support for Visuals Online. Four possible values for res:
# 300 - full size (for printing)
# 72 - 24% (viewing on screen)
# 150 - 1/2 size (in between)
# thumb - max 120px on a side (thumbnails)
# ----------------------------------------------------------------------
def widthFromRes(size, res):
width, height = size
if res == "300":
return None # don't resize
elif res == "150":
return int(round(width / 2.0))
elif res == "72":
return int(round(width * .24))
elif res == "thumb":
if width >= height:
if width <= 120:
return None
return 120
if height <= 120:
return None
return int(round(width * (120.0 / height)))
else:
Controller.bail("invalid res value '%s'" % res)
# If the docId comes in in the format 'CDR99999-111.jpg' it is coming
# from the PublishPreview with a size postfix.
# We capture the size instructions.
# --------------------------------------------------------------------
cdrId = cdrId.split('.')[0]
if cdrId.find('-') > 0:
docId, width = cdrId.split('-')
else:
docId = cdrId
intId = cdr.exNormalize(docId)[1]
# If the docId comes in the format 'CDR000999999' it is coming
# from the QC report. If it's coming in the format 'CDR999999' it is
# coming from the PublishPreview report. We're selecting the
# last publishable version for the PP report.
# ------------------------------------------------------------------
ppQuery = ""
if pp == 'Y':
ppQuery = "AND publishable = 'Y'"
elif pp == 'N':
ppQuery = ""
elif cdrId.find('CDR0') < 0:
ppQuery = "AND publishable = 'Y'"
query = """\
SELECT b.data
FROM doc_blob b
JOIN version_blob_usage v
ON b.id = v.blob_id
JOIN doc_version dv
ON dv.id = v.doc_id
AND dv.num = v.doc_version
WHERE v.doc_id = %d
AND dv.num = (SELECT max(num)
FROM doc_version
WHERE id = dv.id
%s
)""" % (intId, ppQuery)
cursor.execute(query)
rows = cursor.fetchall()
if not rows:
Controller.bail("%s not found" % docId)
image_bytes = rows[0][0]
iFile = BytesIO(image_bytes)
image = Image.open(iFile)
if image.mode == 'P':
image = image.convert('RGB')
try:
quality = int(quality)
except Exception:
quality = 85
if res:
width = widthFromRes(image.size, res)
if width:
try:
width = int(width)
iWidth, iHeight = image.size
if width < iWidth:
ratio = 1.0 * iHeight / iWidth
height = int(round(width * ratio))
image = image.resize((width, height), Image.ANTIALIAS)
except Exception as e:
Controller.bail(f"Failure resizing {docId}: {e}")
newImageFile = BytesIO()
if sharpen:
try:
sharpen = float(sharpen)
enh = ImageEnhance.Sharpness(image)
image = enh.enhance(sharpen)
except Exception:
pass
image.save(newImageFile, "JPEG", quality=quality)
image_bytes = newImageFile.getvalue()
if fname:
with open(fname, "wb") as fp:
fp.write(image_bytes)
sys.stdout.buffer.write(f"""\
Content-Type: image/jpeg
Content-Length: {len(image_bytes)}
""".encode("utf-8"))
sys.stdout.buffer.write(image_bytes)