프로젝트

일반

사용자정보

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

hytos / DTI_PID / DTI_PID / potrace.py @ 11ae4ec5

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

1 e3cdaf74 humkyung
#!/usr/bin/env/python3
2
# coding: utf-8
3
4
import numpy as np, cv2, subprocess
5 b0323751 humkyung
import os, sys
6 9dddbb7d humkyung
from xml.dom import minidom
7
import svg.path
8
from xml.etree.ElementTree import Element, SubElement, dump, ElementTree
9
import SymbolBase
10 b0323751 humkyung
from shapely.geometry import LineString
11
from shapely.ops import linemerge
12 5fd8cd99 humkyung
from LineDetector import LineDetector
13 e3cdaf74 humkyung
14
# potrace command
15
POTRACE = os.path.dirname(os.path.realpath(__file__)) + '\\potrace.exe'
16 e08fd87f 김정우
ADJUST = 10
17 9dddbb7d humkyung
18 aaf53137 김정우
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 1b502652 김정우
    retval, buf = cv2.imencode('.bmp', threshold)
26 aaf53137 김정우
    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 35d6d96c 김정우
    )
43 aaf53137 김정우
        
44
    stdout, stderr = p.communicate(input=binbmp)
45
    if len(stderr) != 0:
46
        raise RuntimeError('Potrace threw error:\n' + stderr.decode('utf-8'))
47 35d6d96c 김정우
    
48 1b502652 김정우
    svgFile = open(destFilePath, "w")
49
    svgFile.write(stdout.decode("utf-8"))
50
    svgFile.close()
51 aaf53137 김정우
52 cd198f3b humkyung
if __name__ == '__main__':
53
    execpath = os.path.dirname(os.path.realpath(__file__))
54 e3cdaf74 humkyung
55 cd198f3b humkyung
    path = execpath + '\\res\\UY1-K-2007_P1_300dpi_black.png'
56 5fd8cd99 humkyung
    #lines = linedraw.sketch(path)
57
    #linedraw.visualize(lines)
58 cd198f3b humkyung
#    path = sys.argv[1]  # 1 argument given is a string for the path of drawing
59 5fd8cd99 humkyung
    #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 b0323751 humkyung
    
64 5fd8cd99 humkyung
    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)
클립보드 이미지 추가 (최대 크기: 500 MB)