Marthee commited on
Commit
50d22ba
·
1 Parent(s): d73765e

Update pixelconversion.py

Browse files
Files changed (1) hide show
  1. pixelconversion.py +14 -9
pixelconversion.py CHANGED
@@ -23,26 +23,31 @@ def openDrawPDF(path):
23
  doc = fitz.open(path)
24
  out = fitz.open() # output PDF
25
  page = doc[0]
 
26
  pix = page.get_pixmap() # render page to an image
27
  pl=Image.frombytes('RGB', [pix.width,pix.height],pix.samples)
28
  img=np.array(pl)
29
 
30
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
31
- ratio = pix.width / img.shape[1]
32
-
33
- img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
34
- imgBW=cv2.threshold(img, 250, 255, cv2.THRESH_BINARY_INV)[1]
35
 
 
 
36
  contours, hierarchy = cv2.findContours(imgBW, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
37
- img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
38
 
39
- contour=contours[2]
40
- perimeter = cv2.arcLength(contour, True)
41
- approx = cv2.approxPolyDP(contour, 0.01* perimeter, True)
 
 
 
 
 
 
42
  shape=[]
43
  for point in approx:
44
  x1, y1 = point[0]
45
-
46
  shape.append([(int(x1*ratio)),(int(y1*ratio))])
47
  cv2.circle (img, (x1, y1), 5, 255, 5)
48
 
 
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)
30
 
31
  img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
32
+ ratio = pix.width / img.shape[1]
 
 
 
33
 
34
+ imgGry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
35
+ imgBW=cv2.threshold(imgGry, 250, 255, cv2.THRESH_BINARY_INV)[1]
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
+ contour=newcontours[max_index2]
42
+
43
+ area=cv2.contourArea(contour)
44
+ if area >500:
45
+ perimeter = cv2.arcLength(contour, True)
46
+ approx = cv2.approxPolyDP(contour, 0.01* perimeter, True)
47
  shape=[]
48
  for point in approx:
49
  x1, y1 = point[0]
50
+
51
  shape.append([(int(x1*ratio)),(int(y1*ratio))])
52
  cv2.circle (img, (x1, y1), 5, 255, 5)
53