Spaces:
Build error
Build error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import os
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
def convert2img(pdf_file):
|
| 8 |
+
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
| 9 |
+
page = doc.load_page(0)
|
| 10 |
+
pix = page.get_pixmap(dpi=150)
|
| 11 |
+
img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height, pix.width, pix.n)
|
| 12 |
+
if img.shape[2] == 4:
|
| 13 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGBA2BGR)
|
| 14 |
+
return img
|
| 15 |
+
|
| 16 |
+
def save_image(img):
|
| 17 |
+
filename = f"{uuid.uuid4().hex}.png"
|
| 18 |
+
path = os.path.join("/tmp", filename)
|
| 19 |
+
cv2.imwrite(path, img)
|
| 20 |
+
return filename
|