Update pixelconversion.py
Browse files- pixelconversion.py +54 -36
pixelconversion.py
CHANGED
|
@@ -45,14 +45,20 @@ def is_content_rotated(docpage):
|
|
| 45 |
print("Page content does not appear visually rotated.")
|
| 46 |
return False
|
| 47 |
|
| 48 |
-
def drawisrotated(data,dpi=0):
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
pageDocPath=docPath[0]
|
| 51 |
|
| 52 |
bool_T_F=is_content_rotated(pageDocPath)
|
| 53 |
|
| 54 |
if bool_T_F==True:
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
pageDocPath=docPath[0]
|
| 57 |
pageDocPath.draw_rect([250,250,pageDocPath.mediabox.width-500,pageDocPath.mediabox.height-500], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9)
|
| 58 |
if dpi:
|
|
@@ -77,45 +83,57 @@ def drawisrotated(data,dpi=0):
|
|
| 77 |
# docPath.save('drawnOn.pdf')
|
| 78 |
return docPath,area,perimeter
|
| 79 |
else:
|
| 80 |
-
if
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
else:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
def openDrawPDF(data,dpi=0):
|
| 90 |
-
|
| 91 |
-
doc=fitz.open("pdf", data)
|
| 92 |
-
page = doc[0]
|
| 93 |
-
print('piixelsize',page.mediabox)
|
| 94 |
|
| 95 |
-
if page.rect.height >page.rect.width:
|
| 96 |
-
rectText=fitz.Rect(300, 200, (page.mediabox.width-80),( page.mediabox.width+80) )
|
| 97 |
-
else:
|
| 98 |
-
rectText=fitz.Rect(500, 200, (page.mediabox.height-80),( page.mediabox.height+80) )
|
| 99 |
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
newheight_ratio=1
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
print('fitzzzzzzzzzzzz',fitz.__version__)
|
| 118 |
-
# doc.save('drawnOn.pdf')#, encryption=encrypt, permissions=perm)
|
| 119 |
-
return doc,area,perimeter
|
| 120 |
-
"""### Extract color"""
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
print("Page content does not appear visually rotated.")
|
| 46 |
return False
|
| 47 |
|
| 48 |
+
def drawisrotated(data=0,pdf_content=0,dpi=0):
|
| 49 |
+
if pdf_content:
|
| 50 |
+
docPath = fitz.open(stream=pdf_content, filetype="pdf")
|
| 51 |
+
else:
|
| 52 |
+
docPath = fitz.open("pdf",data) #dropbox path
|
| 53 |
pageDocPath=docPath[0]
|
| 54 |
|
| 55 |
bool_T_F=is_content_rotated(pageDocPath)
|
| 56 |
|
| 57 |
if bool_T_F==True:
|
| 58 |
+
if pdf_content:
|
| 59 |
+
docPath = fitz.open(stream=pdf_content, filetype="pdf")
|
| 60 |
+
else:
|
| 61 |
+
docPath = fitz.open("pdf",data) #dropbox path
|
| 62 |
pageDocPath=docPath[0]
|
| 63 |
pageDocPath.draw_rect([250,250,pageDocPath.mediabox.width-500,pageDocPath.mediabox.height-500], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9)
|
| 64 |
if dpi:
|
|
|
|
| 83 |
# docPath.save('drawnOn.pdf')
|
| 84 |
return docPath,area,perimeter
|
| 85 |
else:
|
| 86 |
+
if pdf_content:
|
| 87 |
+
if dpi:
|
| 88 |
+
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
|
| 89 |
+
return doc,area,perimeter
|
| 90 |
+
else:
|
| 91 |
+
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
|
| 92 |
+
return doc,area,perimeter
|
| 93 |
else:
|
| 94 |
+
if dpi:
|
| 95 |
+
doc,area,perimeter=openDrawPDF(data,dpi=dpi)
|
| 96 |
+
return doc,area,perimeter
|
| 97 |
+
else:
|
| 98 |
+
doc,area,perimeter=openDrawPDF(data)
|
| 99 |
+
return doc,area,perimeter
|
| 100 |
+
|
| 101 |
|
| 102 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
def openDrawPDF(data=0,pdf_content=0,dpi=0):
|
| 106 |
|
| 107 |
+
if pdf_content:
|
| 108 |
+
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 109 |
+
else:
|
| 110 |
+
doc = fitz.open("pdf",data) #dropbox path
|
| 111 |
+
page = doc[0]
|
| 112 |
+
print(page.rotation)
|
|
|
|
| 113 |
|
| 114 |
+
if page.rect.height > page.rect.width:
|
| 115 |
+
rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
|
| 116 |
+
else:
|
| 117 |
+
rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
|
| 118 |
+
page.draw_rect([0 + 10, 0 + 10, page.mediabox.width - 10, page.mediabox.height - 10],color=(75 / 255, 0, 130 / 255), width=1, fill=(75 / 255, 0, 130 / 255), fill_opacity=0.9)
|
| 119 |
|
| 120 |
+
if dpi:
|
| 121 |
+
pix = page.get_pixmap(dpi=dpi)
|
| 122 |
+
newwidth_ratio = pix.width / page.mediabox.width
|
| 123 |
+
newheight_ratio = pix.height / page.mediabox.height
|
| 124 |
+
else:
|
| 125 |
+
print('No DPI specified')
|
| 126 |
+
newwidth_ratio = 1
|
| 127 |
+
newheight_ratio = 1
|
| 128 |
|
| 129 |
+
# Apply the transformation to the rectangle's area and perimeter
|
| 130 |
+
area = ((page.mediabox.width - 10 - 10) * newwidth_ratio) * ((page.mediabox.height - 10 - 10) * newheight_ratio)
|
| 131 |
+
perimeter = (((page.mediabox.width - 10 - 10) * 2) * newwidth_ratio) + (((page.mediabox.height - 10 - 10) * 2) * newheight_ratio)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
# Apply the transformation to the text annotation (shift and scale its position)
|
| 134 |
+
text = """Scale Document"""
|
| 135 |
+
annot1 = page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1, 1, 1),text_color=(1, 1, 1), rotate=page.rotation, align=1)
|
| 136 |
+
annot1.update()
|
| 137 |
+
# print('fitzzzzzzzzzzzz',fitz.__version__)
|
| 138 |
+
# doc.save('drawnOn_transformed.pdf')
|
| 139 |
+
return doc, area, perimeter
|