相机标定与3D重建(2)创建标定板(下)

这个小应用程序可以将一些常用的 opencv 标记,例如 ArUco、ArUcoGrid、Chessboard 和 ChArUco 保存到矢量图形文件中。支持的矢量图形文件格式:.svg、.pdf 和 .ps。(实测中,直接生成pdf文件错误,文件为空,但是可以先生成svg文件然后利用svg2pdf生成pdf文件即可,文末有实现
相机标定与3D重建(2)创建标定板(下)

1.依赖库

2.教程

2.1 图形用户界面

python MarkerPrinterGUI.py

您可以在 GUI 选项卡中切换 ArUco、ArUcoGrid、Chessboard 和 ChArUco 模式,然后您可以从 GUI 菜单中选择字典并在 GUI 入口处修改棋盘形状、标记大小、边框宽度…等,最后单击预览或保存按钮以在 GUI 窗口上显示标记图像或将其保存到文件。

2.2 命令行

2.2.1打印帮助

python MarkerPrinter.py

2.2.2打印预定义字典列表

python MarkerPrinter.py --list_dictionary

2.2.3保存棋盘标定板

python MarkerPrinter.py --chess --file "./chess.pdf" --size_x 16 --size_y 9 --square_length 0.09

2.2.4保存ArUco标定板

python MarkerPrinter.py --aruco --file "./aruco.pdf" --dictionary DICT_ARUCO_ORIGINAL --marker_length 0.07 --marker_id 0 --border_bits 1

2.2.5保存 ArUco Grid标定板

python MarkerPrinter.py --aruco_grid --file "./aruco_grid.pdf" --dictionary DICT_ARUCO_ORIGINAL --size_x 16 --size_y 9 --marker_length 0.07 --marker_separation 0.02 --first_marker 0 --border_bits 1

2.2.5保存ChArUco标定板

python MarkerPrinter.py --charuco --file "./charuco.pdf" --dictionary DICT_ARUCO_ORIGINAL --size_x 16 --size_y 9 --square_length 0.09 --marker_length 0.07 --border_bits 1

2.3有用的选项

2.3.1将输出分成块

如果您使用的是消费级打印机,您将无法打印太大的标记,因此只需在将标记保存到文件之前在 GUI subSize 条目中设置块形状,它将输出标记分成块。如果您使用命令行界面,只需添加 --sub_size_x x --sub_size_y y 作为参数。

2.3.2页面边框

如果直接打印图像,则需要添加页面边框以保护标记,因此只需在将标记保存到文件之前在 GUI pageBorder 条目中设置页面边框。如果您使用命令行界面,只需添加 --page_border_x x --page_border_y y 作为参数。

2.3.3生成 aruco 数据

虽然有内置的 aruco 字典数据,但是如果要更新字典(如果 aruco 更新预定义的字典列表),只需安装 opencv-pythonopencv-contrib-python,然后运行

python MarkerPrinter.py --generate arucoDictBytesList.npz

3 代码地址

https://github.com/opencv/opencv_contrib/tree/4.x/modules/aruco/misc/pattern_generator

3.1代码一

# MarkerPrinterGUI.py 
#!/usr/bin/env python3

# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2019, Josh Chien. All rights reserved.

from MarkerPrinter import *

import tkinter as tk
from tkinter import ttk, filedialog, messagebox

import time

import PIL.Image
import PIL.ImageTk

class MarkerPrinterGUI:

    def VisDPI(self, shape):
        scale0 = float(self.displayShape[0]) / float(shape[0])
        scale1 = float(self.displayShape[1]) / float(shape[1])
        if(scale0 > scale1):
            return scale1 * 96.0
        else:
            return scale0 * 96.0

    def OnShowingHelpGithub(self):
        messagebox.showinfo("Github",
            "https://github.com/dogod621/OpenCVMarkerPrinter")

    def OnCloseWindow(self):
        if(self.window is not None):
            if messagebox.askokcancel("Quit", "Do you want to quit?"):
                self.window.destroy()
                self.window = None

    def OnSelectCharucoMarkerDictionary(self, pDictName):
        self.charucoMarkerDictionaryStr.set(pDictName)

    def __SaveMarker(GenMarkerImageCallback, *args, **kwargs):

        if(kwargs.get("subSize",None) is not None):
            subSizeX, subSizeY = kwargs["subSize"]

            kwargs["subSize"] = None

            if(subSizeX > 0):
                if(subSizeY > 0):
                    kwargs["subSize"] = (subSizeX, subSizeY)
                else:
                    kwargs["subSize"] = (subSizeX, sizeY)
            else:
                if(subSizeY > 0):
                    kwargs["subSize"] = (sizeX, subSizeY)
                else:
                    kwargs["subSize"] = None

        try:
            askFileName = filedialog.asksaveasfilename(initialdir = os.path.abspath("./"), title = "Output", filetypes = (\
                ("scalable vector graphics files","*.svg"), \
                ("portable document format files","*.pdf"), \
                ("post script files","*.ps")),
                defaultextension="*.*")

            if (askFileName):
                GenMarkerImageCallback(askFileName, *args, **kwargs)

        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Save marker failed")
            return

    def OnPreviewOrSaveCharucoMarker(self, askSave = False):
        try:
            sizeX = int(self.charucoMarkerChessboardSizeXStr.get())
            sizeY = int(self.charucoMarkerChessboardSizeYStr.get())
            squareLength = float(self.charucoMarkerSquareLengthStr.get())
            markerLength = float(self.charucoMarkerMarkerLengthStr.get())
            borderBits = int(self.charucoMarkerBorderBitsStr.get())
            dictionary = self.charucoMarkerDictionaryStr.get()
            subSizeX = int(self.charucoMarkerSaveSubSizeXStr.get())
            subSizeY = int(self.charucoMarkerSaveSubSizeYStr.get())
            pageBorderX = float(self.charucoMarkerSavePageBorderXStr.get())
            pageBorderY = float(self.charucoMarkerSavePageBorderYStr.get())
        except ValueError as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Enter invalid parameters")
            return
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Fail to get parameters")
            return

        # Preview
        try:
            dpi = self.VisDPI(((sizeY * squareLength + pageBorderY * 2) * MarkerPrinter.ptPerMeter, (sizeX * squareLength + pageBorderX * 2) * MarkerPrinter.ptPerMeter))
            tkImage = PIL.ImageTk.PhotoImage(image = MarkerPrinter.PreviewCharucoMarkerImage(dictionary, (sizeX, sizeY), squareLength, markerLength, borderBits=borderBits, pageBorder = (pageBorderX, pageBorderY), dpi=dpi))
            self.charucoMarkerImageLabel.imgtk = tkImage
            self.charucoMarkerImageLabel.config(image=tkImage)
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "create marker failed")
            return

        # Save
        if(askSave):
            MarkerPrinterGUI.__SaveMarker(MarkerPrinter.GenCharucoMarkerImage, \
                dictionary, (sizeX, sizeY), squareLength, markerLength, borderBits=borderBits, subSize = (subSizeX, subSizeY), pageBorder = (pageBorderX, pageBorderY))

    def OnPreviewCharucoMarker(self):
        self.OnPreviewOrSaveCharucoMarker(askSave = False)

    def OnSaveCharucoMarker(self):
        self.OnPreviewOrSaveCharucoMarker(askSave = True)

    def InitCharucoMarkerTab(self):
        self.charucoMarkerUIFrame = ttk.Frame(self.charucoMarkerTab)
        self.charucoMarkerImageTab = ttk.Frame(self.charucoMarkerTab)
        self.charucoMarkerUIFrame2 = ttk.Frame(self.charucoMarkerTab)

        self.charucoMarkerUIFrame.grid(row=0, column=0, sticky = tk.NSEW)
        self.charucoMarkerImageTab.grid(row=1, column=0, sticky = tk.NSEW)
        self.charucoMarkerUIFrame2.grid(row=2, column=0, sticky = tk.NSEW)

        self.charucoMarkerImageLabel = tk.Label(self.charucoMarkerImageTab)
        self.charucoMarkerImageLabel.grid(row=0, column=0, sticky = tk.NSEW)

        tk.Label(self.charucoMarkerUIFrame, text="dictionary").grid(row=0, column=0, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame, text="chessboardSizeX").grid(row=0, column=1, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame, text="chessboardSizeY").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame, text="squareLength (Unit: Meter)").grid(row=0, column=3, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame, text="markerLength (Unit: Meter)").grid(row=0, column=4, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame, text="borderBits").grid(row=0, column=5, sticky = tk.NSEW)

        self.charucoMarkerDictionaryStr = tk.StringVar()
        self.charucoMarkerChessboardSizeXStr = tk.StringVar()
        self.charucoMarkerChessboardSizeXStr.set("16")
        self.charucoMarkerChessboardSizeYStr = tk.StringVar()
        self.charucoMarkerChessboardSizeYStr.set("9")
        self.charucoMarkerSquareLengthStr = tk.StringVar()
        self.charucoMarkerSquareLengthStr.set("0.09")
        self.charucoMarkerMarkerLengthStr = tk.StringVar()
        self.charucoMarkerMarkerLengthStr.set("0.07")
        self.charucoMarkerBorderBitsStr = tk.StringVar()
        self.charucoMarkerBorderBitsStr.set("1")

        self.charucoMarkerDictionaryMenue = tk.OptionMenu(self.charucoMarkerUIFrame, self.charucoMarkerDictionaryStr, "DICT_ARUCO_ORIGINAL", command = self.OnSelectCharucoMarkerDictionary)
        self.charucoMarkerDictionaryMenue.grid(row=1, column=0, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame, textvariable=self.charucoMarkerChessboardSizeXStr).grid(row=1, column=1, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame, textvariable=self.charucoMarkerChessboardSizeYStr).grid(row=1, column=2, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame, textvariable=self.charucoMarkerSquareLengthStr).grid(row=1, column=3, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame, textvariable=self.charucoMarkerMarkerLengthStr).grid(row=1, column=4, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame, textvariable=self.charucoMarkerBorderBitsStr).grid(row=1, column=5, sticky = tk.NSEW)

        tk.Button(self.charucoMarkerUIFrame2, text = "Preview", command = self.OnPreviewCharucoMarker).grid(row=1, column=0, sticky = tk.NSEW)
        tk.Button(self.charucoMarkerUIFrame2, text = "Save", command = self.OnSaveCharucoMarker).grid(row=1, column=1, sticky = tk.NSEW)

        tk.Label(self.charucoMarkerUIFrame2, text="Save options:").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="(set 0 as disable)").grid(row=1, column=2, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="subSizeX").grid(row=0, column=3, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="subSizeY").grid(row=0, column=4, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="Divide to chunks, chunk sizeX").grid(row=2, column=3, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="Divide to chunks, chunk sizeY").grid(row=2, column=4, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="pageBorderX (Unit: Meter)").grid(row=0, column=5, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="pageBorderY (Unit: Meter)").grid(row=0, column=6, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="Border or page").grid(row=2, column=5, sticky = tk.NSEW)
        tk.Label(self.charucoMarkerUIFrame2, text="Border or page").grid(row=2, column=6, sticky = tk.NSEW)

        self.charucoMarkerSaveSubSizeXStr = tk.StringVar()
        self.charucoMarkerSaveSubSizeXStr.set("0")
        self.charucoMarkerSaveSubSizeYStr = tk.StringVar()
        self.charucoMarkerSaveSubSizeYStr.set("0")
        self.charucoMarkerSavePageBorderXStr = tk.StringVar()
        self.charucoMarkerSavePageBorderXStr.set("0.02")
        self.charucoMarkerSavePageBorderYStr = tk.StringVar()
        self.charucoMarkerSavePageBorderYStr.set("0.02")

        tk.Entry(self.charucoMarkerUIFrame2, textvariable=self.charucoMarkerSaveSubSizeXStr).grid(row=1, column=3, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame2, textvariable=self.charucoMarkerSaveSubSizeYStr).grid(row=1, column=4, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame2, textvariable=self.charucoMarkerSavePageBorderXStr).grid(row=1, column=5, sticky = tk.NSEW)
        tk.Entry(self.charucoMarkerUIFrame2, textvariable=self.charucoMarkerSavePageBorderYStr).grid(row=1, column=6, sticky = tk.NSEW)

        self.charucoMarkerDictionaryMenue['menu'].delete(0, 'end')
        for dictName in self.dictList:
            self.charucoMarkerDictionaryMenue['menu'].add_command(label=dictName, command=tk._setit(self.charucoMarkerDictionaryStr, dictName, self.OnSelectCharucoMarkerDictionary))

        self.OnSelectCharucoMarkerDictionary("DICT_ARUCO_ORIGINAL")

    def OnSelectArucoGridMarkerDictionary(self, pDictName):
        self.arucoGridMarkerDictionaryStr.set(pDictName)

    def OnPreviewOrSaveArucoGridMarker(self, askSave = False):
        try:
            markersX = int(self.arucoGridMarkerMarkersXStr.get())
            markersY = int(self.arucoGridMarkerMarkersYStr.get())
            markerLength = float(self.arucoGridMarkerMarkerLengthStr.get())
            markerSeparation = float(self.arucoGridMarkerMarkerSeparationStr.get())
            borderBits = int(self.arucoGridMarkerBorderBitsStr.get())
            firstMarker = int(self.arucoGridMarkerFirstMarkerStr.get())
            dictionary = self.arucoGridMarkerDictionaryStr.get()
            subSizeX = int(self.arucoGridMarkerSaveSubSizeXStr.get())
            subSizeY = int(self.arucoGridMarkerSaveSubSizeYStr.get())
            pageBorderX = float(self.arucoGridMarkerSavePageBorderXStr.get())
            pageBorderY = float(self.arucoGridMarkerSavePageBorderYStr.get())
        except ValueError as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Enter invalid parameters")
            return
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Fail to get parameters")
            return

        # Preview
        try:
            dpi=self.VisDPI(((markersY * markerLength + (markersY  - 1) * markerSeparation + pageBorderY * 2) * MarkerPrinter.ptPerMeter, (markersX * markerLength + (markersX  - 1) * markerSeparation + pageBorderX * 2) * MarkerPrinter.ptPerMeter))
            tkImage = PIL.ImageTk.PhotoImage(image = MarkerPrinter.PreviewArucoGridMarkerImage(dictionary, (markersX, markersY), markerLength, markerSeparation, firstMarker, borderBits=borderBits, pageBorder = (pageBorderX, pageBorderY), dpi=dpi))
            self.arucoGridMarkerImageLabel.imgtk = tkImage
            self.arucoGridMarkerImageLabel.config(image=tkImage)
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "create marker failed")
            return

        # Save
        if(askSave):
            MarkerPrinterGUI.__SaveMarker(MarkerPrinter.GenArucoGridMarkerImage, \
                dictionary, (markersX, markersY), markerLength, markerSeparation, firstMarker, borderBits=borderBits, subSize = (subSizeX, subSizeY), pageBorder = (pageBorderX, pageBorderY))

    def OnPreviewArucoGridMarker(self):
        self.OnPreviewOrSaveArucoGridMarker(askSave = False)

    def OnSaveArucoGridMarker(self):
        self.OnPreviewOrSaveArucoGridMarker(askSave = True)

    def InitArucoGridMarkerTab(self):
        self.arucoGridMarkerUIFrame = ttk.Frame(self.arucoGridMarkerTab)
        self.arucoGridMarkerImageTab = ttk.Frame(self.arucoGridMarkerTab)
        self.arucoGridMarkerUIFrame2 = ttk.Frame(self.arucoGridMarkerTab)

        self.arucoGridMarkerUIFrame.grid(row=0, column=0, sticky = tk.NSEW)
        self.arucoGridMarkerImageTab.grid(row=1, column=0, sticky = tk.NSEW)
        self.arucoGridMarkerUIFrame2.grid(row=2, column=0, sticky = tk.NSEW)

        self.arucoGridMarkerImageLabel = tk.Label(self.arucoGridMarkerImageTab)
        self.arucoGridMarkerImageLabel.grid(row=0, column=0, sticky = tk.NSEW)

        tk.Label(self.arucoGridMarkerUIFrame, text="dictionary").grid(row=0, column=0, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame, text="markersX").grid(row=0, column=1, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame, text="markersY").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame, text="markerLength (Unit: Meter)").grid(row=0, column=3, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame, text="markerSeparation (Unit: Meter)").grid(row=0, column=4, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame, text="firstMarker").grid(row=0, column=5, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame, text="borderBits").grid(row=0, column=6, sticky = tk.NSEW)

        self.arucoGridMarkerDictionaryStr = tk.StringVar()
        self.arucoGridMarkerMarkersXStr = tk.StringVar()
        self.arucoGridMarkerMarkersXStr.set("16")
        self.arucoGridMarkerMarkersYStr = tk.StringVar()
        self.arucoGridMarkerMarkersYStr.set("9")
        self.arucoGridMarkerMarkerLengthStr = tk.StringVar()
        self.arucoGridMarkerMarkerLengthStr.set("0.07")
        self.arucoGridMarkerMarkerSeparationStr = tk.StringVar()
        self.arucoGridMarkerMarkerSeparationStr.set("0.02")
        self.arucoGridMarkerFirstMarkerStr = tk.StringVar()
        self.arucoGridMarkerFirstMarkerStr.set("0")
        self.arucoGridMarkerBorderBitsStr = tk.StringVar()
        self.arucoGridMarkerBorderBitsStr.set("1")

        self.arucoGridMarkerDictionaryMenue = tk.OptionMenu(self.arucoGridMarkerUIFrame, self.arucoGridMarkerDictionaryStr, "DICT_ARUCO_ORIGINAL", command = self.OnSelectArucoGridMarkerDictionary)
        self.arucoGridMarkerDictionaryMenue.grid(row=1, column=0, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame, textvariable=self.arucoGridMarkerMarkersXStr).grid(row=1, column=1, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame, textvariable=self.arucoGridMarkerMarkersYStr).grid(row=1, column=2, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame, textvariable=self.arucoGridMarkerMarkerLengthStr).grid(row=1, column=3, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame, textvariable=self.arucoGridMarkerMarkerSeparationStr).grid(row=1, column=4, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame, textvariable=self.arucoGridMarkerFirstMarkerStr).grid(row=1, column=5, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame, textvariable=self.arucoGridMarkerBorderBitsStr).grid(row=1, column=6, sticky = tk.NSEW)

        tk.Button(self.arucoGridMarkerUIFrame2, text = "Preview", command = self.OnPreviewArucoGridMarker).grid(row=1, column=0, sticky = tk.NSEW)
        tk.Button(self.arucoGridMarkerUIFrame2, text = "Save", command = self.OnSaveArucoGridMarker).grid(row=1, column=1, sticky = tk.NSEW)

        tk.Label(self.arucoGridMarkerUIFrame2, text="Save options:").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="(set 0 as disable)").grid(row=1, column=2, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="subSizeX").grid(row=0, column=3, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="subSizeY").grid(row=0, column=4, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="Divide to chunks, chunk sizeX").grid(row=2, column=3, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="Divide to chunks, chunk sizeY").grid(row=2, column=4, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="pageBorderX (Unit: Meter)").grid(row=0, column=5, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="pageBorderY (Unit: Meter)").grid(row=0, column=6, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="Border or page").grid(row=2, column=5, sticky = tk.NSEW)
        tk.Label(self.arucoGridMarkerUIFrame2, text="Border or page").grid(row=2, column=6, sticky = tk.NSEW)

        self.arucoGridMarkerSaveSubSizeXStr = tk.StringVar()
        self.arucoGridMarkerSaveSubSizeXStr.set("0")
        self.arucoGridMarkerSaveSubSizeYStr = tk.StringVar()
        self.arucoGridMarkerSaveSubSizeYStr.set("0")
        self.arucoGridMarkerSavePageBorderXStr = tk.StringVar()
        self.arucoGridMarkerSavePageBorderXStr.set("0.02")
        self.arucoGridMarkerSavePageBorderYStr = tk.StringVar()
        self.arucoGridMarkerSavePageBorderYStr.set("0.02")

        tk.Entry(self.arucoGridMarkerUIFrame2, textvariable=self.arucoGridMarkerSaveSubSizeXStr).grid(row=1, column=3, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame2, textvariable=self.arucoGridMarkerSaveSubSizeYStr).grid(row=1, column=4, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame2, textvariable=self.arucoGridMarkerSavePageBorderXStr).grid(row=1, column=5, sticky = tk.NSEW)
        tk.Entry(self.arucoGridMarkerUIFrame2, textvariable=self.arucoGridMarkerSavePageBorderYStr).grid(row=1, column=6, sticky = tk.NSEW)

        self.arucoGridMarkerDictionaryMenue['menu'].delete(0, 'end')
        for dictName in self.dictList:
            self.arucoGridMarkerDictionaryMenue['menu'].add_command(label=dictName, command=tk._setit(self.arucoGridMarkerDictionaryStr, dictName, self.OnSelectArucoGridMarkerDictionary))

        self.OnSelectArucoGridMarkerDictionary("DICT_ARUCO_ORIGINAL")

    def OnSelectArucoMarkerDictionary(self, pDictName):
        self.arucoMarkerDictionaryStr.set(pDictName)

    def OnPreviewOrSaveArucoMarker(self, askSave = False):
        try:
            markerID = int(self.arucoMarkerMarkerIDStr.get())
            markerLength = float(self.arucoMarkerMarkerLengthStr.get())
            borderBits = int(self.arucoMarkerBorderBitsStr.get())
            dictionary = self.arucoMarkerDictionaryStr.get()
            pageBorderX = float(self.arucoMarkerSavePageBorderXStr.get())
            pageBorderY = float(self.arucoMarkerSavePageBorderYStr.get())
        except ValueError as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Enter invalid parameters")
            return
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Fail to get parameters")
            return

        # Preview
        try:
            dpi=self.VisDPI(((markerLength  + pageBorderY * 2) * MarkerPrinter.ptPerMeter, (markerLength + pageBorderX * 2) * MarkerPrinter.ptPerMeter))
            tkImage = PIL.ImageTk.PhotoImage(image = MarkerPrinter.PreviewArucoMarkerImage(dictionary, markerID, markerLength, borderBits=borderBits, pageBorder = (pageBorderX, pageBorderY), dpi=dpi))
            self.arucoMarkerImageLabel.imgtk = tkImage
            self.arucoMarkerImageLabel.config(image=tkImage)
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "create marker failed")
            return

        # Save
        if(askSave):
            MarkerPrinterGUI.__SaveMarker(MarkerPrinter.GenArucoMarkerImage, \
                dictionary, markerID, markerLength, borderBits=borderBits, pageBorder = (pageBorderX, pageBorderY))

    def OnPreviewArucoMarker(self):
        self.OnPreviewOrSaveArucoMarker(askSave = False)

    def OnSaveArucoMarker(self):
        self.OnPreviewOrSaveArucoMarker(askSave = True)

    def InitArucoMarkerTab(self):
        self.arucoMarkerUIFrame = ttk.Frame(self.arucoMarkerTab)
        self.arucoMarkerImageTab = ttk.Frame(self.arucoMarkerTab)
        self.arucoMarkerUIFrame2 = ttk.Frame(self.arucoMarkerTab)

        self.arucoMarkerUIFrame.grid(row=0, column=0, sticky = tk.NSEW)
        self.arucoMarkerImageTab.grid(row=1, column=0, sticky = tk.NSEW)
        self.arucoMarkerUIFrame2.grid(row=2, column=0, sticky = tk.NSEW)

        self.arucoMarkerImageLabel = tk.Label(self.arucoMarkerImageTab)
        self.arucoMarkerImageLabel.grid(row=0, column=0, sticky = tk.NSEW)

        tk.Label(self.arucoMarkerUIFrame, text="dictionary").grid(row=0, column=0, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame, text="markerID").grid(row=0, column=1, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame, text="markerLength (Unit: Meter)").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame, text="borderBits").grid(row=0, column=3, sticky = tk.NSEW)

        self.arucoMarkerDictionaryStr = tk.StringVar()
        self.arucoMarkerMarkerIDStr = tk.StringVar()
        self.arucoMarkerMarkerIDStr.set("0")
        self.arucoMarkerMarkerLengthStr = tk.StringVar()
        self.arucoMarkerMarkerLengthStr.set("0.07")
        self.arucoMarkerBorderBitsStr = tk.StringVar()
        self.arucoMarkerBorderBitsStr.set("1")

        self.arucoMarkerDictionaryMenue = tk.OptionMenu(self.arucoMarkerUIFrame, self.arucoMarkerDictionaryStr, "DICT_ARUCO_ORIGINAL", command = self.OnSelectArucoMarkerDictionary)
        self.arucoMarkerDictionaryMenue.grid(row=1, column=0, sticky = tk.NSEW)
        tk.Entry(self.arucoMarkerUIFrame, textvariable=self.arucoMarkerMarkerIDStr).grid(row=1, column=1, sticky = tk.NSEW)
        tk.Entry(self.arucoMarkerUIFrame, textvariable=self.arucoMarkerMarkerLengthStr).grid(row=1, column=2, sticky = tk.NSEW)
        tk.Entry(self.arucoMarkerUIFrame, textvariable=self.arucoMarkerBorderBitsStr).grid(row=1, column=3, sticky = tk.NSEW)

        tk.Button(self.arucoMarkerUIFrame2, text = "Preview", command = self.OnPreviewArucoMarker).grid(row=0, column=0, sticky = tk.NSEW)
        tk.Button(self.arucoMarkerUIFrame2, text = "Save", command = self.OnSaveArucoMarker).grid(row=0, column=1, sticky = tk.NSEW)

        tk.Label(self.arucoMarkerUIFrame2, text="Save options:").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame2, text="(set 0 as disable)").grid(row=1, column=2, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame2, text="pageBorderX (Unit: Meter)").grid(row=0, column=3, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame2, text="pageBorderY (Unit: Meter)").grid(row=0, column=4, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame2, text="Border or page").grid(row=2, column=3, sticky = tk.NSEW)
        tk.Label(self.arucoMarkerUIFrame2, text="Border or page").grid(row=2, column=4, sticky = tk.NSEW)

        self.arucoMarkerSavePageBorderXStr = tk.StringVar()
        self.arucoMarkerSavePageBorderXStr.set("0.02")
        self.arucoMarkerSavePageBorderYStr = tk.StringVar()
        self.arucoMarkerSavePageBorderYStr.set("0.02")

        tk.Entry(self.arucoMarkerUIFrame2, textvariable=self.arucoMarkerSavePageBorderXStr).grid(row=1, column=3, sticky = tk.NSEW)
        tk.Entry(self.arucoMarkerUIFrame2, textvariable=self.arucoMarkerSavePageBorderYStr).grid(row=1, column=4, sticky = tk.NSEW)

        self.arucoMarkerDictionaryMenue['menu'].delete(0, 'end')
        for dictName in self.dictList:
            self.arucoMarkerDictionaryMenue['menu'].add_command(label=dictName, command=tk._setit(self.arucoMarkerDictionaryStr, dictName, self.OnSelectArucoMarkerDictionary))

        self.OnSelectArucoMarkerDictionary("DICT_ARUCO_ORIGINAL")

    def OnPreviewOrSaveChessMarker(self, askSave = False):
        try:
            sizeX = int(self.chessMarkerChessboardSizeXStr.get())
            sizeY = int(self.chessMarkerChessboardSizeYStr.get())
            squareLength = float(self.chessMarkerSquareLengthStr.get())
            subSizeX = int(self.chessMarkerSaveSubSizeXStr.get())
            subSizeY = int(self.chessMarkerSaveSubSizeYStr.get())
            pageBorderX = float(self.chessMarkerSavePageBorderXStr.get())
            pageBorderY = float(self.chessMarkerSavePageBorderYStr.get())
        except ValueError as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Enter invalid parameters")
            return
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "Fail to get parameters")
            return

        # Preview
        try:
            dpi=self.VisDPI(((sizeY * squareLength + pageBorderY * 2) * MarkerPrinter.ptPerMeter, (sizeX * squareLength + pageBorderX * 2) * MarkerPrinter.ptPerMeter))
            tkImage = PIL.ImageTk.PhotoImage(image = MarkerPrinter.PreviewChessMarkerImage((sizeX, sizeY), squareLength, pageBorder = (pageBorderX, pageBorderY), dpi=dpi))
            self.chessMarkerImageLabel.imgtk = tkImage
            self.chessMarkerImageLabel.config(image=tkImage)
        except Exception as e:
            warnings.warn(str(e))
            messagebox.showinfo("Error", "create marker failed")
            return

        # Save
        if(askSave):
            MarkerPrinterGUI.__SaveMarker(MarkerPrinter.GenChessMarkerImage, \
                (sizeX, sizeY), squareLength, subSize = (subSizeX, subSizeY), pageBorder = (pageBorderX, pageBorderY))

    def OnPreviewChessMarker(self):
        self.OnPreviewOrSaveChessMarker(askSave = False)

    def OnSaveChessMarker(self):
        self.OnPreviewOrSaveChessMarker(askSave = True)

    def InitChessMarkerTab(self):
        self.chessMarkerUIFrame = ttk.Frame(self.chessMarkerTab)
        self.chessMarkerImageTab = ttk.Frame(self.chessMarkerTab)
        self.chessMarkerUIFrame2 = ttk.Frame(self.chessMarkerTab)

        self.chessMarkerUIFrame.grid(row=0, column=0, sticky = tk.NSEW)
        self.chessMarkerImageTab.grid(row=1, column=0, sticky = tk.NSEW)
        self.chessMarkerUIFrame2.grid(row=2, column=0, sticky = tk.NSEW)

        self.chessMarkerImageLabel = tk.Label(self.chessMarkerImageTab)
        self.chessMarkerImageLabel.grid(row=0, column=0, sticky = tk.NSEW)

        tk.Label(self.chessMarkerUIFrame, text="chessboardSizeX").grid(row=0, column=0, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame, text="chessboardSizeY").grid(row=0, column=1, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame, text="squareLength (Unit: Meter)").grid(row=0, column=2, sticky = tk.NSEW)

        self.chessMarkerChessboardSizeXStr = tk.StringVar()
        self.chessMarkerChessboardSizeXStr.set("16")
        self.chessMarkerChessboardSizeYStr = tk.StringVar()
        self.chessMarkerChessboardSizeYStr.set("9")
        self.chessMarkerSquareLengthStr = tk.StringVar()
        self.chessMarkerSquareLengthStr.set("0.09")

        tk.Entry(self.chessMarkerUIFrame, textvariable=self.chessMarkerChessboardSizeXStr).grid(row=1, column=0, sticky = tk.NSEW)
        tk.Entry(self.chessMarkerUIFrame, textvariable=self.chessMarkerChessboardSizeYStr).grid(row=1, column=1, sticky = tk.NSEW)
        tk.Entry(self.chessMarkerUIFrame, textvariable=self.chessMarkerSquareLengthStr).grid(row=1, column=2, sticky = tk.NSEW)

        tk.Button(self.chessMarkerUIFrame2, text = "Preview", command = self.OnPreviewChessMarker).grid(row=1, column=0, sticky = tk.NSEW)
        tk.Button(self.chessMarkerUIFrame2, text = "Save", command = self.OnSaveChessMarker).grid(row=1, column=1, sticky = tk.NSEW)

        tk.Label(self.chessMarkerUIFrame2, text="Save options:").grid(row=0, column=2, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="(set 0 as disable)").grid(row=1, column=2, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="subSizeX").grid(row=0, column=3, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="subSizeY").grid(row=0, column=4, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="Divide to chunks, chunk sizeX").grid(row=2, column=3, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="Divide to chunks, chunk sizeY").grid(row=2, column=4, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="pageBorderX (Unit: Meter)").grid(row=0, column=5, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="pageBorderY (Unit: Meter)").grid(row=0, column=6, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="Border or page").grid(row=2, column=5, sticky = tk.NSEW)
        tk.Label(self.chessMarkerUIFrame2, text="Border or page").grid(row=2, column=6, sticky = tk.NSEW)

        self.chessMarkerSaveSubSizeXStr = tk.StringVar()
        self.chessMarkerSaveSubSizeXStr.set("0")
        self.chessMarkerSaveSubSizeYStr = tk.StringVar()
        self.chessMarkerSaveSubSizeYStr.set("0")
        self.chessMarkerSavePageBorderXStr = tk.StringVar()
        self.chessMarkerSavePageBorderXStr.set("0.02")
        self.chessMarkerSavePageBorderYStr = tk.StringVar()
        self.chessMarkerSavePageBorderYStr.set("0.02")

        tk.Entry(self.chessMarkerUIFrame2, textvariable=self.chessMarkerSaveSubSizeXStr).grid(row=1, column=3, sticky = tk.NSEW)
        tk.Entry(self.chessMarkerUIFrame2, textvariable=self.chessMarkerSaveSubSizeYStr).grid(row=1, column=4, sticky = tk.NSEW)
        tk.Entry(self.chessMarkerUIFrame2, textvariable=self.chessMarkerSavePageBorderXStr).grid(row=1, column=5, sticky = tk.NSEW)
        tk.Entry(self.chessMarkerUIFrame2, textvariable=self.chessMarkerSavePageBorderYStr).grid(row=1, column=6, sticky = tk.NSEW)

    def Update(self):
        time.sleep(0)
        self.window.after(self.delay, self.Update)

    def __init__(self, pDelay=15, pDisplayShape=(int(400), int(1200))):
        self.delay = pDelay
        self.displayShape = pDisplayShape

        self.dictList = MarkerPrinter.arucoDictBytesList.keys()

        # GUI
        self.window = tk.Tk()
        self.notebook = ttk.Notebook(self.window)
        self.notebook.grid(row=0, column=0, sticky = tk.NSEW)

        self.window.title("MarkerPrinterGUI")
        self.window.config(cursor="arrow")
        self.window.protocol("WM_DELETE_WINDOW", self.OnCloseWindow)

        # Menues
        self.menu = tk.Menu(self.window)
        self.helpMenu = tk.Menu(self.menu, tearoff=0)
        self.menu.add_cascade(label="Help", menu=self.helpMenu)
        self.helpMenu.add_command(label="Github", command=self.OnShowingHelpGithub)
        self.helpMenu.add_command(label="DEBUG_LINE_MODE", command=self.On_DEBUG_LINE_MODE)
        self.helpMenu.add_command(label="DEBUG_BLOCK_MODE", command=self.On_DEBUG_BLOCK_MODE)
        self.helpMenu.add_command(label="CLOSE_DEBUG_MODE", command=self.On_CLOSE_DEBUG_MODE)
        self.window.config(menu=self.menu)

        self.charucoMarkerTab = ttk.Frame(self.notebook)
        self.arucoMarkerTab = ttk.Frame(self.notebook)
        self.arucoGridMarkerTab = ttk.Frame(self.notebook)
        self.chessMarkerTab = ttk.Frame(self.notebook)

        self.notebook.add(self.charucoMarkerTab, text='ChArUco Marker')
        self.notebook.add(self.arucoMarkerTab, text='ArUco Marker')
        self.notebook.add(self.arucoGridMarkerTab, text='ArUcoGrid Marker')
        self.notebook.add(self.chessMarkerTab, text='Chessboard Marker')

        self.InitCharucoMarkerTab()
        self.InitArucoMarkerTab()
        self.InitArucoGridMarkerTab()
        self.InitChessMarkerTab()

        self.Update()
        self.window.mainloop()

    def On_DEBUG_LINE_MODE(self):
        messagebox.showinfo("Note", "You enabled the debug mode: \"LINE\"")
        MarkerPrinter.debugMode = "LINE"

    def On_DEBUG_BLOCK_MODE(self):
        messagebox.showinfo("Note", "You enabled the debug mode: \"BLOCK\"")
        MarkerPrinter.debugMode = "BLOCK"

    def On_CLOSE_DEBUG_MODE(self):
        messagebox.showinfo("Note", "You closed the debug mode")
        MarkerPrinter.debugMode = None

