프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / DTI_PID / potrace.py @ 6e4b95fc

이력 | 보기 | 이력해설 | 다운로드 (2.19 KB)

1
#!/usr/bin/env/python3
2
# coding: utf-8
3

    
4
import numpy as np, cv2, subprocess
5
import os, sys
6
from xml.dom import minidom
7
import svg.path
8
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
9
import SymbolBase
10
from shapely.geometry import LineString
11
from shapely.ops import linemerge
12
from LineDetector import LineDetector
13

    
14
# potrace command
15
POTRACE = os.path.dirname(os.path.realpath(__file__)) + '\\potrace.exe'
16
ADJUST = 10
17

    
18
def convertImageToSvg(imgFilePath, destFilePath):
19
    thresh = 127
20

    
21
    image  = cv2.imread(imgFilePath, cv2.IMREAD_GRAYSCALE)
22
    threshold = cv2.threshold(image, thresh, 255, cv2.THRESH_BINARY)[1]
23

    
24
    # convert to bmp binary so that potrace can handle it
25
    retval, buf = cv2.imencode('.bmp', threshold)
26
    if retval == False:
27
        raise ValueError('Failed to convert into BMP binary data')
28
    # convert buf from numpy.ndarray to bytes
29
    binbmp = buf.tobytes()
30
    
31
    args = [
32
        POTRACE,
33
        '-', '-o-', '--svg'
34
    ]
35
    
36
    p = subprocess.Popen(
37
        args,
38
        stdin=subprocess.PIPE,
39
        stdout=subprocess.PIPE,
40
        stderr=subprocess.PIPE,
41
        shell=False
42
    )
43
        
44
    stdout, stderr = p.communicate(input=binbmp)
45
    if len(stderr) != 0:
46
        raise RuntimeError('Potrace threw error:\n' + stderr.decode('utf-8'))
47
    
48
    svgFile = open(destFilePath, "w")
49
    svgFile.write(stdout.decode("utf-8"))
50
    svgFile.close()
51

    
52
if __name__ == '__main__':
53
    execpath = os.path.dirname(os.path.realpath(__file__))
54

    
55
    path = execpath + '\\res\\UY1-K-2007_P1_300dpi_black.png'
56
    #lines = linedraw.sketch(path)
57
    #linedraw.visualize(lines)
58
#    path = sys.argv[1]  # 1 argument given is a string for the path of drawing
59
    #imgLines = passpotrace(path)    #execpath + '\\res\\UY1-K-2007_P1_300dpi_black.png')
60
    image  = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
61
    lineDetector = LineDetector(image)
62
    lineDetector.Detect()
63
    
64
    try:
65
        xmlFilePath = os.path.dirname(path) + '\\' + os.path.basename(path).split('.')[0] + '.xml'
66
        file = open(xmlFilePath, 'w')
67
        for line in imgLines:
68
            file.write(str(line) + '\n')
69
        file.close()
70
    except Exception as ex:
71
        print('에러가 발생했습니다.\n', ex)