Spaces:
Paused
Paused
Update Code_2_7.py
Browse files- Code_2_7.py +45 -20
Code_2_7.py
CHANGED
|
@@ -85,8 +85,11 @@ This portion is used to convert vertices read from dxf to pixels in order to acc
|
|
| 85 |
|
| 86 |
"""PDF to image"""
|
| 87 |
|
| 88 |
-
def pdftoimg(datadoc):
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
page=doc[0]
|
| 91 |
pix = page.get_pixmap() # render page to an image
|
| 92 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
|
@@ -119,9 +122,12 @@ def get_paper_size_in_inches(width, height):
|
|
| 119 |
return size
|
| 120 |
return "Unknown Size"
|
| 121 |
|
| 122 |
-
def analyze_pdf(datadoc):
|
| 123 |
# Open the PDF file
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
# Iterate through pages and print their sizes
|
| 127 |
for page_number in range(len(pdf_document)):
|
|
@@ -176,10 +182,11 @@ def switch_case(argument):
|
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
-
def RetriveRatio(datadoc,dxfpath):
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
| 183 |
if(width > height ):
|
| 184 |
bigger=width
|
| 185 |
else:
|
|
@@ -1092,10 +1099,12 @@ def rotate_polygon(polygon, angle, pdfrotation,width,height,center_point=(0, 0))
|
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
-
def Create_DF(dxfpath,datadoc,hatched_areas):
|
| 1096 |
-
|
| 1097 |
-
FinalRatio= RetriveRatio(datadoc,dxfpath)
|
| 1098 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1099 |
# hatched_areas = get_hatched_areas(datadoc,dxfpath,FinalRatio)
|
| 1100 |
|
| 1101 |
# hatched_areas=remove_duplicate_shapes(new_hatched_areas)
|
|
@@ -1334,19 +1343,27 @@ def calculate_distance(p1, p2):
|
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
-
def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpath=0,pdfname=0):
|
| 1338 |
OutputPdfStage1='BB Trial.pdf'
|
| 1339 |
-
|
|
|
|
|
|
|
|
|
|
| 1340 |
|
| 1341 |
# hatched_areas = get_hatched_areas(datadoc,dxfpath,FinalRatio)
|
| 1342 |
# hatched_areas=remove_duplicate_shapes(new_hatched_areas)
|
| 1343 |
-
|
| 1344 |
-
|
|
|
|
|
|
|
| 1345 |
flipped_horizontal=flip(img)
|
| 1346 |
allcnts = []
|
| 1347 |
imgg = flipped_horizontal
|
| 1348 |
# imgtransparent1=imgg.copy()
|
| 1349 |
-
|
|
|
|
|
|
|
|
|
|
| 1350 |
page2 = doc[0]
|
| 1351 |
rotationOld=page2.rotation
|
| 1352 |
derotationMatrix=page2.derotation_matrix
|
|
@@ -1374,7 +1391,10 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpa
|
|
| 1374 |
allshapes=[]
|
| 1375 |
# Iterate through each polygon in metric units
|
| 1376 |
NewColors = []
|
| 1377 |
-
|
|
|
|
|
|
|
|
|
|
| 1378 |
i=0
|
| 1379 |
flagcolor = 0
|
| 1380 |
ColorCounter = 0
|
|
@@ -1675,10 +1695,15 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpa
|
|
| 1675 |
else:
|
| 1676 |
OutputPdfStage3 = remove_duplicate_annotations(OutputPdfStage2,threshold=10)
|
| 1677 |
|
| 1678 |
-
|
| 1679 |
-
|
|
|
|
|
|
|
| 1680 |
doc2 =fitz.open('pdf',OutputPdfStage3)
|
| 1681 |
-
|
|
|
|
|
|
|
|
|
|
| 1682 |
list1=pd.DataFrame(columns=['content', 'id', 'subject','color'])
|
| 1683 |
|
| 1684 |
# for page in doc:
|
|
|
|
| 85 |
|
| 86 |
"""PDF to image"""
|
| 87 |
|
| 88 |
+
def pdftoimg(datadoc,pdf_content=0):
|
| 89 |
+
if pdf_content:
|
| 90 |
+
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 91 |
+
else:
|
| 92 |
+
doc =fitz.open('pdf',datadoc)
|
| 93 |
page=doc[0]
|
| 94 |
pix = page.get_pixmap() # render page to an image
|
| 95 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
|
|
|
| 122 |
return size
|
| 123 |
return "Unknown Size"
|
| 124 |
|
| 125 |
+
def analyze_pdf(datadoc,pdf_content=0):
|
| 126 |
# Open the PDF file
|
| 127 |
+
if pdf_content:
|
| 128 |
+
pdf_document = fitz.open(stream=pdf_content, filetype="pdf")
|
| 129 |
+
else:
|
| 130 |
+
pdf_document = fitz.open('pdf',datadoc)
|
| 131 |
|
| 132 |
# Iterate through pages and print their sizes
|
| 133 |
for page_number in range(len(pdf_document)):
|
|
|
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
+
def RetriveRatio(datadoc,dxfpath,pdf_content=0):
|
| 186 |
+
if pdf_content:
|
| 187 |
+
width,height,paper_size = analyze_pdf (datadoc,pdf_content)
|
| 188 |
+
else:
|
| 189 |
+
width,height,paper_size = analyze_pdf (datadoc)
|
| 190 |
if(width > height ):
|
| 191 |
bigger=width
|
| 192 |
else:
|
|
|
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
+
def Create_DF(dxfpath,datadoc,hatched_areas,pdf_content=0):
|
|
|
|
|
|
|
| 1103 |
|
| 1104 |
+
if pdf_content:
|
| 1105 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath,pdf_content)
|
| 1106 |
+
else:
|
| 1107 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath)
|
| 1108 |
# hatched_areas = get_hatched_areas(datadoc,dxfpath,FinalRatio)
|
| 1109 |
|
| 1110 |
# hatched_areas=remove_duplicate_shapes(new_hatched_areas)
|
|
|
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
+
def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpath=0,pdfname=0,pdf_content=0):
|
| 1347 |
OutputPdfStage1='BB Trial.pdf'
|
| 1348 |
+
if pdf_content:
|
| 1349 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath,pdf_content)
|
| 1350 |
+
else:
|
| 1351 |
+
FinalRatio= RetriveRatio(datadoc,dxfpath)
|
| 1352 |
|
| 1353 |
# hatched_areas = get_hatched_areas(datadoc,dxfpath,FinalRatio)
|
| 1354 |
# hatched_areas=remove_duplicate_shapes(new_hatched_areas)
|
| 1355 |
+
if pdf_content:
|
| 1356 |
+
img,pix2=pdftoimg(datadoc,pdf_content)
|
| 1357 |
+
else:
|
| 1358 |
+
img,pix2=pdftoimg(datadoc)
|
| 1359 |
flipped_horizontal=flip(img)
|
| 1360 |
allcnts = []
|
| 1361 |
imgg = flipped_horizontal
|
| 1362 |
# imgtransparent1=imgg.copy()
|
| 1363 |
+
if pdf_content:
|
| 1364 |
+
doc = fitz.open(stream=pdf_content, filetype="pdf")
|
| 1365 |
+
else:
|
| 1366 |
+
doc = fitz.open('pdf',datadoc)
|
| 1367 |
page2 = doc[0]
|
| 1368 |
rotationOld=page2.rotation
|
| 1369 |
derotationMatrix=page2.derotation_matrix
|
|
|
|
| 1391 |
allshapes=[]
|
| 1392 |
# Iterate through each polygon in metric units
|
| 1393 |
NewColors = []
|
| 1394 |
+
if pdf_content:
|
| 1395 |
+
SimilarAreaDictionary=Create_DF(dxfpath,datadoc,hatched_areas,pdf_content)
|
| 1396 |
+
else:
|
| 1397 |
+
SimilarAreaDictionary=Create_DF(dxfpath,datadoc,hatched_areas)
|
| 1398 |
i=0
|
| 1399 |
flagcolor = 0
|
| 1400 |
ColorCounter = 0
|
|
|
|
| 1695 |
else:
|
| 1696 |
OutputPdfStage3 = remove_duplicate_annotations(OutputPdfStage2,threshold=10)
|
| 1697 |
|
| 1698 |
+
if pdf_content:
|
| 1699 |
+
latestimg,pix=pdftoimg(OutputPdfStage3,pdf_content)
|
| 1700 |
+
else:
|
| 1701 |
+
latestimg,pix=pdftoimg(OutputPdfStage3)
|
| 1702 |
doc2 =fitz.open('pdf',OutputPdfStage3)
|
| 1703 |
+
if pdf_content:
|
| 1704 |
+
gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(grouped_df , pdfname,pdfpath,pdf_content)
|
| 1705 |
+
else:
|
| 1706 |
+
gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(grouped_df , pdfname,pdfpath)
|
| 1707 |
list1=pd.DataFrame(columns=['content', 'id', 'subject','color'])
|
| 1708 |
|
| 1709 |
# for page in doc:
|