if __name__ == '__main__':
    MarkerPrinterGUI()

3.1代码二

# MarkerPrinter.py 
#!/usr/bin/env python3

# SPDX-License-Identifier: BSD-3-Clause
#
# Copyright (c) 2019, Josh Chien. All rights reserved.

from argparse import ArgumentParser
import numpy as np
from PIL import Image
import io
import warnings
import os
import cairo
from cairosvg import svg2png
import math
import tempfile

def SaveArucoDictBytesList(filePath = "arucoDictBytesList.npz"):
    import numpy as np

    # cv2 is optional dependency
    try:
        import cv2
        from cv2 import aruco

        # Name, Flag
        dictInfo = \
        [
            ("DICT_4X4_1000", aruco.DICT_4X4_1000),
            ("DICT_5X5_1000", aruco.DICT_5X5_1000),
            ("DICT_6X6_1000", aruco.DICT_6X6_1000),
            ("DICT_7X7_1000", aruco.DICT_7X7_1000),
            ("DICT_ARUCO_ORIGINAL", aruco.DICT_ARUCO_ORIGINAL),
            ("DICT_APRILTAG_16h5", aruco.DICT_APRILTAG_16h5),
            ("DICT_APRILTAG_25h9", aruco.DICT_APRILTAG_25h9),
            ("DICT_APRILTAG_36h10", aruco.DICT_APRILTAG_36h10),
            ("DICT_APRILTAG_36h11", aruco.DICT_APRILTAG_36h11),
        ]

        arucoDictBytesList = {}
        for name, flag in dictInfo:
            arucoDict = aruco.Dictionary_get(flag)
            arucoDictBytesList[name] = arucoDict.bytesList

        np.savez_compressed(filePath, **arucoDictBytesList)
        return arucoDictBytesList

    except Exception as e:
        warnings.warn(str(e))
        return None

    return None

