就是把png中的每一个像素转换成一个小矩形,图片小的话效果还行。
def jpg_to_svg(path):
image_raw = tf.io.gfile.GFile(path, 'rb').read()
img = tf.image.decode_jpeg(image_raw, channels=1)
w, h, c = img.shape
res = ['<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ',
str(w), ' ', str(h), '">']
for y in range(len(img)):
line = img[y]
for x in range(len(line)):
pixel = line[x]
color = pixel[0]
s = str(hex(color))[2:]
add = ['<rect x="', str(x), '" y="', str(y),
'" width="1" height="1" fill="#', s, s, s, '"/>']
res.append(''.join(add))
res.append('</svg>')
with open(path+'.svg', mode='w') as file_handle:
file_handle.write(''.join(res))