Spaces:
Sleeping
Sleeping
Update pixelconversion.py
Browse files- pixelconversion.py +21 -17
pixelconversion.py
CHANGED
|
@@ -21,9 +21,7 @@ import db
|
|
| 21 |
"""### Open PDF and draw a rectangle on it using Fitz"""
|
| 22 |
def openDrawPDF(path):
|
| 23 |
doc = fitz.open(path)
|
| 24 |
-
out = fitz.open() # output PDF
|
| 25 |
page = doc[0]
|
| 26 |
-
print(page.rotation)
|
| 27 |
pix = page.get_pixmap() # render page to an image
|
| 28 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
| 29 |
img=np.array(pl)
|
|
@@ -32,26 +30,29 @@ def openDrawPDF(path):
|
|
| 32 |
ratio = pix.width / img.shape[1]
|
| 33 |
|
| 34 |
imgGry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 35 |
-
imgBW=cv2.threshold(imgGry,
|
|
|
|
| 36 |
contours, hierarchy = cv2.findContours(imgBW, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 37 |
|
| 38 |
newcontours = contours[2:]
|
| 39 |
areas = [cv2.contourArea(c) for c in newcontours]
|
| 40 |
max_index2 = np.argmax(areas)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
shape=[]
|
| 48 |
for point in approx:
|
| 49 |
x1, y1 = point[0]
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
if page.rect.height > page.rect.width:
|
| 56 |
rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
|
| 57 |
rotate=0
|
|
@@ -63,11 +64,14 @@ def openDrawPDF(path):
|
|
| 63 |
page.draw_rect([shape[3][0]-20,shape[3][1]-20,shape[1][0]+20,shape[1][1]+20], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
|
| 64 |
else:
|
| 65 |
page.draw_rect([shape[0][0]-20,shape[0][1]-20,shape[2][0]+20,shape[2][1]+20], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
|
|
|
|
| 66 |
text = """Scale Document"""
|
| 67 |
-
annot1=page.add_freetext_annot(rectText, text, fontsize=45, fontname='helv', border_color=(1,1,1), text_color=(1,1,1), rotate=
|
| 68 |
annot1.update()
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
"""### Extract color"""
|
| 72 |
|
| 73 |
def DetectColor(img,color=0):
|
|
|
|
| 21 |
"""### Open PDF and draw a rectangle on it using Fitz"""
|
| 22 |
def openDrawPDF(path):
|
| 23 |
doc = fitz.open(path)
|
|
|
|
| 24 |
page = doc[0]
|
|
|
|
| 25 |
pix = page.get_pixmap() # render page to an image
|
| 26 |
pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
|
| 27 |
img=np.array(pl)
|
|
|
|
| 30 |
ratio = pix.width / img.shape[1]
|
| 31 |
|
| 32 |
imgGry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
| 33 |
+
imgBW=cv2.threshold(imgGry, 254, 255, cv2.THRESH_BINARY_INV)[1]
|
| 34 |
+
|
| 35 |
contours, hierarchy = cv2.findContours(imgBW, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
| 36 |
|
| 37 |
newcontours = contours[2:]
|
| 38 |
areas = [cv2.contourArea(c) for c in newcontours]
|
| 39 |
max_index2 = np.argmax(areas)
|
| 40 |
+
# print(max_index)
|
| 41 |
+
cnt=newcontours[max_index2]
|
| 42 |
+
contour=cnt
|
| 43 |
+
|
| 44 |
+
perimeter = cv2.arcLength(contour, True)
|
| 45 |
+
approx = cv2.approxPolyDP(contour, 0.01* perimeter, True)
|
| 46 |
+
|
| 47 |
shape=[]
|
| 48 |
for point in approx:
|
| 49 |
x1, y1 = point[0]
|
| 50 |
+
p1 = fitz.Point(x1*ratio,y1*ratio)
|
| 51 |
+
p1=p1*page.derotation_matrix
|
| 52 |
+
shape.append([p1[0],p1[1]])
|
| 53 |
+
# cv2.circle (img, (x1, y1), 5, 255, 5)
|
| 54 |
+
print(shape)
|
| 55 |
+
# rotate=0
|
| 56 |
if page.rect.height > page.rect.width:
|
| 57 |
rectText=fitz.Rect(300, 200, (page.rect.width-80),( page.rect.width+80) )
|
| 58 |
rotate=0
|
|
|
|
| 64 |
page.draw_rect([shape[3][0]-20,shape[3][1]-20,shape[1][0]+20,shape[1][1]+20], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
|
| 65 |
else:
|
| 66 |
page.draw_rect([shape[0][0]-20,shape[0][1]-20,shape[2][0]+20,shape[2][1]+20], color = (75/255,0,130/255), width = 1,fill=(75/255,0,130/255),fill_opacity=0.9 )
|
| 67 |
+
|
| 68 |
text = """Scale Document"""
|
| 69 |
+
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)
|
| 70 |
annot1.update()
|
| 71 |
+
|
| 72 |
+
doc.save('2kk.pdf')#, encryption=encrypt, permissions=perm)
|
| 73 |
+
|
| 74 |
+
return doc#,encrypt,perm
|
| 75 |
"""### Extract color"""
|
| 76 |
|
| 77 |
def DetectColor(img,color=0):
|