File size: 9,382 Bytes
154e666 1e39b35 8552136 0ec50f5 8552136 bb0f07e 8552136 bb0f07e 8552136 0ec50f5 8099542 0ec50f5 bb0f07e 0ec50f5 8552136 0ec50f5 8552136 0ec50f5 bb0f07e 0ec50f5 154e666 bb0f07e 0ec50f5 5cd6add 0ec50f5 8099542 0ec50f5 8099542 5cd6add 8099542 5cd6add 8099542 bb0f07e 8099542 0ec50f5 8ce3007 0ec50f5 154e666 8099542 0ec50f5 8099542 |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# -*- coding: utf-8 -*-
"""pixelconversion.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1hfdgkYOw8w6DdJqsZx8txmw8INUGxesl
"""
# pip install pymupdf -q
"""### Imports"""
import fitz
from PIL import Image
import numpy as np
import cv2
import db
import tsadropboxretrieval
import fitz
import math
from io import BytesIO
################################################
### Check if page is visually rotated or not
def is_content_rotated(docpage):
docpage.draw_rect([250,250,docpage.mediabox.width-500,docpage.mediabox.height-500], color = (0,0,0), width = 1,fill=(0,0,0),fill_opacity=1)
pix = docpage.get_pixmap()
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
kernel = np.ones((3,3),np.uint8)
img = cv2.dilate(img,kernel, iterations=6)
img=cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV)[1]
# cv2_imshow(img)
contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Check the orientation of page through large shape drawn
for contour in contours:
rect = cv2.minAreaRect(contour)
angle = rect[-1]
# print(angle)
if angle != 0.0 and angle != 90.0 and angle != 180.0 and angle!=-90.0 and angle!=-180.0:
print("Page content appears visually rotated.")
return True
print("Page content does not appear visually rotated.")
return False
# def drawisrotated(data=0,pdf_content=0,dpi=0):
# if pdf_content:
# docPath = fitz.open(stream=pdf_content, filetype="pdf")
# else:
# docPath = fitz.open("pdf",data) #dropbox path
# pageDocPath=docPath[0]
# bool_T_F=is_content_rotated(pageDocPath)
# if bool_T_F==True:
# if pdf_content:
# docPath = fitz.open(stream=pdf_content, filetype="pdf")
# else:
# docPath = fitz.open("pdf",data) #dropbox path
# pageDocPath=docPath[0]
# 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)
# if dpi:
# pix=pageDocPath.get_pixmap(dpi=dpi)
# newwidth_ratio=pix.width/pageDocPath.mediabox.width
# newheight_ratio=pix.height/pageDocPath.mediabox.height
# else:
# newwidth_ratio=1
# newheight_ratio=1
# area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio)
# perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2)
# if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
# rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
# else:
# rectText=fitz.Rect(500, 200, (pageDocPath.mediabox.height-80),( pageDocPath.mediabox.height+80) )
# text = """Scale Document"""
# 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)
# annot1.update()
# # docPath.save('drawnOn.pdf')
# return docPath,area,perimeter
# else:
# if pdf_content:
# if dpi:
# doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
# return doc,area,perimeter
# else:
# doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
# return doc,area,perimeter
# else:
# if dpi:
# doc,area,perimeter=openDrawPDF(data,dpi=dpi)
# return doc,area,perimeter
# else:
# doc,area,perimeter=openDrawPDF(data)
# return doc,area,perimeter
def drawisrotated(data=0,pdf_content=0,dpi=0):
if pdf_content:
docPath = fitz.open(stream=pdf_content, filetype="pdf")
else:
docPath = fitz.open("pdf",data) #dropbox path
if pdf_content:
if dpi:
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content,dpi=dpi)
return doc,area,perimeter
else:
doc,area,perimeter=openDrawPDF(pdf_content=pdf_content)
return doc,area,perimeter
else:
if dpi:
doc,area,perimeter=openDrawPDF(data,dpi=dpi)
return doc,area,perimeter
else:
doc,area,perimeter=openDrawPDF(data)
return doc,area,perimeter
# def openDrawPDF(data=0,pdf_content=0,dpi=0):
# if pdf_content:
# doc = fitz.open(stream=pdf_content, filetype="pdf")
# else:
# doc = fitz.open("pdf",data) #dropbox path
# page = doc[0]
# print(page.rotation)
# if page.rect.height > page.rect.width:
# rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
# else:
# rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
# 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)
# if dpi:
# pix = page.get_pixmap(dpi=dpi)
# newwidth_ratio = pix.width / page.mediabox.width
# newheight_ratio = pix.height / page.mediabox.height
# else:
# print('No DPI specified')
# newwidth_ratio = 1
# newheight_ratio = 1
# # Apply the transformation to the rectangle's area and perimeter
# area = ((page.mediabox.width - 10 - 10) * newwidth_ratio) * ((page.mediabox.height - 10 - 10) * newheight_ratio)
# perimeter = (((page.mediabox.width - 10 - 10) * 2) * newwidth_ratio) + (((page.mediabox.height - 10 - 10) * 2) * newheight_ratio)
# # Apply the transformation to the text annotation (shift and scale its position)
# text = """Scale Document"""
# 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)
# annot1.update()
# # print('fitzzzzzzzzzzzz',fitz.__version__)
# # doc.save('drawnOn_transformed.pdf')
# return doc, area, perimeter
def openDrawPDF(data=0,pdf_content=0,dpi=0):
# Open the document
# doc = fitz.open(pdfOutput) # Dropbox
if pdf_content:
doc = fitz.open(stream=pdf_content, filetype="pdf")
else:
doc = fitz.open("pdf",data) #dropbox path
page = doc[0]
derotationMatrix=page.derotation_matrix
print(page.rotation)
if page.rect.height > page.rect.width:
rectText = fitz.Rect(300, 200, (page.mediabox.width - 80), (page.mediabox.width + 80))
else:
rectText = fitz.Rect(500, 200, (page.mediabox.height - 80), (page.mediabox.height + 80))
if dpi:
pix = page.get_pixmap(dpi=dpi)
newwidth_ratio = pix.width / page.mediabox.width
newheight_ratio = pix.height / page.mediabox.height
else:
print('No DPI specified')
newwidth_ratio = 1
newheight_ratio = 1
# Apply the transformation to the rectangle's area and perimeter
area = ((page.mediabox.width) * newwidth_ratio) * ((page.mediabox.height ) * newheight_ratio)
perimeter = (((page.mediabox.width ) * 2) * newwidth_ratio) + (((page.mediabox.height) * 2) * newheight_ratio)
# full page rectangle
# Original diagonal points
startRect = fitz.Point(0, 0)
endRect = fitz.Point(page.mediabox.width, page.mediabox.height)
# Apply derotation if needed
# startRect = startRect * derotationMatrix
# endRect = endRect * derotationMatrix
# Create rectangle corners from diagonal points
top_left = fitz.Point(startRect.x, startRect.y)
top_right = fitz.Point(endRect.x, startRect.y)
bottom_right = fitz.Point(endRect.x, endRect.y)
bottom_left = fitz.Point(startRect.x, endRect.y)
# List of points for polyline (closing the rectangle)
rect_points = [top_left, top_right, bottom_right, bottom_left, top_left]
# Add polyline annotation
annot = page.add_polyline_annot(rect_points)
annot.set_colors(stroke=(75/255, 0, 130/255), fill=(75/255, 0, 130/255))
annot.set_opacity(0.9)
annot.set_border(width=20)
annot.set_info(content=str(perimeter)+' m', subject='Perimeter Measurement', title="ADR Team")
annot.update()
page_width = page.rect.width
page_height = page.rect.height
startLength = fitz.Point(0, page_height / 2) *derotationMatrix # left side
endLength = fitz.Point(page_width, page_height / 2) *derotationMatrix # right side
print(f"Line length: {length} points")
# startLength = startLength * derotationMatrix
# endLength = endLength * derotationMatrix
shapeLength=[startLength, endLength]
line_annot = page.add_polyline_annot(shapeLength)
line_annot.set_colors(stroke=(75/255, 0, 130/255)) # red line
line_annot.set_border(width=10)
line_annot.set_info(content=str(length)+' m',subject='Perimeter Measurement', title="ADR Team")
line_annot.update()
# Apply the transformation to the text annotation (shift and scale its position)
text = """Scale Document"""
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)
annot1.update()
# doc.save('drawnOn_transformed.pdf')
return doc, area, perimeter |