From 24bb4adb24204c4b7b8308197fa0ed58095f9287 Mon Sep 17 00:00:00 2001 From: wu58430 Date: Mon, 8 Jan 2024 12:01:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=86=99=E5=9B=BE=E7=89=87=E8=BD=AF=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MDC_WRITE/main.py => 写频脚本/picture.py | 123 ++++++++++++++++------- {MDC_WRITE => 写频脚本}/uvk5_cn.py | 0 2 files changed, 89 insertions(+), 34 deletions(-) rename MDC_WRITE/main.py => 写频脚本/picture.py (79%) rename {MDC_WRITE => 写频脚本}/uvk5_cn.py (100%) diff --git a/MDC_WRITE/main.py b/写频脚本/picture.py similarity index 79% rename from MDC_WRITE/main.py rename to 写频脚本/picture.py index 9718037..89c21e0 100644 --- a/MDC_WRITE/main.py +++ b/写频脚本/picture.py @@ -1,14 +1,18 @@ import serial +from PyQt5.QtWidgets import QButtonGroup + from PyQt5.QtCore import QTimer from PyQt5.QtWidgets import QMainWindow, QPushButton, QFileDialog, QLabel, QRadioButton, QMessageBox, QComboBox, \ QProgressBar from PyQt5.QtWidgets import QApplication import sys -from PyQt5.QtGui import QImage, QPixmap, QColor +from PyQt5.QtGui import QImage, QPixmap, QColor, qGray, qRgb + resized_image=None cal_bin=1 com_open="" -pixel_list =[] +turn_color=0 +compress_pixels = [0] * 1024 Crc16Tab = [0, 4129, 8258, 12387, 16516, 20645, 24774, 28903, 33032, 37161, 41290, 45419, 49548, 53677, 57806, 61935, 4657, 528, 12915, 8786, 21173, 17044, 29431, 25302, 37689, 33560, 45947, 41818, 54205, 50076, 62463, 58334, 9314, 13379, 1056, 5121, 25830, 29895, 17572, 21637, 42346, 46411, 34088, 38153, 58862, 62927, 50604, 54669, 13907, 9842, 5649, 1584, 30423, 26358, 22165, 18100, 46939, 42874, 38681, 34616, 63455, 59390, 55197, 51132, 18628, 22757, 26758, 30887, 2112, 6241, 10242, 14371, 51660, 55789, @@ -29,35 +33,44 @@ class MainWindow(QMainWindow): def initUI(self): global cal_bin + global turn_color - self.setWindowTitle("Image Processing") - self.setGeometry(100, 100, 50+50+256, 300) + self.setWindowTitle("K5图片") + self.setGeometry(100, 100, 50+20+256, 250) - self.open_button = QPushButton("Open Image", self) - self.open_button.setGeometry(20, 20, 100, 30) + self.open_button = QPushButton("打开图片", self) + self.open_button.setGeometry(10, 50, 100, 30) self.open_button.clicked.connect(self.open_image) - self.process_button = QPushButton("Process Image", self) - self.process_button.setGeometry(20, 70, 120, 30) + self.process_button = QPushButton("写入图片", self) + self.process_button.setGeometry(130, 50, 100, 30) self.process_button.clicked.connect(self.process_image) self.process_button.setEnabled(False) self.label = QLabel(self) - self.label.setGeometry(50, 150, 256, 128) - + self.label.setGeometry(35, 100, 256, 128) + self.button_group = QButtonGroup(self) self.radioButton1 = QRadioButton("效果1", self) self.radioButton2 = QRadioButton("效果2", self) + self.radioButton3 = QRadioButton("反色", self) cal_bin = 1 - self.radioButton1.setGeometry(0, 100, 120, 30) - self.radioButton2.setGeometry(50, 100, 120, 30) + self.button_group.addButton(self.radioButton1) + self.button_group.addButton(self.radioButton2) + self.radioButton1.setGeometry(250, 40, 120, 20) + self.radioButton2.setGeometry(250, 55, 120, 30) + self.radioButton3.setGeometry(250, 75, 120, 30) self.radioButton1.setChecked(True) # 默认选中第一个单选按钮 self.radioButton1.toggled.connect(self.on_radio_button_toggled) self.radioButton2.toggled.connect(self.on_radio_button_toggled) + self.radioButton3.toggled.connect(self.on_radio_button3_toggled) + + + turn_color = 0 self.combo_box = QComboBox(self) - self.combo_box.setGeometry(20, 120, 200, 30) + self.combo_box.setGeometry(10, 10, 100, 20) self.populate_serial_ports() # Populate available serial ports self.combo_box.currentIndexChanged.connect(self.on_combo_box_changed) @@ -66,7 +79,7 @@ class MainWindow(QMainWindow): self.timer.start(500) # Refresh every 5 seconds (1000 milliseconds) self.progress_bar = QProgressBar(self) - self.progress_bar.setGeometry(50, 150, 200, 20) + self.progress_bar.setGeometry(130, 10, 200, 20) self.progress_bar.setValue(0) # ... (previous code remains the same) @@ -103,28 +116,24 @@ class MainWindow(QMainWindow): def qimage_to_gray_list(self,img): if img.isNull(): return None - + global compress_pixels width = img.width() height = img.height() - - pixel_list = [] + compress_pixels = [0 for _ in range(len(compress_pixels))] for y in range(height): for x in range(width): gray_value = QColor(img.pixelColor(x, y)).lightness() - pixel_list.append(gray_value) - - return pixel_list + if gray_value!=255 : + compress_pixels[y // 8 * 128 + x]=compress_pixels[y//8*128+x]|(1<<(y%8)) def on_combo_box_changed(self, index): global com_open com_open = self.combo_box.currentText() - # Debugging: print selected serial port - print(f"Selected port: {com_open}") def on_radio_button_toggled(self): global cal_bin global resized_image - global pixel_list + global turn_color sender = self.sender() @@ -133,20 +142,31 @@ class MainWindow(QMainWindow): self.radioButton2.setChecked(False) if self.process_button.isEnabled() and cal_bin == 2: binarized_image = self.binarize_image1(resized_image) - pixel_list=self.qimage_to_gray_list(binarized_image) + compress_pixels=self.qimage_to_gray_list(binarized_image) self.show_img(binarized_image) cal_bin=1 - else: + elif sender == self.radioButton2: self.radioButton1.setChecked(False) if self.process_button.isEnabled() and cal_bin == 1: binarized_image = self.binarize_image2(resized_image) - pixel_list=self.qimage_to_gray_list(binarized_image) + compress_pixels=self.qimage_to_gray_list(binarized_image) self.show_img(binarized_image) cal_bin=2 + + def on_radio_button3_toggled(self): + global turn_color + turn_color = 1 - turn_color + if self.radioButton1.isChecked(): + binarized_image = self.binarize_image1(resized_image) + else: + binarized_image = self.binarize_image2(resized_image) + + compress_pixels = self.qimage_to_gray_list(binarized_image) + self.show_img(binarized_image) + def open_image(self): global resized_image global cal_bin - global pixel_list options = QFileDialog.Options() file_path, _ = QFileDialog.getOpenFileName(self, "Open Image File", "", "Image Files (*.jpg *.png *.bmp *.jpeg)", @@ -163,7 +183,7 @@ class MainWindow(QMainWindow): binarized_image = self.binarize_image2(resized_image) #binarized_image.save("C:/Users/RUPC/Desktop/3.jpg") - pixel_list=self.qimage_to_gray_list(binarized_image) + compress_pixels=self.qimage_to_gray_list(binarized_image) self.show_img(binarized_image) @@ -173,18 +193,28 @@ class MainWindow(QMainWindow): print("Exception occurred:", str(e)) def process_image(self): - global pixel_list global com_open - + global compress_pixels self.disable_all_widgets() if self.time_set()==False: self.enable_all_widgets() self.progress_bar.setValue(0) return False - add=0x1E320 + add=0x1E350 num=128 self.progress_bar.setValue(20) + # compress_pixels = [0] * 1024 + # + # for i in range(8): + # for j in range(128): + # sum=0 + # for k in range(8): + # sum=sum+(pixel_list[(k+i*8)*128+j]<