Spaces:
Sleeping
Sleeping
Update pixelconversion.py
Browse files- pixelconversion.py +69 -78
pixelconversion.py
CHANGED
|
@@ -24,34 +24,48 @@ from io import BytesIO
|
|
| 24 |
################################################
|
| 25 |
### Check if page is visually rotated or not
|
| 26 |
|
| 27 |
-
def is_content_rotated(
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
for contour in contours:
|
| 31 |
rect = cv2.minAreaRect(contour)
|
| 32 |
angle = rect[-1]
|
| 33 |
-
print(angle)
|
| 34 |
-
if angle != 0.0 and angle != 90.0 and angle != 180.0:
|
| 35 |
print("Page content appears visually rotated.")
|
| 36 |
return True
|
| 37 |
print("Page content does not appear visually rotated.")
|
| 38 |
return False
|
| 39 |
|
| 40 |
-
def drawisrotated(data):
|
| 41 |
docPath = fitz.open("pdf",data) #dropbox path
|
| 42 |
pageDocPath=docPath[0]
|
| 43 |
-
if pageDocPath.rotation !=0:
|
| 44 |
-
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)
|
| 45 |
-
else:
|
| 46 |
-
pageDocPath.draw_rect([0+250,0+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)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 51 |
-
mask=DetectColor(img,color=(75,0,130))
|
| 52 |
-
bool_T_F=is_content_rotated(mask)
|
| 53 |
-
print(bool_T_F)
|
| 54 |
if bool_T_F==True:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
|
| 56 |
rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
|
| 57 |
else:
|
|
@@ -60,81 +74,58 @@ def drawisrotated(data):
|
|
| 60 |
text = """Scale Document"""
|
| 61 |
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)
|
| 62 |
annot1.update()
|
| 63 |
-
|
|
|
|
| 64 |
else:
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
"""### Open PDF and draw a rectangle on it using Fitz"""
|
| 70 |
-
def openDrawPDF(data):
|
| 71 |
|
| 72 |
doc=fitz.open("pdf", data)
|
| 73 |
page = doc[0]
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
else:
|
| 78 |
-
rectText=fitz.Rect(500, 200, (page.
|
| 79 |
if page.rotation !=0:
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
else:
|
| 82 |
-
page.draw_rect([0+10,0+10,page.
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
text = """Scale Document"""
|
| 85 |
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)
|
| 86 |
annot1.update()
|
| 87 |
|
| 88 |
-
|
|
|
|
| 89 |
"""### Extract color"""
|
| 90 |
|
| 91 |
-
def DetectColor(img,color=0):
|
| 92 |
-
|
| 93 |
-
imgCopy=img.copy()
|
| 94 |
-
imgCopy=cv2.cvtColor(imgCopy,cv2.COLOR_BGR2HSV)
|
| 95 |
-
tol=5 #tolerance
|
| 96 |
-
# color=hexRGB(color)
|
| 97 |
-
h,s,v = cv2.cvtColor(np.uint8([[[color[2],color[1],color[0]]]]),cv2.COLOR_BGR2HSV)[0][0]
|
| 98 |
-
|
| 99 |
-
lower =np.array( [h- tol, 100, 100 ], dtype='uint8')
|
| 100 |
-
upper = np.array( [h + tol, 255, 255],dtype='uint8')
|
| 101 |
-
|
| 102 |
-
mask = cv2.inRange(imgCopy, lower , upper)
|
| 103 |
-
|
| 104 |
-
detectedColors = cv2.bitwise_and(imgCopy,imgCopy, mask= mask) # Bitwise-AND mask and original image
|
| 105 |
-
|
| 106 |
-
kernel=np.ones((3,3),np.uint8)
|
| 107 |
-
mask=cv2.dilate(mask,kernel, iterations=5)
|
| 108 |
-
mask=cv2.erode(mask,kernel, iterations=4)
|
| 109 |
-
|
| 110 |
-
detectedColors=cv2.dilate(detectedColors,kernel, iterations=5)
|
| 111 |
-
detectedColors=cv2.erode(detectedColors,kernel, iterations=4)
|
| 112 |
-
|
| 113 |
-
detectedColors=cv2.cvtColor(detectedColors,cv2.COLOR_HSV2BGR)
|
| 114 |
-
detectedColors=cv2.medianBlur(detectedColors,7)
|
| 115 |
-
# cv2_imshow(detectedColors)
|
| 116 |
-
|
| 117 |
-
return mask
|
| 118 |
-
|
| 119 |
-
"""### For backend - calc area and perim"""
|
| 120 |
-
|
| 121 |
-
def getAreaPerimeter(path,name):
|
| 122 |
-
dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
|
| 123 |
-
md, res =dbxTeam.files_download(path= path+name)
|
| 124 |
-
data = res.content
|
| 125 |
-
doc=fitz.open("pdf", data)
|
| 126 |
-
area=0
|
| 127 |
-
perimeter=0
|
| 128 |
-
for page in doc:
|
| 129 |
-
pix = page.get_pixmap(dpi=300) # render page to an image
|
| 130 |
-
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
| 131 |
-
img=np.array(pl)
|
| 132 |
-
print(img.shape)
|
| 133 |
-
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 134 |
-
mask=DetectColor(img,color=(73,0,130)) #detect colored rect drawn on the pdf
|
| 135 |
-
# cv2.imwrite('maskk.png',mask)
|
| 136 |
-
contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 137 |
-
for contour in contours:
|
| 138 |
-
area = cv2.contourArea(contour)
|
| 139 |
-
perimeter = cv2.arcLength(contour, True)
|
| 140 |
-
return area,perimeter
|
|
|
|
| 24 |
################################################
|
| 25 |
### Check if page is visually rotated or not
|
| 26 |
|
| 27 |
+
def is_content_rotated(docpage):
|
| 28 |
+
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)
|
| 29 |
+
pix = docpage.get_pixmap()
|
| 30 |
+
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
| 31 |
+
img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
|
| 32 |
+
kernel = np.ones((3,3),np.uint8)
|
| 33 |
+
img = cv2.dilate(img,kernel, iterations=6)
|
| 34 |
+
img=cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV)[1]
|
| 35 |
+
# cv2_imshow(img)
|
| 36 |
+
contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 37 |
+
# Check the orientation of page through large shape drawn
|
| 38 |
for contour in contours:
|
| 39 |
rect = cv2.minAreaRect(contour)
|
| 40 |
angle = rect[-1]
|
| 41 |
+
# print(angle)
|
| 42 |
+
if angle != 0.0 and angle != 90.0 and angle != 180.0 and angle!=-90.0 and angle!=-180.0:
|
| 43 |
print("Page content appears visually rotated.")
|
| 44 |
return True
|
| 45 |
print("Page content does not appear visually rotated.")
|
| 46 |
return False
|
| 47 |
|
| 48 |
+
def drawisrotated(data,dpi=0):
|
| 49 |
docPath = fitz.open("pdf",data) #dropbox path
|
| 50 |
pageDocPath=docPath[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
bool_T_F=is_content_rotated(pageDocPath)
|
| 53 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if bool_T_F==True:
|
| 55 |
+
docPath = fitz.open("pdf",data) #dropbox path
|
| 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:
|
| 59 |
+
pix=pageDocPath.get_pixmap(dpi=dpi)
|
| 60 |
+
|
| 61 |
+
newwidth_ratio=pix.width/pageDocPath.mediabox.width
|
| 62 |
+
newheight_ratio=pix.height/pageDocPath.mediabox.height
|
| 63 |
+
else:
|
| 64 |
+
newwidth_ratio=1
|
| 65 |
+
newheight_ratio=1
|
| 66 |
+
area=((pageDocPath.mediabox.height-500-250)*newheight_ratio) * ((pageDocPath.mediabox.width-500-250)*newwidth_ratio)
|
| 67 |
+
perimeter=(((pageDocPath.mediabox.width-500-250)*newwidth_ratio) *2) + ( ((pageDocPath.mediabox.height-500-250)*newheight_ratio) *2)
|
| 68 |
+
|
| 69 |
if pageDocPath.mediabox.height >pageDocPath.mediabox.width:
|
| 70 |
rectText=fitz.Rect(300, 200, (pageDocPath.mediabox.width-80),( pageDocPath.mediabox.width+80) )
|
| 71 |
else:
|
|
|
|
| 74 |
text = """Scale Document"""
|
| 75 |
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)
|
| 76 |
annot1.update()
|
| 77 |
+
# docPath.save('drawnOn.pdf')
|
| 78 |
+
return docPath,area,perimeter
|
| 79 |
else:
|
| 80 |
+
if dpi:
|
| 81 |
+
doc,area,perimeter=openDrawPDF(data,dpi=dpi)
|
| 82 |
+
return doc,area,perimeter
|
| 83 |
+
else:
|
| 84 |
+
doc,area,perimeter=openDrawPDF(data)
|
| 85 |
+
return doc,area,perimeter,docPath
|
| 86 |
|
| 87 |
|
| 88 |
"""### Open PDF and draw a rectangle on it using Fitz"""
|
| 89 |
+
def openDrawPDF(data,dpi=0):
|
| 90 |
|
| 91 |
doc=fitz.open("pdf", data)
|
| 92 |
page = doc[0]
|
| 93 |
+
|
| 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 |
if page.rotation !=0:
|
| 100 |
+
# print('if')
|
| 101 |
+
page.draw_rect([250,250,page.mediabox.width-500,page.mediabox.height-500], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
|
| 102 |
+
if dpi:
|
| 103 |
+
pix=page.get_pixmap(dpi=dpi)
|
| 104 |
+
newwidth_ratio=pix.width/page.mediabox.width
|
| 105 |
+
newheight_ratio=pix.height/page.mediabox.height
|
| 106 |
+
else:
|
| 107 |
+
newwidth_ratio=1
|
| 108 |
+
newheight_ratio=1
|
| 109 |
+
area=((page.mediabox.height-500-250)*newheight_ratio) * ((page.mediabox.width-500-250)*newwidth_ratio)
|
| 110 |
+
perimeter=(((page.mediabox.width-500-250)*newwidth_ratio)*2) + (((page.mediabox.height-500-250)*newheight_ratio)*2)
|
| 111 |
+
|
| 112 |
else:
|
| 113 |
+
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 )
|
| 114 |
+
if dpi:
|
| 115 |
+
pix=page.get_pixmap(dpi=dpi)
|
| 116 |
+
newwidth_ratio=pix.width/page.mediabox.width
|
| 117 |
+
newheight_ratio=pix.height/page.mediabox.height
|
| 118 |
+
else:
|
| 119 |
+
newwidth_ratio=1
|
| 120 |
+
newheight_ratio=1
|
| 121 |
+
area=((page.mediabox.width-10-10)*newwidth_ratio)*((page.mediabox.height-10-10)*newheight_ratio)
|
| 122 |
+
perimeter=(((page.mediabox.width-10-10)*2)*newwidth_ratio)+(((page.mediabox.height-10-10)*2)*newheight_ratio)
|
| 123 |
+
|
| 124 |
text = """Scale Document"""
|
| 125 |
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)
|
| 126 |
annot1.update()
|
| 127 |
|
| 128 |
+
# doc.save('drawnOn.pdf')#, encryption=encrypt, permissions=perm)
|
| 129 |
+
return doc,area,perimeter
|
| 130 |
"""### Extract color"""
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|