class MarkerPrinter:

    debugMode = None # "LINE" "BLOCK"

    # Static Vars
    # SVG https://oreillymedia.github.io/Using_SVG/guide/units.html
    # for PDF and SVG, 1 pixel = 1/72 inch, 1 cm = 1/2.54 inch, 1pixl = 2.54/72 cm, 1cm = 72/2.54 pixels
    ptPerMeter = 72 / 2.54 * 100

    surface = {
            ".SVG": cairo.SVGSurface,
            ".PDF": cairo.PDFSurface,
            ".PS": cairo.PSSurface }

    if (os.path.isfile("arucoDictBytesList.npz")):
        arucoDictBytesList = np.load("arucoDictBytesList.npz")
    else:
        warnings.warn("Missing build-in arucoDictBytesList.npz, generate it again")
        arucoDictBytesList = SaveArucoDictBytesList(filePath = "arucoDictBytesList.npz")

    arucoDictMarkerSize = \
        {
            "DICT_4X4_1000": 4,
            "DICT_5X5_1000": 5,
            "DICT_6X6_1000": 6,
            "DICT_7X7_1000": 7,
            "DICT_ARUCO_ORIGINAL": 5,
            "DICT_APRILTAG_16h5": 4,
            "DICT_APRILTAG_25h9": 5,
            "DICT_APRILTAG_36h10": 6,
            "DICT_APRILTAG_36h11": 6,
        }

    def ArucoBits(dictionary, markerID):
        bytesList = MarkerPrinter.arucoDictBytesList[dictionary][markerID].ravel()
        markerSize = MarkerPrinter.arucoDictMarkerSize[dictionary]

        arucoBits = np.zeros(shape = (markerSize, markerSize), dtype = bool)
        base2List = np.array( [128, 64, 32, 16, 8, 4, 2, 1], dtype = np.uint8)
        currentByteIdx = 0
        currentByte = bytesList[currentByteIdx]
        currentBit = 0
        for row in range(markerSize):
            for col in range(markerSize):
                if(currentByte >= base2List[currentBit]):
                    arucoBits[row, col] = True
                    currentByte -= base2List[currentBit]
                currentBit = currentBit + 1
                if(currentBit == 8):
                    currentByteIdx = currentByteIdx + 1
                    currentByte = bytesList[currentByteIdx]
                    if(8 * (currentByteIdx + 1) > arucoBits.size):
                        currentBit = 8 * (currentByteIdx + 1) - arucoBits.size
                    else:
                        currentBit = 0;
        return arucoBits

    def __DrawBlock(context,
        dictionary = None, markerLength = None, borderBits = 1,
        chessboardSize = (1, 1), squareLength = None, firstMarkerID = 0,
        blockX = 0, blockY = 0, originX = 0, originY = 0, pageBorderX = 0, pageBorderY = 0,
        mode = "CHESS" ):

        if(squareLength is None):
            squareLength = markerLength

        if(markerLength is None):
            markerLength = squareLength

        if((squareLength is None) or (markerLength is None)):
            raise ValueError("lenght is None")

        dawMarkerBlock = False
        if ((mode == "ARUCO") or (mode == "ARUCOGRID")):
            dawMarkerBlock = True
        elif(chessboardSize[1] % 2 == 0):
            dawMarkerBlock = (( blockX % 2 == 0 ) == ( blockY % 2 == 0 ))
        else:
            dawMarkerBlock = (( blockX % 2 == 0 ) != ( blockY % 2 == 0 ))

        if(dawMarkerBlock):
            if (mode != "CHESS"):
                if(dictionary is None):
                    raise ValueError("dictionary is None")

                if (mode == "CHARUCO"):
                    originX = (blockX - originX) * squareLength + (squareLength - markerLength)*0.5 + pageBorderX
                    originY = (blockY - originY) * squareLength + (squareLength - markerLength)*0.5 + pageBorderY
                else:
                    originX = (blockX - originX) * squareLength + pageBorderX
                    originY = (blockY - originY) * squareLength + pageBorderY

                context.set_source_rgba(0.0, 0.0, 0.0, 1.0)
                context.rectangle(originX, originY, markerLength, markerLength)
                context.fill()

                # Generate marker
                if  (mode == "CHARUCO"):
                    markerID = firstMarkerID + (blockY * chessboardSize[0] + blockX) // 2
                elif (mode == "ARUCO"):
                    markerID = firstMarkerID
                elif (mode == "ARUCOGRID"):
                    markerID = firstMarkerID + (blockY * chessboardSize[0] + blockX)

                marker = MarkerPrinter.ArucoBits(dictionary, markerID)
                markerSize = marker.shape[0]
                unitLength = markerLength / (float)(markerSize + borderBits * 2)

                markerBitMap = np.zeros(shape = (markerSize+borderBits*2, markerSize+borderBits*2), dtype = bool)
                markerBitMap[borderBits:-borderBits,borderBits:-borderBits] = marker
                markerBitMap = np.swapaxes(markerBitMap, 0, 1)

                # Compute edges
                hEdges = np.zeros(shape = (markerSize+1,markerSize+1), dtype = bool)
                vEdges = np.zeros(shape = (markerSize+1,markerSize+1), dtype = bool)

                for mx in range(markerSize):
                    for my in range(markerSize+1):
                        if ( markerBitMap[mx + borderBits, my + borderBits - 1] ^ markerBitMap[mx + borderBits, my + borderBits]):
                            hEdges[mx, my] = True

                for mx in range(markerSize+1):
                    for my in range(markerSize):
                        if ( markerBitMap[mx + borderBits - 1, my + borderBits] ^ markerBitMap[mx + borderBits, my + borderBits]):
                            vEdges[mx, my] = True

                # Use for debug, check edge or position is correct or not
                if(MarkerPrinter.debugMode is not None):
                    if(MarkerPrinter.debugMode.upper() == "LINE"):
                        context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                        context.set_line_width(unitLength * 0.1)
                        for mx in range(markerSize+1):
                            for my in range(markerSize+1):
                                if(hEdges[mx, my]):
                                    context.move_to(originX + unitLength * (mx + borderBits    ), originY + unitLength * (my + borderBits    ))
                                    context.line_to(originX + unitLength * (mx + borderBits + 1), originY + unitLength * (my + borderBits    ))
                                    context.stroke()
                                if(vEdges[mx, my]):
                                    context.move_to(originX + unitLength * (mx + borderBits    ), originY + unitLength * (my + borderBits    ))
                                    context.line_to(originX + unitLength * (mx + borderBits    ), originY + unitLength * (my + borderBits + 1))
                                    context.stroke()

                    elif(MarkerPrinter.debugMode.upper() == "BLOCK"):
                        context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                        for mx in range(markerSize):
                            for my in range(markerSize):
                                if(markerBitMap[mx + borderBits, my + borderBits]):
                                    context.rectangle(
                                        originX + unitLength * (mx + borderBits),
                                        originY + unitLength * (my + borderBits),
                                        unitLength, unitLength)
                                    context.fill()

                else:
                    while(True):
                        found = False

                        # Find start position
                        sx = 0
                        sy = 0
                        for my in range(markerSize):
                            for mx in range(markerSize):
                                if(hEdges[mx, my]):
                                    found = True
                                    sx = mx
                                    sy = my
                                    if(markerBitMap[sx + borderBits, sy + borderBits - 1]):
                                        context.set_source_rgba(0.0, 0.0, 0.0, 1.0)
                                    else:
                                        context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                                    break
                            if(found):
                                break

                        context.move_to (originX + unitLength * (sx + borderBits), originY + unitLength * (sy + borderBits))

                        # Use wall follower maze solving algorithm to draw white part
                        cx = sx
                        cy = sy
                        cd = 3 # 0 right, 1 down, 2 left, 3 up
                        while(True):
                            nd = (cd + 1)%4
                            moved = False
                            if(nd == 0):
                                if(hEdges[cx, cy]):
                                    hEdges[cx, cy] = False
                                    cx = cx + 1
                                    moved = True
                            elif(nd == 1):
                                if(vEdges[cx, cy]):
                                    vEdges[cx, cy] = False
                                    cy = cy + 1
                                    moved = True
                            elif(nd == 2):
                                if(hEdges[cx - 1, cy]):
                                    hEdges[cx - 1, cy] = False
                                    cx = cx - 1
                                    moved = True
                            elif(nd == 3):
                                if(vEdges[cx, cy - 1]):
                                    vEdges[cx, cy - 1] = False
                                    cy = cy - 1
                                    moved = True

                            if((cx == sx) and (cy == sy)):
                                context.close_path ()
                                break
                            else:
                                if(moved):
                                    context.line_to(originX + unitLength * (cx + borderBits), originY + unitLength * (cy + borderBits))
                                cd = nd

                        if (found):
                            context.fill()
                        else:
                            break

        else:
            originX = (blockX - originX) * squareLength + pageBorderX
            originY = (blockY - originY) * squareLength + pageBorderY
            context.set_source_rgba(0.0, 0.0, 0.0, 1.0)
            context.rectangle(originX, originY, squareLength, squareLength)
            context.fill()

    def __CheckChessMarkerImage(chessboardSize, squareLength, subSize=None, pageBorder=(0,0)):
        if(len(chessboardSize) != 2):
            raise ValueError("len(chessboardSize) != 2")
        else:
            sizeX, sizeY = chessboardSize

        if(len(pageBorder) != 2):
            raise ValueError("len(pageBorder) != 2")
        else:
            pageBorderX, pageBorderY = pageBorder

        if(sizeX <= 1):
            raise ValueError("sizeX <= 1")

        if(sizeY <= 1):
            raise ValueError("sizeY <= 1")

        if(squareLength <= 0):
            raise ValueError("squareLength <= 0")

        if(pageBorderX < 0):
            raise ValueError("pageBorderX < 0")

        if(pageBorderY < 0):
            raise ValueError("pageBorderY < 0")

        if(subSize is not None):
            subSizeX, subSizeY = subSize

            if(subSizeX < 0):
                raise ValueError("subSizeX < 0")

            if(subSizeY < 0):
                raise ValueError("subSizeY < 0")

    def PreviewChessMarkerImage(chessboardSize, squareLength, pageBorder=(0, 0), dpi=96):
        MarkerPrinter.__CheckChessMarkerImage(chessboardSize, squareLength, pageBorder=pageBorder)

        squareLength = squareLength * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        prevImage = None
        with tempfile.TemporaryDirectory() as tmpdirname:
            with MarkerPrinter.surface[".SVG"] (
                os.path.join(tmpdirname, "tempSVG.svg"),
                chessboardSize[0] * squareLength + pageBorder[0] * 2,
                chessboardSize[1] * squareLength + pageBorder[1] * 2) as surface:
                context = cairo.Context(surface)

                context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                context.rectangle(0, 0,
                    chessboardSize[0] * squareLength + pageBorder[0] * 2,
                    chessboardSize[1] * squareLength + pageBorder[1] * 2)
                context.fill()

                context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                context.rectangle(pageBorder[0], pageBorder[1],
                    chessboardSize[0] * squareLength,
                    chessboardSize[1] * squareLength)
                context.fill()

                for bx in range(chessboardSize[0]):
                    for by in range(chessboardSize[1]):
                        MarkerPrinter.__DrawBlock(
                            context = context,
                            chessboardSize = chessboardSize,
                            squareLength = squareLength,
                            blockX = bx,
                            blockY = by,
                            pageBorderX = pageBorder[0],
                            pageBorderY = pageBorder[1],
                            mode = "CHESS")

            with open(os.path.join(tmpdirname, "tempSVG.svg")) as file:
                prevImage = Image.open(io.BytesIO(svg2png(bytestring=file.read(), dpi=dpi)))

        return prevImage

    def GenChessMarkerImage(filePath, chessboardSize, squareLength, subSize=None, pageBorder=(0, 0)):
        MarkerPrinter.__CheckChessMarkerImage(chessboardSize, squareLength, subSize=subSize, pageBorder=pageBorder)

        squareLength = squareLength * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        # Check
        path, nameExt = os.path.split(filePath)
        name, ext = os.path.splitext(nameExt)

        if(len(path) > 0):
            if not(os.path.isdir(path)):
                os.makedirs(path)

        if((ext.upper() != ".SVG") and (ext.upper() != ".PS") and (ext.upper() != ".PDF")):
            raise ValueError("file extention is not supported, should be: svg, ps, pdf")

        # Draw
        with MarkerPrinter.surface[ext.upper()] (
            filePath,
            chessboardSize[0] * squareLength + pageBorder[0] * 2,
            chessboardSize[1] * squareLength + pageBorder[1] * 2) as surface:
            context = cairo.Context(surface)

            context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
            context.rectangle(0, 0,
                chessboardSize[0] * squareLength + pageBorder[0] * 2,
                chessboardSize[1] * squareLength + pageBorder[1] * 2)
            context.fill()

            context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
            context.rectangle(pageBorder[0], pageBorder[1],
                chessboardSize[0] * squareLength,
                chessboardSize[1] * squareLength)
            context.fill()

            for bx in range(chessboardSize[0]):
                for by in range(chessboardSize[1]):
                    MarkerPrinter.__DrawBlock(
                        context = context,
                        chessboardSize = chessboardSize,
                        squareLength = squareLength,
                        blockX = bx,
                        blockY = by,
                        pageBorderX = pageBorder[0],
                        pageBorderY = pageBorder[1],
                        mode = "CHESS" )

        if(subSize is not None):
            subDivide = (\
                chessboardSize[0] // subSize[0] + int(chessboardSize[0] % subSize[0] > 0),
                chessboardSize[1] // subSize[1] + int(chessboardSize[1] % subSize[1] > 0))

            subChessboardBlockX = np.clip ( np.arange(0, subSize[0] * subDivide[0] + 1, subSize[0]), 0, chessboardSize[0])
            subChessboardBlockY = np.clip ( np.arange(0, subSize[1] * subDivide[1] + 1, subSize[1]), 0, chessboardSize[1])

            subChessboardSliceX = subChessboardBlockX.astype(np.float) * squareLength
            subChessboardSliceY = subChessboardBlockY.astype(np.float) * squareLength

            for subXID in range(subDivide[0]):
                for subYID in range(subDivide[1]):
                    subName = name + \
                        "_X" + str(subChessboardBlockX[subXID]) + "_" + str(subChessboardBlockX[subXID+1]) + \
                        "_Y" + str(subChessboardBlockY[subYID]) + "_" + str(subChessboardBlockY[subYID+1])

                    with MarkerPrinter.surface[ext.upper()](
                        os.path.join(path, subName + ext),
                        subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID] + pageBorder[0] * 2,
                        subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID] + pageBorder[1] * 2) as surface:
                        context = cairo.Context(surface)

                        context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                        context.rectangle(0, 0,
                            subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID] + pageBorder[0] * 2,
                            subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID] + pageBorder[1] * 2)
                        context.fill()

                        context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                        context.rectangle(pageBorder[0], pageBorder[1],
                            subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID],
                            subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID])
                        context.fill()

                        for bx in range(subChessboardBlockX[subXID+1] - subChessboardBlockX[subXID]):
                            for by in range(subChessboardBlockY[subYID+1] - subChessboardBlockY[subYID]):
                                MarkerPrinter.__DrawBlock(
                                    context = context,
                                    chessboardSize = chessboardSize,
                                    squareLength = squareLength,
                                    blockX = subChessboardBlockX[subXID] + bx,
                                    blockY = subChessboardBlockY[subYID] + by,
                                    originX = subChessboardBlockX[subXID],
                                    originY = subChessboardBlockY[subYID],
                                    pageBorderX = pageBorder[0],
                                    pageBorderY = pageBorder[1],
                                    mode = "CHESS" )


    def __CheckArucoMarkerImage(dictionary, markerID, markerLength, borderBits=1, pageBorder=(0, 0)):
        if(len(pageBorder) != 2):
            raise ValueError("len(pageBorder) != 2")
        else:
            pageBorderX, pageBorderY = pageBorder

        if not (dictionary in MarkerPrinter.arucoDictBytesList):
            raise ValueError("dictionary is not support")

        if(MarkerPrinter.arucoDictBytesList[dictionary].shape[0] <= markerID ):
            raise ValueError("markerID is not in aruce dictionary")

        if(markerID < 0):
            raise ValueError("markerID < 0")

        if(markerLength <= 0):
            raise ValueError("markerLength <= 0")

        if(borderBits <= 0):
            raise ValueError("borderBits <= 0")

        if(pageBorderX < 0):
            raise ValueError("pageBorderX < 0")

        if(pageBorderY < 0):
            raise ValueError("pageBorderY < 0")

    def PreviewArucoMarkerImage(dictionary, markerID, markerLength, borderBits=1, pageBorder=(0, 0), dpi=96):
        MarkerPrinter.__CheckArucoMarkerImage(dictionary, markerID, markerLength, borderBits=borderBits, pageBorder=pageBorder)

        markerLength = markerLength * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        prevImage = None
        with tempfile.TemporaryDirectory() as tmpdirname:
            with MarkerPrinter.surface[".SVG"] (
                os.path.join(tmpdirname, "tempSVG.svg"),
                markerLength + pageBorder[0] * 2,
                markerLength + pageBorder[1] * 2) as surface:
                context = cairo.Context(surface)

                context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                context.rectangle(0, 0,
                    markerLength + pageBorder[0] * 2,
                    markerLength + pageBorder[1] * 2)
                context.fill()

                context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                context.rectangle(pageBorder[0], pageBorder[1],
                    markerLength,
                    markerLength)
                context.fill()

                MarkerPrinter.__DrawBlock(
                    context = context,
                    dictionary = dictionary,
                    markerLength = markerLength,
                    borderBits = borderBits,
                    firstMarkerID = markerID,
                    pageBorderX = pageBorder[0],
                    pageBorderY = pageBorder[1],
                    mode = "ARUCO")

            with open(os.path.join(tmpdirname, "tempSVG.svg")) as file:
                prevImage = Image.open(io.BytesIO(svg2png(bytestring=file.read(), dpi=dpi)))

        return prevImage

    def GenArucoMarkerImage(filePath, dictionary, markerID, markerLength, borderBits=1, pageBorder=(0, 0)):
        MarkerPrinter.__CheckArucoMarkerImage(dictionary, markerID, markerLength, borderBits=borderBits, pageBorder=pageBorder)

        markerLength = markerLength * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        # Check
        path, nameExt = os.path.split(filePath)
        name, ext = os.path.splitext(nameExt)

        if(len(path) > 0):
            if not(os.path.isdir(path)):
                os.makedirs(path)

        if((ext.upper() != ".SVG") and (ext.upper() != ".PS") and (ext.upper() != ".PDF")):
            raise ValueError("file extention is not supported, should be: svg, ps, pdf")

        # Draw
        with MarkerPrinter.surface[ext.upper()] (
            filePath,
            markerLength + pageBorder[0] * 2,
            markerLength + pageBorder[1] * 2) as surface:
            context = cairo.Context(surface)

            context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
            context.rectangle(0, 0,
                markerLength + pageBorder[0] * 2,
                markerLength + pageBorder[1] * 2)
            context.fill()

            context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
            context.rectangle(pageBorder[0], pageBorder[1],
                markerLength,
                markerLength)
            context.fill()

            MarkerPrinter.__DrawBlock(
                context = context,
                dictionary = dictionary,
                markerLength = markerLength,
                borderBits = borderBits,
                firstMarkerID = markerID,
                pageBorderX = pageBorder[0],
                pageBorderY = pageBorder[1],
                mode = "ARUCO")

    def __CheckCharucoMarkerImage(dictionary, chessboardSize, squareLength, markerLength, borderBits=1, subSize=None, pageBorder=(0, 0)):
        if(len(chessboardSize) != 2):
            raise ValueError("len(chessboardSize) != 2")
        else:
            sizeX, sizeY = chessboardSize

        if(len(pageBorder) != 2):
            raise ValueError("len(pageBorder) != 2")
        else:
            pageBorderX, pageBorderY = pageBorder

        if not (dictionary in MarkerPrinter.arucoDictBytesList):
            raise ValueError("dictionary is not support")

        if(MarkerPrinter.arucoDictBytesList[dictionary].shape[0] < (( sizeX * sizeY ) // 2)):
            raise ValueError("aruce dictionary is not enough for your board size")

        if(sizeX <= 1):
            raise ValueError("sizeX <= 1")

        if(sizeY <= 1):
            raise ValueError("sizeY <= 1")

        if(squareLength <= 0):
            raise ValueError("squareLength <= 0")

        if(markerLength <= 0):
            raise ValueError("markerLength <= 0")

        if(squareLength < markerLength):
            raise ValueError("squareLength < markerLength")

        if(borderBits <= 0):
            raise ValueError("borderBits <= 0")

        if(pageBorderX < 0):
            raise ValueError("pageBorderX < 0")

        if(pageBorderY < 0):
            raise ValueError("pageBorderY < 0")

        if(subSize is not None):
            subSizeX, subSizeY = subSize

            if(subSizeX < 0):
                raise ValueError("subSizeX < 0")

            if(subSizeY < 0):
                raise ValueError("subSizeY < 0")

    def PreviewCharucoMarkerImage(dictionary, chessboardSize, squareLength, markerLength, borderBits=1, pageBorder=(0, 0), dpi=96):
        MarkerPrinter.__CheckCharucoMarkerImage(dictionary, chessboardSize, squareLength, markerLength, borderBits=borderBits, pageBorder=pageBorder)

        squareLength = squareLength * MarkerPrinter.ptPerMeter
        markerLength = markerLength * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        prevImage = None
        with tempfile.TemporaryDirectory() as tmpdirname:
            with MarkerPrinter.surface[".SVG"] (
                os.path.join(tmpdirname, "tempSVG.svg"),
                chessboardSize[0] * squareLength + pageBorder[0] * 2,
                chessboardSize[1] * squareLength + pageBorder[1] * 2) as surface:
                context = cairo.Context(surface)

                context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                context.rectangle(0, 0,
                    chessboardSize[0] * squareLength + pageBorder[0] * 2,
                    chessboardSize[1] * squareLength + pageBorder[1] * 2)
                context.fill()

                context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                context.rectangle(pageBorder[0], pageBorder[1],
                    chessboardSize[0] * squareLength,
                    chessboardSize[1] * squareLength)
                context.fill()

                for bx in range(chessboardSize[0]):
                    for by in range(chessboardSize[1]):
                        MarkerPrinter.__DrawBlock(
                            context = context,
                            dictionary = dictionary,
                            markerLength = markerLength,
                            borderBits = borderBits,
                            chessboardSize = chessboardSize,
                            squareLength = squareLength,
                            blockX = bx,
                            blockY = by,
                            pageBorderX = pageBorder[0],
                            pageBorderY = pageBorder[1],
                            mode = "CHARUCO")

            with open(os.path.join(tmpdirname, "tempSVG.svg")) as file:
                prevImage = Image.open(io.BytesIO(svg2png(bytestring=file.read(), dpi=dpi)))

        return prevImage

    def GenCharucoMarkerImage(filePath, dictionary, chessboardSize, squareLength, markerLength, borderBits=1, subSize=None, pageBorder=(0, 0)):
        MarkerPrinter.__CheckCharucoMarkerImage(dictionary, chessboardSize, squareLength, markerLength, borderBits=borderBits, subSize=subSize, pageBorder=pageBorder)

        squareLength = squareLength * MarkerPrinter.ptPerMeter
        markerLength = markerLength * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        # Check
        path, nameExt = os.path.split(filePath)
        name, ext = os.path.splitext(nameExt)

        if(len(path) > 0):
            if not(os.path.isdir(path)):
                os.makedirs(path)

        if((ext.upper() != ".SVG") and (ext.upper() != ".PS") and (ext.upper() != ".PDF")):
            raise ValueError("file extention is not supported, should be: svg, ps, pdf")

        # Draw
        with MarkerPrinter.surface[ext.upper()] (
            filePath,
            chessboardSize[0] * squareLength + pageBorder[0] * 2,
            chessboardSize[1] * squareLength + pageBorder[1] * 2) as surface:
            context = cairo.Context(surface)

            context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
            context.rectangle(0, 0,
                chessboardSize[0] * squareLength + pageBorder[0] * 2,
                chessboardSize[1] * squareLength + pageBorder[1] * 2)
            context.fill()

            context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
            context.rectangle(pageBorder[0], pageBorder[1],
                chessboardSize[0] * squareLength,
                chessboardSize[1] * squareLength)
            context.fill()

            for bx in range(chessboardSize[0]):
                for by in range(chessboardSize[1]):
                    MarkerPrinter.__DrawBlock(
                        context = context,
                        dictionary = dictionary,
                        markerLength = markerLength,
                        borderBits = borderBits,
                        chessboardSize = chessboardSize,
                        squareLength = squareLength,
                        blockX = bx,
                        blockY = by,
                        pageBorderX = pageBorder[0],
                        pageBorderY = pageBorder[1],
                        mode = "CHARUCO")

        if(subSize is not None):
            subDivide = (\
                chessboardSize[0] // subSize[0] + int(chessboardSize[0] % subSize[0] > 0),
                chessboardSize[1] // subSize[1] + int(chessboardSize[1] % subSize[1] > 0))

            subChessboardBlockX = np.clip ( np.arange(0, subSize[0] * subDivide[0] + 1, subSize[0]), 0, chessboardSize[0])
            subChessboardBlockY = np.clip ( np.arange(0, subSize[1] * subDivide[1] + 1, subSize[1]), 0, chessboardSize[1])

            subChessboardSliceX = subChessboardBlockX.astype(np.float) * squareLength
            subChessboardSliceY = subChessboardBlockY.astype(np.float) * squareLength

            for subXID in range(subDivide[0]):
                for subYID in range(subDivide[1]):
                    subName = name + \
                        "_X" + str(subChessboardBlockX[subXID]) + "_" + str(subChessboardBlockX[subXID+1]) + \
                        "_Y" + str(subChessboardBlockY[subYID]) + "_" + str(subChessboardBlockY[subYID+1])

                    with MarkerPrinter.surface[ext.upper()](
                        os.path.join(path, subName + ext),
                        subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID] + pageBorder[0] * 2,
                        subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID] + pageBorder[1] * 2) as surface:
                        context = cairo.Context(surface)

                        context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                        context.rectangle(0, 0,
                            subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID] + pageBorder[0] * 2,
                            subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID] + pageBorder[1] * 2)
                        context.fill()

                        context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                        context.rectangle(pageBorder[0], pageBorder[1],
                            subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID],
                            subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID])
                        context.fill()

                        for bx in range(subChessboardBlockX[subXID+1] - subChessboardBlockX[subXID]):
                            for by in range(subChessboardBlockY[subYID+1] - subChessboardBlockY[subYID]):
                                MarkerPrinter.__DrawBlock(
                                    context = context,
                                    dictionary = dictionary,
                                    markerLength = markerLength,
                                    borderBits = borderBits,
                                    chessboardSize = chessboardSize,
                                    squareLength = squareLength,
                                    blockX = subChessboardBlockX[subXID] + bx,
                                    blockY = subChessboardBlockY[subYID] + by,
                                    originX = subChessboardBlockX[subXID],
                                    originY = subChessboardBlockY[subYID],
                                    pageBorderX = pageBorder[0],
                                    pageBorderY = pageBorder[1],
                                    mode = "CHARUCO")

    def __CheckArucoGridMarkerImage(dictionary, chessboardSize, markerLength, markerSeparation, firstMarker, borderBits=1, subSize=None, pageBorder=(0, 0)):
        if(len(chessboardSize) != 2):
            raise ValueError("len(chessboardSize) != 2")
        else:
            sizeX, sizeY = chessboardSize

        if(len(pageBorder) != 2):
            raise ValueError("len(pageBorder) != 2")
        else:
            pageBorderX, pageBorderY = pageBorder

        if not (dictionary in MarkerPrinter.arucoDictBytesList):
            raise ValueError("dictionary is not support")

        if(MarkerPrinter.arucoDictBytesList[dictionary].shape[0] < (( sizeX * sizeY ) + firstMarker)):
            raise ValueError("aruce dictionary is not enough for your board size and firstMarker")

        if(sizeX <= 1):
            raise ValueError("sizeX <= 1")

        if(sizeY <= 1):
            raise ValueError("sizeY <= 1")

        if(markerLength <= 0):
            raise ValueError("markerLength <= 0")

        if(markerSeparation <= 0):
            raise ValueError("markerSeparation <= 0")

        if(borderBits <= 0):
            raise ValueError("borderBits <= 0")

        if(pageBorderX < 0):
            raise ValueError("pageBorderX < 0")

        if(pageBorderY < 0):
            raise ValueError("pageBorderY < 0")

        if(subSize is not None):
            subSizeX, subSizeY = subSize

            if(subSizeX < 0):
                raise ValueError("subSizeX < 0")

            if(subSizeY < 0):
                raise ValueError("subSizeY < 0")

    def PreviewArucoGridMarkerImage(dictionary, chessboardSize, markerLength, markerSeparation, firstMarker, borderBits=1, pageBorder=(0, 0), dpi=96):
        MarkerPrinter.__CheckArucoGridMarkerImage(dictionary, chessboardSize, markerLength, markerSeparation, firstMarker, borderBits=borderBits, pageBorder=pageBorder)

        markerLength = markerLength * MarkerPrinter.ptPerMeter
        markerSeparation = markerSeparation * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        prevImage = None
        with tempfile.TemporaryDirectory() as tmpdirname:
            with MarkerPrinter.surface[".SVG"] (
                os.path.join(tmpdirname, "tempSVG.svg"),
                chessboardSize[0] * markerLength + (chessboardSize[0] - 1) * markerSeparation + pageBorder[0] * 2,
                chessboardSize[1] * markerLength + (chessboardSize[1] - 1) * markerSeparation + pageBorder[1] * 2) as surface:
                context = cairo.Context(surface)

                context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                context.rectangle(0, 0,
                    chessboardSize[0] * markerLength + (chessboardSize[0] - 1) * markerSeparation + pageBorder[0] * 2,
                    chessboardSize[1] * markerLength + (chessboardSize[1] - 1) * markerSeparation + pageBorder[1] * 2)
                context.fill()

                context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                context.rectangle(pageBorder[0], pageBorder[1],
                    chessboardSize[0] * markerLength + (chessboardSize[0] - 1) * markerSeparation,
                    chessboardSize[1] * markerLength + (chessboardSize[1] - 1) * markerSeparation)
                context.fill()

                for bx in range(chessboardSize[0]):
                    for by in range(chessboardSize[1]):
                        MarkerPrinter.__DrawBlock(
                            context = context,
                            dictionary = dictionary,
                            markerLength = markerLength,
                            borderBits = borderBits,
                            chessboardSize = chessboardSize,
                            squareLength = markerLength + markerSeparation,
                            firstMarkerID = firstMarker,
                            blockX = bx,
                            blockY = by,
                            pageBorderX = pageBorder[0],
                            pageBorderY = pageBorder[1],
                            mode = "ARUCOGRID")

            with open(os.path.join(tmpdirname, "tempSVG.svg")) as file:
                prevImage = Image.open(io.BytesIO(svg2png(bytestring=file.read(), dpi=dpi)))

        return prevImage

    def GenArucoGridMarkerImage(filePath, dictionary, chessboardSize, markerLength, markerSeparation, firstMarker, borderBits=1, subSize=None, pageBorder=(0, 0)):
        MarkerPrinter.__CheckArucoGridMarkerImage(dictionary, chessboardSize, markerLength, markerSeparation, firstMarker, borderBits=borderBits, subSize=subSize, pageBorder=pageBorder)

        markerLength = markerLength * MarkerPrinter.ptPerMeter
        markerSeparation = markerSeparation * MarkerPrinter.ptPerMeter
        pageBorder = (pageBorder[0] * MarkerPrinter.ptPerMeter, pageBorder[1] * MarkerPrinter.ptPerMeter)

        # Check
        path, nameExt = os.path.split(filePath)
        name, ext = os.path.splitext(nameExt)

        if(len(path) > 0):
            if not(os.path.isdir(path)):
                os.makedirs(path)

        if((ext.upper() != ".SVG") and (ext.upper() != ".PS") and (ext.upper() != ".PDF")):
            raise ValueError("file extention is not supported, should be: svg, ps, pdf")

        # Draw
        with MarkerPrinter.surface[ext.upper()] (
            filePath,
            chessboardSize[0] * markerLength + (chessboardSize[0] - 1) * markerSeparation + pageBorder[0] * 2,
            chessboardSize[1] * markerLength + (chessboardSize[1] - 1) * markerSeparation + pageBorder[1] * 2) as surface:
            context = cairo.Context(surface)

            context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
            context.rectangle(0, 0,
                chessboardSize[0] * markerLength + (chessboardSize[0] - 1) * markerSeparation + pageBorder[0] * 2,
                chessboardSize[1] * markerLength + (chessboardSize[1] - 1) * markerSeparation + pageBorder[1] * 2)
            context.fill()

            context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
            context.rectangle(pageBorder[0], pageBorder[1],
                chessboardSize[0] * markerLength + (chessboardSize[0] - 1) * markerSeparation,
                chessboardSize[1] * markerLength + (chessboardSize[1] - 1) * markerSeparation)
            context.fill()

            for bx in range(chessboardSize[0]):
                for by in range(chessboardSize[1]):
                    MarkerPrinter.__DrawBlock(
                        context = context,
                        dictionary = dictionary,
                        markerLength = markerLength,
                        borderBits = borderBits,
                        chessboardSize = chessboardSize,
                        squareLength = markerLength + markerSeparation,
                        firstMarkerID = firstMarker,
                        blockX = bx,
                        blockY = by,
                        pageBorderX = pageBorder[0],
                        pageBorderY = pageBorder[1],
                        mode = "ARUCOGRID")

        if(subSize is not None):
            subDivide = (\
                chessboardSize[0] // subSize[0] + int(chessboardSize[0] % subSize[0] > 0),
                chessboardSize[1] // subSize[1] + int(chessboardSize[1] % subSize[1] > 0))

            subChessboardBlockX = np.clip ( np.arange(0, subSize[0] * subDivide[0] + 1, subSize[0]), 0, chessboardSize[0])
            subChessboardBlockY = np.clip ( np.arange(0, subSize[1] * subDivide[1] + 1, subSize[1]), 0, chessboardSize[1])

            subChessboardSliceX = subChessboardBlockX.astype(np.float) * (markerLength + markerSeparation)
            subChessboardSliceY = subChessboardBlockY.astype(np.float) * (markerLength + markerSeparation)

            subChessboardSliceX[-1] -= markerSeparation
            subChessboardSliceY[-1] -= markerSeparation

            for subXID in range(subDivide[0]):
                for subYID in range(subDivide[1]):
                    subName = name + \
                        "_X" + str(subChessboardBlockX[subXID]) + "_" + str(subChessboardBlockX[subXID+1]) + \
                        "_Y" + str(subChessboardBlockY[subYID]) + "_" + str(subChessboardBlockY[subYID+1])

                    with MarkerPrinter.surface[ext.upper()](
                        os.path.join(path, subName + ext),
                        subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID] + pageBorder[0] * 2,
                        subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID] + pageBorder[1] * 2) as surface:
                        context = cairo.Context(surface)

                        context.set_source_rgba(0.5, 0.5, 0.5, 1.0)
                        context.rectangle(0, 0,
                            subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID] + pageBorder[0] * 2,
                            subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID] + pageBorder[1] * 2)
                        context.fill()

                        context.set_source_rgba(1.0, 1.0, 1.0, 1.0)
                        context.rectangle(pageBorder[0], pageBorder[1],
                            subChessboardSliceX[subXID+1] - subChessboardSliceX[subXID],
                            subChessboardSliceY[subYID+1] - subChessboardSliceY[subYID])
                        context.fill()

                        for bx in range(subChessboardBlockX[subXID+1] - subChessboardBlockX[subXID]):
                            for by in range(subChessboardBlockY[subYID+1] - subChessboardBlockY[subYID]):
                                MarkerPrinter.__DrawBlock(
                                    context = context,
                                    dictionary = dictionary,
                                    markerLength = markerLength,
                                    borderBits = borderBits,
                                    chessboardSize = chessboardSize,
                                    squareLength = markerLength + markerSeparation,
                                    firstMarkerID = firstMarker,
                                    blockX = subChessboardBlockX[subXID] + bx,
                                    blockY = subChessboardBlockY[subYID] + by,
                                    originX = subChessboardBlockX[subXID],
                                    originY = subChessboardBlockY[subYID],
                                    pageBorderX = pageBorder[0],
                                    pageBorderY = pageBorder[1],
                                    mode = "ARUCOGRID")

