generateChessboard.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. import io
  3. import os
  4. import sys
  5. from docx import Document
  6. from docx.shared import Inches, Cm
  7. import cv2
  8. import numpy as np
  9. scale = 37.79527559055118 # word的cm与px的比例(别问为什么, 自己去测)
  10. def main(param):
  11. docx_name, chessboard_cm, width_cm, height_cm = param
  12. chessboard_pixel = chessboard_cm*scale
  13. width = round(width_cm / chessboard_cm)
  14. height = round(height_cm / chessboard_cm)
  15. # width_pix = (width + 1) * chessboard_pixel
  16. # height_pix = (height + 1) * chessboard_pixel
  17. width_pix = round(width_cm * scale)
  18. height_pix = round(height_cm * scale)
  19. size = width_cm/height_cm
  20. image = np.zeros((height_pix, width_pix, 3), dtype=np.uint8)
  21. image.fill(255)
  22. color = (255, 255, 255)
  23. fill_color = 0
  24. for j in range(0, height + 1):
  25. y = round(j * chessboard_pixel)
  26. for i in range(0, width + 1):
  27. x0 = round(i * chessboard_pixel)
  28. y0 = y
  29. rect_start = (x0, y0)
  30. x1 = round(x0 + chessboard_pixel)
  31. y1 = round(y0 + chessboard_pixel)
  32. rect_end = (x1, y1)
  33. cv2.rectangle(image, rect_start, rect_end, color, 1, 0)
  34. image[y0:y1, x0:x1] = fill_color
  35. if width % 2:
  36. if i != width:
  37. fill_color = (0 if (fill_color == 255) else 255)
  38. else:
  39. if i != width + 1:
  40. fill_color = (0 if (fill_color == 255) else 255)
  41. bottom = round(width_pix/size)
  42. if bottom < height_pix:
  43. image = image[0:bottom, :, :]
  44. # 创建显示窗口
  45. win_name = "chessboard"
  46. # cv.namedWindow(win_name, cv.WINDOW_NORMAL)
  47. cv2.imwrite(win_name + ".bmp", image)
  48. # cv.imshow(win_name, image)
  49. # cv.waitKey()
  50. doc = Document() # 以默认模板建立文档对象
  51. distance = Inches(0)
  52. sec = doc.sections[0]
  53. sec.left_margin = distance # 以下依次设置左、右、上、下页面边距
  54. sec.right_margin = distance
  55. sec.top_margin = distance
  56. sec.bottom_margin = distance
  57. sec.page_width = Cm(width_cm) # 设置页面宽度
  58. sec.page_height = Cm(height_cm) # 设置页面高度
  59. img_encode = cv2.imencode('.bmp', image)[1]
  60. str_encode = img_encode .tostring()
  61. cc = io.BytesIO(str_encode)
  62. # img = doc.add_picture(cc, Cm(42.01))
  63. doc.add_picture(cc)
  64. # doc.add_picture(win_name + ".bmp")
  65. doc.save(docx_name) # 保存图像
  66. if __name__ == '__main__':
  67. param = ['chessboard.docx', 4, 42, 29.7] # 文档名, 正方格长度cm, 页面宽度cm, 页面高度cm
  68. length = len(sys.argv)
  69. if length > 1:
  70. for idx in range(1, 5 if (length > 5) else length):
  71. param[idx - 1] = sys.argv[idx]
  72. strlist = os.path.splitext(str(param[0]))
  73. if len(strlist) == 1:
  74. param[0] = str(param[0]) + '.docx'
  75. elif strlist[-1] != 'docx':
  76. param[0] = ''.join(strlist[0:-1]) + '.docx'
  77. # print(param)
  78. main(param)