Update pixelconversion.py
Browse files- pixelconversion.py +151 -50
pixelconversion.py
CHANGED
|
@@ -19,6 +19,7 @@ import cv2
|
|
| 19 |
import db
|
| 20 |
import tsadropboxretrieval
|
| 21 |
import fitz
|
|
|
|
| 22 |
from io import BytesIO
|
| 23 |
|
| 24 |
################################################
|
|
@@ -45,77 +46,134 @@ def is_content_rotated(docpage):
|
|
| 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 |
-
|
| 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:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
newwidth_ratio=pix.width/pageDocPath.mediabox.width
|
| 68 |
-
newheight_ratio=pix.height/pageDocPath.mediabox.height
|
| 69 |
-
else:
|
| 70 |
-
newwidth_ratio=1
|
| 71 |
-
newheight_ratio=1
|
| 72 |
-
area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio)
|
| 73 |
-
perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2)
|
| 74 |
-
|
| 75 |
-
if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
|
| 76 |
-
rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
|
| 77 |
else:
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
text = """Scale Document"""
|
| 81 |
-
annot1=pageDocPath.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate= pageDocPath.rotation, align=1)
|
| 82 |
-
annot1.update()
|
| 83 |
-
# docPath.save('drawnOn.pdf')
|
| 84 |
-
return docPath,area,perimeter
|
| 85 |
else:
|
| 86 |
-
if
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return doc,area,perimeter
|
| 90 |
-
else:
|
| 91 |
-
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
|
| 92 |
-
return doc,area,perimeter
|
| 93 |
else:
|
| 94 |
-
|
| 95 |
-
|
| 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)
|
|
@@ -127,13 +185,56 @@ def openDrawPDF(data=0,pdf_content=0,dpi=0):
|
|
| 127 |
newheight_ratio = 1
|
| 128 |
|
| 129 |
# Apply the transformation to the rectangle's area and perimeter
|
| 130 |
-
area = ((page.mediabox.width
|
| 131 |
-
perimeter = (((page.mediabox.width
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 138 |
# doc.save('drawnOn_transformed.pdf')
|
| 139 |
return doc, area, perimeter
|
|
|
|
| 19 |
import db
|
| 20 |
import tsadropboxretrieval
|
| 21 |
import fitz
|
| 22 |
+
import math
|
| 23 |
from io import BytesIO
|
| 24 |
|
| 25 |
################################################
|
|
|
|
| 46 |
print("Page content does not appear visually rotated.")
|
| 47 |
return False
|
| 48 |
|
| 49 |
+
# def drawisrotated(data=0,pdf_content=0,dpi=0):
|
| 50 |
+
# if pdf_content:
|
| 51 |
+
# docPath = fitz.open(stream=pdf_content, filetype="pdf")
|
| 52 |
+
# else:
|
| 53 |
+
# docPath = fitz.open("pdf",data) #dropbox path
|
| 54 |
+
# pageDocPath=docPath[0]
|
| 55 |
+
|
| 56 |
+
# bool_T_F=is_content_rotated(pageDocPath)
|
| 57 |
+
|
| 58 |
+
# if bool_T_F==True:
|
| 59 |
+
# if pdf_content:
|
| 60 |
+
# docPath = fitz.open(stream=pdf_content, filetype="pdf")
|
| 61 |
+
# else:
|
| 62 |
+
# docPath = fitz.open("pdf",data) #dropbox path
|
| 63 |
+
# pageDocPath=docPath[0]
|
| 64 |
+
# 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)
|
| 65 |
+
# if dpi:
|
| 66 |
+
# pix=pageDocPath.get_pixmap(dpi=dpi)
|
| 67 |
+
|
| 68 |
+
# newwidth_ratio=pix.width/pageDocPath.mediabox.width
|
| 69 |
+
# newheight_ratio=pix.height/pageDocPath.mediabox.height
|
| 70 |
+
# else:
|
| 71 |
+
# newwidth_ratio=1
|
| 72 |
+
# newheight_ratio=1
|
| 73 |
+
# area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio)
|
| 74 |
+
# perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2)
|
| 75 |
+
|
| 76 |
+
# if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
|
| 77 |
+
# rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
|
| 78 |
+
# else:
|
| 79 |
+
# rectText=fitz.Rect(500, 200, (pageDocPath.mediabox.height-80),( pageDocPath.mediabox.height+80) )
|
| 80 |
+
|
| 81 |
+
# text = """Scale Document"""
|
| 82 |
+
# annot1=pageDocPath.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate= pageDocPath.rotation, align=1)
|
| 83 |
+
# annot1.update()
|
| 84 |
+
# # docPath.save('drawnOn.pdf')
|
| 85 |
+
# return docPath,area,perimeter
|
| 86 |
+
# else:
|
| 87 |
+
# if pdf_content:
|
| 88 |
+
# if dpi:
|
| 89 |
+
# doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
|
| 90 |
+
# return doc,area,perimeter
|
| 91 |
+
# else:
|
| 92 |
+
# doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
|
| 93 |
+
# return doc,area,perimeter
|
| 94 |
+
# else:
|
| 95 |
+
# if dpi:
|
| 96 |
+
# doc,area,perimeter=openDrawPDF(data,dpi=dpi)
|
| 97 |
+
# return doc,area,perimeter
|
| 98 |
+
# else:
|
| 99 |
+
# doc,area,perimeter=openDrawPDF(data)
|
| 100 |
+
# return doc,area,perimeter
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
|
| 104 |
def drawisrotated(data=0,pdf_content=0,dpi=0):
|
| 105 |
if pdf_content:
|
| 106 |
docPath = fitz.open(stream=pdf_content, filetype="pdf")
|
| 107 |
else:
|
| 108 |
docPath = fitz.open("pdf",data) #dropbox path
|
| 109 |
+
if pdf_content:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
if dpi:
|
| 111 |
+
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
|
| 112 |
+
return doc,area,perimeter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
+
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
|
| 115 |
+
return doc,area,perimeter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
else:
|
| 117 |
+
if dpi:
|
| 118 |
+
doc,area,perimeter=openDrawPDF(data,dpi=dpi)
|
| 119 |
+
return doc,area,perimeter
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
else:
|
| 121 |
+
doc,area,perimeter=openDrawPDF(data)
|
| 122 |
+
return doc,area,perimeter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
+
# def openDrawPDF(data=0,pdf_content=0,dpi=0):
|
| 127 |
|
| 128 |
+
# if pdf_content:
|
| 129 |
+
# doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 130 |
+
# else:
|
| 131 |
+
# doc = fitz.open("pdf",data) #dropbox path
|
| 132 |
+
# page = doc[0]
|
| 133 |
+
# print(page.rotation)
|
| 134 |
+
|
| 135 |
+
# if page.rect.height > page.rect.width:
|
| 136 |
+
# rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
|
| 137 |
+
# else:
|
| 138 |
+
# rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
|
| 139 |
+
# 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)
|
| 140 |
+
|
| 141 |
+
# if dpi:
|
| 142 |
+
# pix = page.get_pixmap(dpi=dpi)
|
| 143 |
+
# newwidth_ratio = pix.width / page.mediabox.width
|
| 144 |
+
# newheight_ratio = pix.height / page.mediabox.height
|
| 145 |
+
# else:
|
| 146 |
+
# print('No DPI specified')
|
| 147 |
+
# newwidth_ratio = 1
|
| 148 |
+
# newheight_ratio = 1
|
| 149 |
+
|
| 150 |
+
# # Apply the transformation to the rectangle's area and perimeter
|
| 151 |
+
# area = ((page.mediabox.width - 10 - 10) * newwidth_ratio) * ((page.mediabox.height - 10 - 10) * newheight_ratio)
|
| 152 |
+
# perimeter = (((page.mediabox.width - 10 - 10) * 2) * newwidth_ratio) + (((page.mediabox.height - 10 - 10) * 2) * newheight_ratio)
|
| 153 |
+
|
| 154 |
+
# # Apply the transformation to the text annotation (shift and scale its position)
|
| 155 |
+
# text = """Scale Document"""
|
| 156 |
+
# 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)
|
| 157 |
+
# annot1.update()
|
| 158 |
+
# # print('fitzzzzzzzzzzzz',fitz.__version__)
|
| 159 |
+
# # doc.save('drawnOn_transformed.pdf')
|
| 160 |
+
# return doc, area, perimeter
|
| 161 |
+
|
| 162 |
+
def openDrawPDF(data=0,pdf_content=0,dpi=0):
|
| 163 |
+
# Open the document
|
| 164 |
+
# doc = fitz.open(pdfOutput) # Dropbox
|
| 165 |
if pdf_content:
|
| 166 |
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 167 |
else:
|
| 168 |
doc = fitz.open("pdf",data) #dropbox path
|
| 169 |
page = doc[0]
|
| 170 |
+
derotationMatrix=page.derotation_matrix
|
| 171 |
print(page.rotation)
|
| 172 |
|
| 173 |
if page.rect.height > page.rect.width:
|
| 174 |
rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
|
| 175 |
else:
|
| 176 |
rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
|
|
|
|
| 177 |
|
| 178 |
if dpi:
|
| 179 |
pix = page.get_pixmap(dpi=dpi)
|
|
|
|
| 185 |
newheight_ratio = 1
|
| 186 |
|
| 187 |
# Apply the transformation to the rectangle's area and perimeter
|
| 188 |
+
area = ((page.mediabox.width) * newwidth_ratio) * ((page.mediabox.height ) * newheight_ratio)
|
| 189 |
+
perimeter = (((page.mediabox.width ) * 2) * newwidth_ratio) + (((page.mediabox.height) * 2) * newheight_ratio)
|
| 190 |
+
# full page rectangle
|
| 191 |
+
|
| 192 |
+
# Original diagonal points
|
| 193 |
+
startRect = fitz.Point(0, 0)
|
| 194 |
+
endRect = fitz.Point(page.mediabox.width, page.mediabox.height)
|
| 195 |
+
|
| 196 |
+
# Apply derotation if needed
|
| 197 |
+
# startRect = startRect * derotationMatrix
|
| 198 |
+
# endRect = endRect * derotationMatrix
|
| 199 |
+
|
| 200 |
+
# Create rectangle corners from diagonal points
|
| 201 |
+
top_left = fitz.Point(startRect.x, startRect.y)
|
| 202 |
+
top_right = fitz.Point(endRect.x, startRect.y)
|
| 203 |
+
bottom_right = fitz.Point(endRect.x, endRect.y)
|
| 204 |
+
bottom_left = fitz.Point(startRect.x, endRect.y)
|
| 205 |
+
|
| 206 |
+
# List of points for polyline (closing the rectangle)
|
| 207 |
+
rect_points = [top_left, top_right, bottom_right, bottom_left, top_left]
|
| 208 |
+
|
| 209 |
+
# Add polyline annotation
|
| 210 |
+
annot = page.add_polyline_annot(rect_points)
|
| 211 |
+
annot.set_colors(stroke=(75/255, 0, 130/255), fill=(75/255, 0, 130/255))
|
| 212 |
+
annot.set_opacity(0.9)
|
| 213 |
+
annot.set_border(width=20)
|
| 214 |
+
annot.set_info(content=str(perimeter)+' m', subject='Perimeter Measurement', title="ADR Team")
|
| 215 |
+
annot.update()
|
| 216 |
+
|
| 217 |
+
page_width = page.mediabox.width
|
| 218 |
+
page_height = page.mediabox.height
|
| 219 |
+
startLength = fitz.Point(0, page_height / 2) # left side
|
| 220 |
+
endLength = fitz.Point(page_width, page_height / 2) # right side
|
| 221 |
+
|
| 222 |
+
length = math.sqrt((endLength.x - startLength.x)**2 + (endLength.y - startLength.y)**2)
|
| 223 |
+
print(f"Line length: {length} points")
|
| 224 |
+
# startLength = startLength * derotationMatrix
|
| 225 |
+
# endLength = endLength * derotationMatrix
|
| 226 |
+
|
| 227 |
+
shapeLength=[startLength, endLength]
|
| 228 |
+
line_annot = page.add_polyline_annot(shapeLength)
|
| 229 |
+
line_annot.set_colors(stroke=(75/255, 0, 130/255)) # red line
|
| 230 |
+
line_annot.set_border(width=10)
|
| 231 |
+
line_annot.set_info(content=str(length)+' m',subject='Perimeter Measurement', title="ADR Team")
|
| 232 |
+
line_annot.update()
|
| 233 |
|
| 234 |
# Apply the transformation to the text annotation (shift and scale its position)
|
| 235 |
text = """Scale Document"""
|
| 236 |
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)
|
| 237 |
annot1.update()
|
| 238 |
+
|
| 239 |
# doc.save('drawnOn_transformed.pdf')
|
| 240 |
return doc, area, perimeter
|