这是一张jpg的位图,所谓位图,就是由像素组成的原始图片,而CAD是由点线面组成的矢量图。
一、设定绘图环境,导入所需的包:
import os
os.chdir(r'C:\Users\super.000\Desktop')
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import skimage
from skimage import io,feature,color,measure
二、定义CAD绘图函数:
def plotLine(line):
s = 'pline\n'
for i in line:
x,y = i
s +='%s,%s\n'%(y,-x)
s += ' \n'
return s
三、定义位图转矢量函数:
def imContours(im,level):
im_gray = color.rgb2gray(im)
im_contours = measure.find_contours(im_gray,level)
plt.figure(figsize=(12,8))
plt.title('level is :%s'%level)
for line in im_contours:
x,y = line[:,0],line[:,1]
plt.plot(y,-x)
return im_contours
四、手工删选出最合适的level匹配值:
im = io.imread('dog.jpg')
for level in np.arange(0,1,0.1):
imContours(im,level)
五、打印输出转换的CAD位图:
with open('1.txt','w') as f:
for line in imContours(im,0.3):
f.write(plotLine(line))
效果如下: