m24h作者
4年10个月前修改于 4年10个月前IP:上海
900481
15楼

再贡献一个脚本 直接从图片转换和zip压缩生成可用的CWS文件

同样图片生成3层 每层曝光45秒 我这里干膜曝光一层即可停止取下 油墨曝光3层比较合适

不同打印机需要自己修改脚本中内嵌的.gcode和slice.conf文件内容

---

少了一句以为是注释的“; Number of Slices = 3” 而且slice.conf文件是不必要的 脚本已被修改

----cwsmake.bat---

0<0# : ^
''' 
@python %~f0 %* 
@goto :eof
'''

import zipfile
from PIL import Image
import numpy
import sys
import io
import os

if len(sys.argv)<2 :
	print('Make a CWS zipped file with 3 layers using an image file')
	print('Usage: python',sys.argv[0],'<image file name> <output CWS filename>')
	print('Example: python',sys.argv[0],'circuit.bmp circuit.cws')
	exit(1)
	
if len(sys.argv)>2:
	cwsfile=sys.argv[2]
else:
	cwsfile=os.path.splitext(sys.argv[1])[0]+'.cws'
	
img = numpy.array(Image.open(sys.argv[1]).convert('L'))
(h,w)=img.shape
nw=int((w+2)/3)
img2=numpy.empty([h,nw,3], dtype=img.dtype)
for y in range(h):
	for x in range(nw):
		img2[y,x,2]=img[y,x*3]
		img2[y,x,1]=img[y,x*3+1] if x*3+1<w else 0
		img2[y,x,0]=img[y,x*3+2] if x*3+2<w else 0

imgio=io.BytesIO()	
Image.fromarray(img2).save(imgio, format='PNG')
b_png=imgio.getvalue()

b_gcode=b'''
;Number of Slices = 3
G28
G21 ;Set units to be mm
G91 ;Relative Positioning
M17 ;Enable motors
<Slice> Blank
M106 S0

;<Slice> 0
M106 S255
;<Delay> 45000
M106 S0
;<Slice> Blank
G1 Z4.000 F20
G1 Z-3.950 F20
;<Delay> 34000

;<Slice> 1
M106 S255
;<Delay> 45000
M106 S0
;<Slice> Blank
G1 Z4.000 F20
G1 Z-3.950 F20
;<Delay> 34000

;<Slice> 2
M106 S255
;<Delay> 45000
M106 S0
;<Slice> Blank
G1 Z4.000 F20
G1 Z-3.950 F20
;<Delay> 34000

M18 ;Disable Motors
M106 SO
G1 Z80
;<Completed>
'''


try:
	import zlib
	zipcompmode=zipfile.ZIP_DEFLATED
except:
	zipcompmode=zipfile.ZIP_STORED
	
with zipfile.ZipFile(cwsfile, mode='w', compression=zipcompmode) as zf:
	zf.writestr('b.gcode', b_gcode)
	zf.writestr('b000.png', b_png)
	zf.writestr('b001.png', b_png)
	zf.writestr('b002.png', b_png)

print ('CWS File', cwsfile, 'created')
exit(0)


评论
文号 / 900481

千古风流
名片专栏发私信
学术分 1
总主题 68 帖总回复 1007 楼拥有证书:进士 学者 机友
注册于 2020/01/22 18:44:43最后登录 2026/10/31 02:54:58
主体类型:个人
所属领域:无
认证方式:手机号
IP归属地:上海

个人简介

个人开源项目: m24h.github.io

笔记
{{note.content}}
{{n.user.username}}
{{fromNow(n.toc)}} {{n.status === noteStatus.disabled ? "已屏蔽" : ""}} {{n.status === noteStatus.unknown ? "正在审核" : ""}} {{n.status === noteStatus.deleted ? '已删除' : ''}}
  • 编辑
  • 删除
  • {{n.status === 'disabled' ? "解除屏蔽" : "屏蔽" }}
我也是有底线的