개정판 e3cdaf74
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