프로젝트

일반

사용자정보

개정판 e3cdaf74

IDe3cdaf742cd521ece81a2779d67809305ac0ff14
상위 9ecba041
하위 4135735d

humkyung 이(가) 약 7년 전에 추가함

Add potrace files

차이점 보기:

DTI_PID/DTI_PID/potrace.py
1
#!/usr/bin/env/python3
2
# coding: utf-8
3

  
4
import numpy as np, cv2, subprocess
5
import os
6

  
7
# potrace command
8
POTRACE = os.path.dirname(os.path.realpath(__file__)) + '\\potrace.exe'
9
    
10
def passpotrace(image): 
11
    # convert to bmp binary so that potrace can handle it
12
    retval, buf = cv2.imencode('.bmp', image)
13
    if retval == False:
14
        raise ValueError('Failed to convert into BMP binary data')
15
    # convert buf from numpy.ndarray to bytes
16
    binbmp = buf.tobytes()
17
    
18
    args = [
19
        POTRACE,
20
        '-', '-o-', '--svg'
21
    ]
22
    
23
    p = subprocess.Popen(
24
        args,
25
        stdin=subprocess.PIPE,
26
        stdout=subprocess.PIPE,
27
        stderr=subprocess.PIPE,
28
        shell=False
29
        )
30
        
31
    stdout, stderr = p.communicate(input=binbmp)
32
    if len(stderr) != 0:
33
        raise RuntimeError('Potrace threw error:\n' + stderr.decode('utf-8'))
34

  
35
    return stdout
36
    
37
if __name__ == '__main__':
38
    execpath = os.path.dirname(os.path.realpath(__file__))
39

  
40
    testpic = cv2.imread(execpath + '\\res\\UY1-K-2007_P1_300dpi_black.png', cv2.IMREAD_GRAYSCALE)
41
    svg = passpotrace(testpic).decode('utf-8')
42

  
43

  
44
    file = open(execpath + '\\res\\UY1-K-2007_P1_300dpi_black.svg', 'w')
45
    file.write(svg)
46
    file.close()

내보내기 Unified diff