convert2img.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/python3.6
  2. # -*- coding: utf-8 -*-
  3. # @Time : 2021/3/17 13:13
  4. # @Author : lijunliang
  5. # @Email : lijunliang@topnet.net.cn
  6. # @File : convert2img.py
  7. # @Software: PyCharm
  8. import fitz
  9. import os
  10. from loguru import logger
  11. import cv2 as cv
  12. import numpy as np
  13. def create_border(image):
  14. """
  15. 修复尺寸
  16. :param image:
  17. :return:
  18. """
  19. img_height, img_width, _ = image.shape
  20. add_w_l, add_h_u = int((248 - img_height) / 2), int((200 - img_width) / 2)
  21. image = cv.copyMakeBorder(image, add_w_l, add_w_l, add_h_u, add_h_u, cv.BORDER_CONSTANT, value=[255, 255, 255, 255])
  22. return image
  23. def convert_img(pdf_path: str) -> str:
  24. """
  25. pdf首页转图片
  26. :param pdf_path:pdf文件路径
  27. :return:
  28. """
  29. try:
  30. doc = fitz.open(pdf_path)
  31. pdf_name = os.path.splitext(pdf_path)[0]
  32. if doc.page_count < 1:
  33. return ""
  34. page = doc[0]
  35. rotate = int(0)
  36. # 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。
  37. zoom_x = 2.0
  38. zoom_y = 2.0
  39. trans = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
  40. pm = page.getPixmap(matrix=trans, alpha=False)
  41. image_path = '%s.png' % pdf_name
  42. pm.writePNG(image_path)
  43. image_path = image_zoom(image_path)
  44. return image_path
  45. except Exception as e:
  46. logger.warning("生成首页失败---》%s" % e)
  47. return ""
  48. def image_zoom(img_path: str):
  49. """
  50. 图片缩放
  51. :param img_path: 图片路径
  52. :return:
  53. """
  54. image = cv.imread(img_path)
  55. img_height, img_width, _ = image.shape
  56. if (img_height / img_width) > (248 / 200):
  57. rate = 248 / img_height
  58. else:
  59. rate = 200 / img_width
  60. resize_image = cv.resize(image, (int(img_width * rate), int(img_height * rate)),
  61. interpolation=cv.INTER_AREA)
  62. new_image = create_border(resize_image) # 修复尺寸
  63. kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32) # 定义一个核
  64. dst = cv.filter2D(new_image, -1, kernel=kernel)
  65. cv.imwrite(img_path, dst) # 保存
  66. return img_path
  67. if __name__ == '__main__':
  68. file_path="../folder/071efc233bac58e9b655f00961a3c63728d15a71.pdf"
  69. convert_img(file_path)