class EveryXDays(Base): HELP = _('''\ Download this periodical every x days. For example, if you choose 30 days, the periodical will be downloaded every 30 days. Note that you can set periods of less than a day, like 0.1 days to download a periodical more than once a day. ''') def __init__(self, parent=None): Base.__init__(self, parent) self.l1 = QLabel(_('&Download every:')) self.interval = QDoubleSpinBox(self) self.interval.setMinimum(0.04) self.interval.setSpecialValueText(_('every hour')) self.interval.setMaximum(1000.0) self.interval.setValue(31.0) self.interval.setSuffix(' ' + _('days')) self.interval.setSingleStep(1.0) self.interval.setDecimals(2) self.l1.setBuddy(self.interval) self.l2 = QLabel( _('Note: You can set intervals of less than a day,' ' by typing the value manually.')) self.l2.setWordWrap(True) self.l.addWidget(self.l1, 0, 0, 1, 1) self.l.addWidget(self.interval, 0, 1, 1, 1) self.l.addWidget(self.l2, 1, 0, 1, -1) def initialize(self, typ=None, val=None): if val is None: val = 31.0 self.interval.setValue(val) @property def schedule(self): schedule = self.interval.value() return 'interval', schedule
class EveryXDays(Base): HELP = _('''\ Download this periodical every x days. For example, if you choose 30 days, the periodical will be downloaded every 30 days. Note that you can set periods of less than a day, like 0.1 days to download a periodical more than once a day. ''') def __init__(self, parent=None): Base.__init__(self, parent) self.l1 = QLabel(_('&Download every:')) self.interval = QDoubleSpinBox(self) self.interval.setMinimum(0.04) self.interval.setSpecialValueText(_('every hour')) self.interval.setMaximum(1000.0) self.interval.setValue(31.0) self.interval.setSuffix(' ' + _('days')) self.interval.setSingleStep(1.0) self.interval.setDecimals(2) self.l1.setBuddy(self.interval) self.l2 = QLabel(_('Note: You can set intervals of less than a day,' ' by typing the value manually.')) self.l2.setWordWrap(True) self.l.addWidget(self.l1, 0, 0, 1, 1) self.l.addWidget(self.interval, 0, 1, 1, 1) self.l.addWidget(self.l2, 1, 0, 1, -1) def initialize(self, typ=None, val=None): if val is None: val = 31.0 self.interval.setValue(val) @property def schedule(self): schedule = self.interval.value() return 'interval', schedule
class MainWindow(QWidget): def __init__(self): QMainWindow.__init__(self) self.resize(500, 300) self.mainLayout = QHBoxLayout() self.chooseLayout = QHBoxLayout() self.layout = QVBoxLayout() self.pixLayout = QHBoxLayout() self.thresholdLayout = QHBoxLayout() self.timeLayout = QHBoxLayout() self.contoursLayout = QHBoxLayout() self.setLayout(self.mainLayout) self.setWindowTitle( "Image To Gcode V1.0 ----- build By yizheneng [email protected]") self.imageLabel = QLabel("image") self.mainLayout.addWidget(self.imageLabel) self.mainLayout.addLayout(self.layout) self.mainLayout.setStretchFactor(self.layout, 1) self.mainLayout.setStretchFactor(self.imageLabel, 3) self.pixLengthLabel = QLabel(u"像素大小(mm):") self.pixDoubleSpinBox = QDoubleSpinBox() self.pixDoubleSpinBox.setValue(1) self.pixDoubleSpinBox.setDecimals(6) self.pixLayout.addWidget(self.pixLengthLabel) self.pixLayout.addWidget(self.pixDoubleSpinBox) self.thresholdLabel = QLabel(u"阈值:") self.thresholdSpinBox = QSpinBox() self.thresholdSpinBox.valueChanged.connect(self.ThresholdValChange) self.thresholdSpinBox.setMaximum(255) self.thresholdSpinBox.setValue(120) self.thresholdLayout.addWidget(self.thresholdLabel) self.thresholdLayout.addWidget(self.thresholdSpinBox) self.timeLabel = QLabel(u"灼烧时间:") self.timeDoubleSpinBox = QDoubleSpinBox() self.timeDoubleSpinBox.setValue(0.3) self.timeLayout.addWidget(self.timeLabel) self.timeLayout.addWidget(self.timeDoubleSpinBox) self.chooseLabel = QLabel(u"只雕刻轮廓:") self.chooseBox = QCheckBox() self.chooseLayout.addWidget(self.chooseLabel) self.chooseLayout.addWidget(self.chooseBox) self.chooseBox.stateChanged.connect(self.ChooseValChanged) self.contoursWidthLabel = QLabel(u"边框宽度") self.ContoursWidthSpinBox = QSpinBox() self.ContoursWidthSpinBox.setEnabled(False) self.ContoursWidthSpinBox.setValue(1) self.contoursLayout.addWidget(self.contoursWidthLabel) self.contoursLayout.addWidget(self.ContoursWidthSpinBox) self.loadImageButton = QPushButton(u"加载图片") self.loadImageButton.clicked.connect(self.LoadImageButtonClicked) self.previewButton = QPushButton(u"预览") self.previewButton.clicked.connect(self.ThresholdValChange) self.makeCodeButton = QPushButton(u"生成G代码") self.makeCodeButton.clicked.connect(self.MakeGcode) self.layout.addLayout(self.pixLayout) self.layout.addLayout(self.thresholdLayout) self.layout.addLayout(self.timeLayout) self.layout.addLayout(self.chooseLayout) self.layout.addLayout(self.contoursLayout) self.layout.addWidget(self.loadImageButton) self.layout.addWidget(self.previewButton) self.layout.addWidget(self.makeCodeButton) def LoadImageButtonClicked(self): self.filePath = QFileDialog.getOpenFileName(self, u"选择图片文件", "", "Images (*.bmp)") if self.filePath == "": QMessageBox.warning(self, u"发生错误", u"没有选择可以识别的文件!!") return self.srcImage = QImage(self.filePath) self.grayImage = QImage(self.srcImage.size(), QImage.Format_Indexed8) for i in range(256): self.grayImage.setColor(i, qRgb(i, i, i)) for i in range(self.srcImage.width()): for j in range(self.srcImage.height()): temp = qGray(self.srcImage.pixel(i, j)) self.grayImage.setPixel(i, j, temp) self.srcImage = QImage(self.grayImage) self.resultImage = QImage(self.grayImage) self.imageLabel.setPixmap(QPixmap(self.srcImage)) def ChooseValChanged(self): self.ContoursWidthSpinBox.setEnabled(self.chooseBox.isChecked()) def ThresholdValChange(self): for i in range(self.srcImage.width()): for j in range(self.srcImage.height()): temp = self.srcImage.pixelIndex(i, j) if (temp >= self.thresholdSpinBox.value()): self.grayImage.setPixel(i, j, 255) else: self.grayImage.setPixel(i, j, 0) self.resultImage = QImage(self.grayImage) #如果选中了只雕刻轮廓 if self.chooseBox.isChecked(): img = np.zeros( (self.grayImage.height(), self.grayImage.width(), 1), np.uint8) for i in range(self.grayImage.width()): for j in range(self.grayImage.height()): img[j, i] = self.grayImage.pixelIndex(i, j) #提取轮廓 contours = cv.findContours(img, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE) img = np.zeros( (self.grayImage.height(), self.grayImage.width(), 1), np.uint8) cv.drawContours(img, contours[1][:-1], -1, (255, 255, 255), self.ContoursWidthSpinBox.value()) #转换轮廓到显示界面 for i in range(self.resultImage.width()): for j in range(self.resultImage.height()): if img[j, i] == 0: self.resultImage.setPixel(i, j, 255) else: self.resultImage.setPixel(i, j, 0) self.imageLabel.setPixmap(QPixmap(self.resultImage)) def MakeGcode(self): path = QFileDialog.getSaveFileName(self, u"选择保存路径", "", " (*.nc)") if path == "": QMessageBox.warning(self, u"发生错误", u"路径错误!!") return f = open(path, 'w') f.write("M5\n") for i in range(self.resultImage.width()): flag = False #检测这一行是否有点 for j in range(self.resultImage.height()): if self.resultImage.pixelIndex(i, j) < 128: flag = True break #如果这一行都没有点则跳过这一行 if flag: f.write("G0 Y%f\n" % (i * self.pixDoubleSpinBox.value())) else: continue if (i % 2) > 0: for j in range(self.resultImage.height()): if self.resultImage.pixelIndex(i, j) < 128: f.write("G0 X%f\n" % (j * self.pixDoubleSpinBox.value())) f.write("M3\n") f.write("G4 P%f\n" % self.timeDoubleSpinBox.value()) f.write("M5\n") else: for j in range(self.resultImage.height())[::-1]: if self.resultImage.pixelIndex(i, j) < 128: f.write("G0 X%f\n" % (j * self.pixDoubleSpinBox.value())) f.write("M3\n") f.write("G4 P%f\n" % self.timeDoubleSpinBox.value()) f.write("M5\n") f.write("M5\n") f.write("G0 X0 Y0\n") f.close() QMessageBox.information(self, u"成功", u"生成G代码文件成功!!")