from
cairosvg
import
svg2png
import
os
import
re
def
makedirs(path):
if
not
os.path.exists(path):
os.makedirs(path)
def
ChangeSVG2png(svg_path, chinese):
outputpath
=
f
'strokeOrder/{chinese}'
makedirs(outputpath)
png_files
=
sorted
([f
for
f
in
os.listdir(outputpath)
if
f.startswith(f
'{chinese}_'
)
and
f.endswith(
'.png'
)])
if
len
(png_files)
=
=
len
(svg_path):
print
(f
'*** {chinese} 的笔顺图已经生成,跳过***'
)
return
svg_output
=
{
'width'
:
'1024px'
,
'height'
:
'1024px'
,
'xmlns'
:
'http://www.w3.org/2000/svg'
,
"font_color"
:
"#000000"
, # 黑色
"font_color_last"
:
"#FF1111"
, # 红色
"output_address"
: outputpath,
"output_filename"
: ''
}
if
not
os.path.exists(outputpath):
os.mkdir(outputpath)
grid_lines
=
[
'<line x1="0" y1="0" x2="1024" y2="1024" style="stroke:#EEEEEE;stroke-width:4;stroke-dasharray:10,10"/>'
,
'<line x1="1024" y1="0" x2="0" y2="1024" style="stroke:#EEEEEE;stroke-width:4;stroke-dasharray:10,10"/>'
,
'<line x1="0" y1="512" x2="1024" y2="512" style="stroke:#EEEEEE;stroke-width:4;stroke-dasharray:10,10"/>'
,
'<line x1="512" y1="0" x2="512" y2="1024" style="stroke:#EEEEEE;stroke-width:4;stroke-dasharray:10,10"/>'
,
]
if
len
(svg_path)
=
=
1
:
svg_code
=
[]
svg_code_temp
=
'<svg style="width:'
+
svg_output[
'width'
]
+
'; height:'
+
svg_output[
'height'
]
+
';" xmlns="'
+
\
svg_output[
'xmlns'
]
+
'">'
svg_code.append(svg_code_temp)
svg_code.extend(grid_lines)
svg_code_temp
=
' <g transform="translate(0, 900) scale(1, -1)">'
svg_code.append(svg_code_temp)
svg_code_temp
=
' <path d="'
+
svg_path[
0
]
+
'" style="fill:'
+
svg_output[
'font_color_last'
]
+
';"></path>'
svg_code.append(svg_code_temp)
svg_code_temp
=
' </g>'
svg_code.append(svg_code_temp)
svg_code_temp
=
'</svg>'
svg_code.append(svg_code_temp)
svgcode
=
'\n'
.join(svg_code)
svg_output[
'output_filename'
]
=
svg_output[
'output_address'
]
+
'/'
+
chinese
+
'1.png'
try
:
svg2png(bytestring
=
svgcode, write_to
=
svg_output[
'output_filename'
])
except
Exception as e:
print
(
'error:'
+
str
(e))
svg_code
=
[]
svg_code_temp
=
'<svg style="width:'
+
svg_output[
'width'
]
+
'; height:'
+
svg_output[
'height'
]
+
';" xmlns="'
+
svg_output[
'xmlns'
]
+
'">'
svg_code.append(svg_code_temp)
svg_code_temp
=
'<g transform="translate(0, 900) scale(1, -1)">'
svg_code.append(svg_code_temp)
for
j
in
range
(
len
(svg_path)):
svg_code_temp
=
' <path d="'
+
svg_path[j]
+
'" style="fill:'
+
svg_output[
'font_color'
]
+
';"></path>'
svg_code.append(svg_code_temp)
svg_code_temp
=
' </g>'
svg_code.append(svg_code_temp)
svg_code_temp
=
'</svg>'
svg_code.append(svg_code_temp)
svgcode
=
'\n'
.join(svg_code)
svg_output[
'output_filename'
]
=
svg_output[
'output_address'
]
+
'/'
+
chinese
+
'.svg'
try
:
with
open
(svg_output[
'output_filename'
],
'w'
) as f:
f.write(svgcode)
except
Exception as e:
print
(
'error:'
+
str
(e))
else
:
for
i
in
range
(
len
(svg_path)):
svg_code
=
[]
svg_code_temp
=
'<svg style="width:'
+
svg_output[
'width'
]
+
'; height:'
+
svg_output[
'height'
]
+
';" xmlns="'
+
svg_output[
'xmlns'
]
+
'">'
svg_code.append(svg_code_temp)
svg_code.extend(grid_lines)
svg_code_temp
=
' <g transform="translate(0, 900) scale(1, -1)">'
svg_code.append(svg_code_temp)
for
j
in
range
(i
+
1
):
if
j
=
=
i:
svg_code_temp
=
' <path d="'
+
svg_path[j]
+
'" style="fill:'
+
svg_output[
'font_color_last'
]
+
';"></path>'
else
:
svg_code_temp
=
' <path d="'
+
svg_path[j]
+
'" style="fill:'
+
svg_output[
'font_color'
]
+
';"></path>'
svg_code.append(svg_code_temp)
svg_code_temp
=
' </g>'
svg_code.append(svg_code_temp)
svg_code_temp
=
'</svg>'
svg_code.append(svg_code_temp)
svgcode
=
'\n'
.join(svg_code)
svg_output[
'output_filename'
]
=
svg_output[
'output_address'
]
+
'/'
+
chinese
+
'_'
+
str
(i
+
1
)
+
'.png'
try
:
svg2png(bytestring
=
svgcode, write_to
=
svg_output[
'output_filename'
])
except
Exception as e:
print
(
'error:'
+
str
(e))
svg_code
=
[]
svg_code_temp
=
'<svg style="width:'
+
svg_output[
'width'
]
+
'; height:'
+
svg_output[
'height'
]
+
';" xmlns="'
+
svg_output[
'xmlns'
]
+
'">'
svg_code.append(svg_code_temp)
svg_code_temp
=
'<g transform="translate(0, 900) scale(1, -1)">'
svg_code.append(svg_code_temp)
for
j
in
range
(
len
(svg_path)):
svg_code_temp
=
' <path d="'
+
svg_path[j]
+
'" style="fill:'
+
svg_output[
'font_color'
]
+
';"></path>'
svg_code.append(svg_code_temp)
svg_code_temp
=
' </g>'
svg_code.append(svg_code_temp)
svg_code_temp
=
'</svg>'
svg_code.append(svg_code_temp)
svgcode
=
'\n'
.join(svg_code)
svg_output[
'output_filename'
]
=
svg_output[
'output_address'
]
+
'/'
+
chinese
+
'.svg'
try
:
with
open
(svg_output[
'output_filename'
],
'w'
) as f:
f.write(svgcode)
except
Exception as e:
print
(
'error:'
+
str
(e))
def
main(svg_path, chinese):
ChangeSVG2png(svg_path, chinese)
if
__name__
=
=
'__main__'
:
svg_data
=
stroke_order
=
[]
for
line
in
svg_data.split(
'\n'
):
if
line.strip():
match
=
re.search(r
'd="([^"]+)"'
, line)
if
match:
path_data
=
match.group(
1
)
stroke_order.append(path_data)
chinese
=
'㲋'
# 修改为您要生成的汉字
svg_path
=
[x
for
x
in
stroke_order]
print
(svg_path)
print
(
len
(svg_path))
main(svg_path, chinese)