if __name__ == '__main__':
    parser = ArgumentParser()

    # Save marker image parameters
    chessGroup = parser.add_argument_group('chess', 'Chessboard')
    arucoGroup = parser.add_argument_group('aruco', 'ArUco')
    arucoGridGroup = parser.add_argument_group('aruco_grid', 'ArUco grid')
    charucoGroup = parser.add_argument_group('charuco', 'ChArUco')
    exclusiveGroup = parser.add_mutually_exclusive_group()

    exclusiveGroup.add_argument(
        "--chess", action='store_true', default=False,
        help="Choose to save chessboard marker")

    exclusiveGroup.add_argument(
        "--aruco", action='store_true', default=False,
        help="Choose to save ArUco marker")

    exclusiveGroup.add_argument(
        "--aruco_grid", action='store_true', default=False,
        help="Choose to save ArUco grid marker")

    exclusiveGroup.add_argument(
        "--charuco", action='store_true', default=False,
        help="Choose to save ChArUco marker")

    # Utility functions parameters
    exclusiveGroup.add_argument(
        "--generate", dest="arucoDataFileName",
        help="Generate aruco data to FILE", metavar="FILE")

    exclusiveGroup.add_argument(
        "--list_dictionary", action='store_true', default=False,
        help="List predefined aruco dictionary")

    # Parameters
    # fileName
    parser.add_argument(
        "--file", dest="fileName", default="./image.pdf",
        help="Save marker image to FILE", metavar="FILE")
    for group in [chessGroup, arucoGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_file", dest="fileName",
            help="Save marker image to FILE", metavar="FILE")

    # dictionary
    parser.add_argument(
        "--dictionary", dest="dictionary", default="DICT_ARUCO_ORIGINAL",
        help="Generate marker via predefined DICTIONARY aruco dictionary", metavar="DICTIONARY")
    for group in [arucoGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_dictionary", dest="dictionary",
            help="Generate marker via predefined DICTIONARY aruco dictionary", metavar="DICTIONARY")

    # size
    parser.add_argument(
        "--size_x", dest="sizeX", default="16",
        help="Save marker image with N board width", metavar="N")
    parser.add_argument(
        "--size_y", dest="sizeY", default="9",
        help="Save marker image with N board height", metavar="N")

    for group in [chessGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_size_x", dest="sizeX",
            help="Save marker image with N board width", metavar="N")
        group.add_argument(
            "--" + group.title + "_size_y", dest="sizeY",
            help="Save marker image with N board height", metavar="N")

    # length
    parser.add_argument(
        "--square_length", dest="squareLength", default="0.09",
        help="Save marker image with L square length (Unit: meter)", metavar="L")
    parser.add_argument(
        "--marker_length", dest="markerLength", default="0.07",
        help="Save marker image with L marker length (Unit: meter)", metavar="L")
    parser.add_argument(
        "--marker_separation", dest="markerSeparation", default="0.02",
        help="Save marker image with L separation length (Unit: meter)", metavar="L")

    for group in [chessGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_square_length", dest="squareLength",
            help="Save marker image with L blocks length (Unit: meter)", metavar="L")

    for group in [arucoGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_marker_length", dest="markerLength",
            help="Save marker image with L marker length (Unit: meter)", metavar="L")

    for group in [arucoGridGroup]:
        group.add_argument(
            "--" + group.title + "_marker_separation", dest="markerSeparation",
            help="Save marker image with L gap length (Unit: meter)", metavar="L")

    # else
    parser.add_argument(
        "--marker_id", dest="markerID", default="0",
        help="Save marker image with ID marker", metavar="ID")
    parser.add_argument(
        "--first_marker", dest="firstMarker", default="0",
        help="Save marker image that start with ID marker", metavar="ID")
    parser.add_argument(
        "--border_bits", dest="borderBits", default="1",
        help="Save marker image with N border size", metavar="N")

    for group in [arucoGroup]:
        group.add_argument(
            "--" + group.title + "_marker_id", dest="markerID",
            help="Save marker image with ID marker", metavar="ID")

    for group in [arucoGridGroup]:
        group.add_argument(
            "--" + group.title + "_first_marker", dest="firstMarker",
            help="Save marker image that start with ID marker", metavar="ID")

    for group in [arucoGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_border_bits", dest="borderBits",
            help="Save marker image with N border size", metavar="N")

    # sub size
    parser.add_argument(
        "--sub_size_x", dest="subSizeX", default="0",
        help="Save marker image with N chuck width", metavar="N")
    parser.add_argument(
        "--sub_size_y", dest="subSizeY", default="0",
        help="Save marker image with N chuck height", metavar="N")

    for group in [chessGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_sub_size_x", dest="subSizeX",
            help="Save marker image with N chuck width", metavar="N")
        group.add_argument(
            "--" + group.title + "_sub_size_y", dest="subSizeY",
            help="Save marker image with N chuck height", metavar="N")

    # page border
    parser.add_argument(
        "--page_border_x", dest="pageBorderX", default="0",
        help="Save with page border width L length (Unit: meter)", metavar="L")
    parser.add_argument(
        "--page_border_y", dest="pageBorderY", default="0",
        help="Save with page border height L length (Unit: meter)", metavar="L")

    for group in [chessGroup, arucoGroup, arucoGridGroup, charucoGroup]:
        group.add_argument(
            "--" + group.title + "_page_border_x", dest="pageBorderX", default="0",
            help="Save with page border width L length (Unit: meter)", metavar="L")
        group.add_argument(
            "--" + group.title + "_page_border_y", dest="pageBorderY", default="0",
            help="Save with page border height L length (Unit: meter)", metavar="L")

    # Run
    args = parser.parse_args()

    if(args.arucoDataFileName is not None):
        print("Generate aruco data to: " + args.arucoDataFileName)
        SaveArucoDictBytesList(args.arucoDataFileName)

    elif(args.list_dictionary):
        print("List predefined aruco dictionary")
        for i in MarkerPrinter.arucoDictBytesList.keys():
            print(i)

    elif(args.chess):
        try:
            sizeX = int(args.sizeX)
            sizeY = int(args.sizeY)
            squareLength = float(args.squareLength)
            subSizeX = int(args.subSizeX)
            subSizeY = int(args.subSizeY)
            pageBorderX = float(args.pageBorderX)
            pageBorderY = float(args.pageBorderY)
        except ValueError as e:
            warnings.warn(str(e))
        else:
            print("Save chessboard marker with parms: " + \
                    str({ \
                        "fileName": args.fileName, \
                        "sizeX": sizeX, \
                        "sizeY": sizeY, \
                        "squareLength": squareLength, \
                        "subSizeX": subSizeX, \
                        "subSizeY": subSizeY, \
                        "pageBorderX": pageBorderX, \
                        "pageBorderY": pageBorderY, \
                    }))

            subSize = None

            if(subSizeX > 0):
                if(subSizeY > 0):
                    subSize = (subSizeX, subSizeY)
                else:
                    subSize = (subSizeX, sizeY)
            else:
                if(subSizeY > 0):
                    subSize = (sizeX, subSizeY)
                else:
                    subSize = None

            # Gen
            MarkerPrinter.GenChessMarkerImage(args.fileName, (sizeX, sizeY), squareLength, subSize = subSize, pageBorder = (pageBorderX, pageBorderY))

    elif(args.aruco):
        try:
            markerLength = float(args.markerLength)
            markerID = int(args.markerID)
            borderBits = int(args.borderBits)
            pageBorderX = float(args.pageBorderX)
            pageBorderY = float(args.pageBorderY)
        except ValueError as e:
            warnings.warn(str(e))
        else:
            print("Save ArUco marker with parms: " + \
                    str({ \
                        "fileName": args.fileName, \
                        "dictionary": args.dictionary, \
                        "markerLength": markerLength, \
                        "markerID": markerID, \
                        "borderBits": borderBits, \
                        "pageBorderX": pageBorderX, \
                        "pageBorderY": pageBorderY, \
                    }))

            # Gen
            MarkerPrinter.GenArucoMarkerImage(args.fileName, args.dictionary, markerID, markerLength, borderBits=borderBits, pageBorder = (pageBorderX, pageBorderY))

    elif(args.aruco_grid):
        try:
            sizeX = int(args.sizeX)
            sizeY = int(args.sizeY)
            markerLength = float(args.markerLength)
            markerSeparation = float(args.markerSeparation)
            firstMarker = int(args.firstMarker)
            borderBits = int(args.borderBits)
            subSizeX = int(args.subSizeX)
            subSizeY = int(args.subSizeY)
            pageBorderX = float(args.pageBorderX)
            pageBorderY = float(args.pageBorderY)
        except ValueError as e:
            warnings.warn(str(e))
        else:
            print("Save ArUco grid marker with parms: " + \
                    str({ \
                        "fileName": args.fileName, \
                        "dictionary": args.dictionary, \
                        "sizeX": sizeX, \
                        "sizeY": sizeY, \
                        "markerLength": markerLength, \
                        "markerSeparation": markerSeparation, \
                        "firstMarker": firstMarker, \
                        "borderBits": borderBits, \
                        "subSizeX": subSizeX, \
                        "subSizeY": subSizeY, \
                        "pageBorderX": pageBorderX, \
                        "pageBorderY": pageBorderY, \
                    }))

            subSize = None

            if(subSizeX > 0):
                if(subSizeY > 0):
                    subSize = (subSizeX, subSizeY)
                else:
                    subSize = (subSizeX, sizeY)
            else:
                if(subSizeY > 0):
                    subSize = (sizeX, subSizeY)
                else:
                    subSize = None

            # Gen
            MarkerPrinter.GenArucoGridMarkerImage(args.fileName, args.dictionary, (sizeX, sizeY), markerLength, markerSeparation, firstMarker, borderBits=borderBits, subSize=subSize, pageBorder = (pageBorderX, pageBorderY))

    elif(args.charuco):
        try:
            sizeX = int(args.sizeX)
            sizeY = int(args.sizeY)
            squareLength = float(args.squareLength)
            markerLength = float(args.markerLength)
            borderBits = int(args.borderBits)
            subSizeX = int(args.subSizeX)
            subSizeY = int(args.subSizeY)
            pageBorderX = float(args.pageBorderX)
            pageBorderY = float(args.pageBorderY)
        except ValueError as e:
            warnings.warn(str(e))
        else:
            print("Save ChArUco marker with parms: " + \
                    str({ \
                        "fileName": args.fileName, \
                        "dictionary": args.dictionary, \
                        "sizeX": sizeX, \
                        "sizeY": sizeY, \
                        "squareLength": squareLength, \
                        "markerLength": markerLength, \
                        "borderBits": borderBits, \
                        "subSizeX": subSizeX, \
                        "subSizeY": subSizeY, \
                        "pageBorderX": pageBorderX, \
                        "pageBorderY": pageBorderY, \
                    }))

            subSize = None

            if(subSizeX > 0):
                if(subSizeY > 0):
                    subSize = (subSizeX, subSizeY)
                else:
                    subSize = (subSizeX, sizeY)
            else:
                if(subSizeY > 0):
                    subSize = (sizeX, subSizeY)
                else:
                    subSize = None

            # Gen
            MarkerPrinter.GenCharucoMarkerImage(args.fileName, args.dictionary, (sizeX, sizeY), squareLength, markerLength, borderBits=borderBits, subSize=subSize, pageBorder = (pageBorderX, pageBorderY))

    else:
        parser.print_help()

BONUS

以下是svg文件转为png或者pdf的代码

import cairosvg

# svg_path = 'a.svg'
# png_path = 'a.png'
# cairosvg.svg2png(url=svg_path, write_to=png_path)


# import os
#
# path = "001"
# for file in os.listdir(path):
#     path = ''
#     path = os.path.join(path, file)
#     #     print(path)
#
#     if file.endswith('.svg'):
#         png_path = path.replace('.svg', '.png')
#         print(path, png_path)
#         cairosvg.svg2png(url=path, write_to=png_path)
#     else:
#         print(path)

import cairosvg

cairosvg.svg2pdf(url='chess.svg', write_to='image.pdf')

for PDF and SVG, 1 pixel = 1/72 inch, 1 cm = 1/2.54 inch, 1pixl = 2.54/72 cm, 1cm = 72/2.54 pixels

参考目录

https://github.com/opencv/opencv_contrib/tree/4.x/modules/aruco/misc/pattern_generator

上一篇:SilverLight开发系列第2步:使用vs2008和Blend 2.5打造Hellow World程序


下一篇:分类算法总结