只需5分钟,我用AI写了一个图像处理软件!

如题,使用ChatGPT轻松创建一个简易的图像处理软件,如果你有更多的时间,可以使得软件更加的复杂精巧,有更多的处理功能和好看的UI,这些现在通过AI都是可以实现的。

先展示所实现图像处理软件的结果示意图,再展示使用ChatGPT创建的过程。

1 结果示意图

图片
原图

图片
灰度化处理

图片
模糊化处理

图片
边缘检测

2 ChatGPT创作过程

(1)我:用python开发一个软件

        ChatGPT:

当然可以!我们可以先明确一下要开发的软件类型和功能。您有具体的软件开发需求或想法吗?比如:

桌面应用程序 (如文字处理器、文件管理器)
Web 应用程序 (如在线工具、社交平台)
数据分析工具 (如数据可视化、统计分析)
游戏开发
自动化脚本 (如文件处理、网络爬虫)
如果您还没有具体想法,也可以告诉我您感兴趣的领域,我可以给您一些建议!

(2)我:桌面应用程序,目前主要用来处理图像

        ChatGPT:

了解了!我们可以用 Python 和 PyQt 库来开发一个图像处理的桌面应用程序。这个应用程序可以包括以下功能:

加载和保存图像:从本地文件系统加载图像,并允许用户将处理后的图像保存到本地。
基本图像处理功能:例如裁剪、旋转、调整亮度/对比度、滤镜应用等。
高级图像处理:例如边缘检测、模糊化、锐化等,使用 OpenCV 等库来实现。
实时预览:在用户调整参数时实时预览效果。
我们可以先从一个基本的框架开始,逐步添加功能。以下是一个使用 PyQt5 和 OpenCV 的基本图像处理应用程序的骨架代码:

1. 安装必要的库
首先,确保安装了 PyQt5 和 OpenCV。您可以使用 pip 安装它们:
pip install PyQt5 opencv-python

2. 创建基本应用程序框架
接下来,我们可以编写一个简单的应用程序来加载和显示图像。
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QAction, QFileDialog
from PyQt5.QtGui import QPixmap
import cv2
import numpy as np

class ImageProcessor(QMainWindow):
def __init__(self):
super().__init__()

self.setWindowTitle('图像处理应用程序')
self.setGeometry(100, 100, 800, 600)

# 创建菜单
self.create_menu()

# 设置主标签来显示图像
self.label = QLabel(self)
self.setCentralWidget(self.label)

def create_menu(self):
# 创建菜单栏
        menubar = self.menuBar()

# 文件菜单
        file_menu = menubar.addMenu('文件')

# 打开图像动作
        open_action = QAction('打开', self)
        open_action.triggered.connect(self.open_image)
        file_menu.addAction(open_action)

# 退出动作
        exit_action = QAction('退出', self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

def open_image(self):
# 打开文件对话框选择图像
        options = QFileDialog.Options()
        file_path, _ = QFileDialog.getOpenFileName(self, '打开图像', '', '图像文件 (*.png *.jpg *.bmp);;所有文件 (*)', options=options)

if file_path:
# 使用OpenCV读取图像
            image = cv2.imread(file_path)
self.display_image(image)

def display_image(self, image):
# 将OpenCV图像转换为Qt格式
        height, width, channel = image.shape
        bytes_per_line = 3 * width
        q_img = QPixmap(QPixmap.fromImage(QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888).rgbSwapped()))

# 在标签中显示图像
self.label.setPixmap(q_img)
self.label.adjustSize()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageProcessor()
    window.show()
    sys.exit(app.exec_())

3. 运行应用程序
运行上述代码后,将启动一个基本的图像处理应用程序,用户可以从菜单中选择“打开”来加载图像。图像将显示在应用程序窗口中。

4. 后续步骤
接下来,我们可以逐步添加更多功能,比如图像的基本处理(裁剪、旋转等)和高级处理(滤镜、边缘检测等)。您可以告诉我希望优先添加哪些功能,我可以帮助您实现这些功能的代码!

您觉得这个初始框架怎么样?需要做任何调整或添加吗?

(3)我:测试了下,不能打开有中文的路径,你修改下,完整的代码发我

        ChatGPT:

好的!看起来问题是由于文件路径中包含中文字符,导致 OpenCV 无法正确读取文件。为了解决这个问题,我们可以使用 Python 的 os 模块来获取文件路径并将其转换为适合 OpenCV 读取的格式。

这里是修改后的代码。此版本通过使用 cv2.imdecode() 方法读取图像数据,以便更好地处理包含中文字符的文件路径。
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QAction, QFileDialog
from PyQt5.QtGui import QPixmap, QImage
import cv2
import numpy as np

class ImageProcessor(QMainWindow):
def __init__(self):
super().__init__()

self.setWindowTitle('图像处理应用程序')
self.setGeometry(100, 100, 800, 600)

        # 创建菜单
self.create_menu()

        # 设置主标签来显示图像
self.label = QLabel(self)
self.setCentralWidget(self.label)

def create_menu(self):
        # 创建菜单栏
menubar = self.menuBar()

        # 文件菜单
file_menu = menubar.addMenu('文件')

        # 打开图像动作
open_action = QAction('打开', self)
open_action.triggered.connect(self.open_image)
file_menu.addAction(open_action)

        # 退出动作
exit_action = QAction('退出', self)
exit_action.triggered.connect(self.close)
file_menu.addAction(exit_action)

def open_image(self):
        # 打开文件对话框选择图像
options = QFileDialog.Options()
file_path, _ = QFileDialog.getOpenFileName(self, '打开图像', '', '图像文件 (*.png *.jpg *.bmp);;所有文件 (*)', options=options)

if file_path:
print(f"Selected file path: {file_path}")  # 打印文件路径以调试

            # 使用imread_unicode方法读取图像
image = self.imread_unicode(file_path)

if image is None:
print("Error: Unable to load image. Please check the file path and format.")
else:
self.display_image(image)

def imread_unicode(self, file_path):
        # 使用numpy和OpenCV读取支持中文路径的图像
image = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), cv2.IMREAD_COLOR)
return image

def display_image(self, image):
        # 将OpenCV图像转换为Qt格式
height, width, channel = image.shape
bytes_per_line = 3 * width
q_img = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
pixmap = QPixmap.fromImage(q_img)

        # 在标签中显示图像
self.label.setPixmap(pixmap)
self.label.adjustSize()

if __name__ == '__main__':
app = QApplication(sys.argv)
window = ImageProcessor()
window.show()
sys.exit(app.exec_())

(4)我:问题解决,但是希望窗口不要根据图像大小而变化,软件窗口固定,当然可以最大和最小化

        ChatGPT:

很高兴听到问题解决了!要让软件的窗口大小固定而不根据图像大小变化,我们需要调整窗口的设置,确保窗口在初始化时有一个固定的大小,并且在显示图像时不会改变这个大小。

修改后的代码
下面是调整后的代码,其中窗口大小固定,并且图像在标签(QLabel)中居中显示而不改变窗口的大小。

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QAction, QFileDialog, QVBoxLayout, QWidget, QScrollArea
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
import cv2
import numpy as np

class ImageProcessor(QMainWindow):
def __init__(self):
super().__init__()

self.setWindowTitle('图像处理应用程序')
self.setFixedSize(800, 600)  # 设置固定窗口大小

# 创建主窗口和布局
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.layout = QVBoxLayout(self.central_widget)

# 设置滚动区域以防图像过大
self.scroll_area = QScrollArea(self)
self.label = QLabel(self)
self.scroll_area.setWidget(self.label)
self.scroll_area.setWidgetResizable(True)
self.layout.addWidget(self.scroll_area)

# 创建菜单
self.create_menu()

def create_menu(self):
# 创建菜单栏
        menubar = self.menuBar()

# 文件菜单
        file_menu = menubar.addMenu('文件')

# 打开图像动作
        open_action = QAction('打开', self)
        open_action.triggered.connect(self.open_image)
        file_menu.addAction(open_action)

# 退出动作
        exit_action = QAction('退出', self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

def open_image(self):
try:
# 打开文件对话框选择图像
            options = QFileDialog.Options()
            file_path, _ = QFileDialog.getOpenFileName(self, '打开图像', '', '图像文件 (*.png *.jpg *.bmp);;所有文件 (*)', options=options)

if file_path:
                print(f"Selected file path: {file_path}")  # 打印文件路径以调试

# 使用imread_unicode方法读取图像
                image = self.imread_unicode(file_path)

if image is None:
                    print("Error: Unable to load image. Please check the file path and format.")
else:
self.display_image(image)
        except Exception as e:
            print(f"An error occurred: {e}")

def imread_unicode(self, file_path):
try:
# 使用numpy和OpenCV读取支持中文路径的图像
            image_data = np.fromfile(file_path, dtype=np.uint8)
            image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
return image
        except Exception as e:
            print(f"Error loading image: {e}")
return None

def display_image(self, image):
try:
# 将OpenCV图像转换为Qt格式
            height, width, channel = image.shape
            bytes_per_line = 3 * width
            q_img = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
            pixmap = QPixmap.fromImage(q_img)

# 在标签中显示图像
self.label.setPixmap(pixmap)
self.label.setAlignment(Qt.AlignCenter)  # 居中显示
self.label.adjustSize()
        except Exception as e:
            print(f"Error displaying image: {e}")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageProcessor()
    window.show()
    sys.exit(app.exec_())

代码说明
固定窗口大小:通过 self.setFixedSize(800, 600) 方法来设置应用程序窗口的初始大小为 800x600。窗口大小在启动后是固定的,不会随图像大小改变,但用户可以通过最大化或最小化调整窗口。

添加滚动区域:

使用 QScrollArea 允许图像滚动查看,防止图像过大溢出窗口。
设置 self.scroll_area.setWidgetResizable(True) 使得滚动区域可以调整大小而不改变主窗口的大小。
图像居中显示:设置 self.label.setAlignment(Qt.AlignCenter) 使得图像在标签中居中显示。

测试步骤
运行上述代码。
打开包含中文字符的图像文件路径或任意图像文件。
确认窗口大小不会改变,图像在窗口中显示且可以通过滚动查看完整图像。
这样应该满足您对固定窗口大小的需求,同时也提供了一个可用的用户界面来处理不同大小的图像。如果有其他需求或问题,请告诉我!

(5)我:问题解决,然后希望在此基础上,界面一侧有可以选择选项的按钮,进行不同的图像处理功能

        ChatGPT:​​​​​​​

很高兴问题解决了!接下来,我们可以在应用程序界面的侧边添加一个按钮面板,以便选择不同的图像处理功能。为了实现这一点,我们可以使用 QPushButton 或 QComboBox 来提供图像处理选项,并在用户选择不同的选项时应用相应的处理。

下面是一个示例代码,展示如何在现有应用程序的基础上添加一个侧边面板,并实现图像处理功能:

import sys
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QAction, QFileDialog, QVBoxLayout, QWidget, QScrollArea, QPushButton, QComboBox, QHBoxLayout
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
import cv2
import numpy as np

class ImageProcessor(QMainWindow):
def __init__(self):
super().__init__()

self.setWindowTitle('图像处理应用程序')
self.setFixedSize(1200, 600)  # 设置固定窗口大小

# 创建主窗口和布局
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.main_layout = QHBoxLayout(self.central_widget)  # 使用水平布局

# 创建侧边面板
self.sidebar = QWidget(self)
self.sidebar.setFixedWidth(200)
self.sidebar_layout = QVBoxLayout(self.sidebar)
self.main_layout.addWidget(self.sidebar)

# 创建图像显示区域
self.scroll_area = QScrollArea(self)
self.label = QLabel(self)
self.scroll_area.setWidget(self.label)
self.scroll_area.setWidgetResizable(True)
self.main_layout.addWidget(self.scroll_area)

# 添加处理选项按钮
self.processing_options = QComboBox(self)
self.processing_options.addItems(["无处理", "灰度化", "模糊", "边缘检测"])
self.processing_options.currentIndexChanged.connect(self.apply_processing)
self.sidebar_layout.addWidget(self.processing_options)

# 添加打开图像按钮
self.open_button = QPushButton('打开图像', self)
self.open_button.clicked.connect(self.open_image)
self.sidebar_layout.addWidget(self.open_button)

# 创建菜单
self.create_menu()

self.original_image = None  # 存储原图
self.current_image = None  # 存储当前处理图像

def create_menu(self):
# 创建菜单栏
        menubar = self.menuBar()

# 文件菜单
        file_menu = menubar.addMenu('文件')

# 退出动作
        exit_action = QAction('退出', self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

def open_image(self):
try:
# 打开文件对话框选择图像
            options = QFileDialog.Options()
            file_path, _ = QFileDialog.getOpenFileName(self, '打开图像', '', '图像文件 (*.png *.jpg *.bmp);;所有文件 (*)', options=options)

if file_path:
                print(f"Selected file path: {file_path}")  # 打印文件路径以调试

# 使用imread_unicode方法读取图像
self.original_image = self.imread_unicode(file_path)

if self.original_image is None:
                    print("Error: Unable to load image. Please check the file path and format.")
else:
self.current_image = self.original_image.copy()  # 初始化当前图像
self.display_image(self.current_image)
        except Exception as e:
            print(f"An error occurred: {e}")

def imread_unicode(self, file_path):
try:
# 使用numpy和OpenCV读取支持中文路径的图像
            image_data = np.fromfile(file_path, dtype=np.uint8)
            image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
return image
        except Exception as e:
            print(f"Error loading image: {e}")
return None

def display_image(self, image):
try:
# 处理图像的通道数
if len(image.shape) == 2:  # 灰度图像
                height, width = image.shape
                bytes_per_line = width
                q_img = QImage(image.data, width, height, bytes_per_line, QImage.Format_Grayscale8)
            elif len(image.shape) == 3:  # 彩色图像
                height, width, channel = image.shape
                bytes_per_line = 3 * width
                q_img = QImage(image.data, width, height, bytes_per_line, QImage.Format_RGB888).rgbSwapped()
else:
                raise ValueError("Unsupported image format")

            pixmap = QPixmap.fromImage(q_img)

# 在标签中显示图像
self.label.setPixmap(pixmap)
self.label.setAlignment(Qt.AlignCenter)  # 居中显示
self.label.adjustSize()
        except Exception as e:
            print(f"Error displaying image: {e}")

def apply_processing(self):
if self.original_image is None:
return

        option = self.processing_options.currentText()
if option == "无处理":
self.current_image = self.original_image.copy()  # 恢复原图
else:
self.current_image = self.original_image.copy()  # 复制原图以进行处理
if option == "灰度化":
self.current_image = cv2.cvtColor(self.current_image, cv2.COLOR_BGR2GRAY)
            elif option == "模糊":
self.current_image = cv2.GaussianBlur(self.current_image, (15, 15), 0)
            elif option == "边缘检测":
self.current_image = cv2.Canny(self.current_image, 100, 200)

# 将处理后的图像显示在窗口中
self.display_image(self.current_image)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ImageProcessor()
    window.show()
    sys.exit(app.exec_())

代码说明
窗口布局:

使用 QHBoxLayout 创建水平布局,将侧边面板和图像显示区域分开。
在侧边面板中添加了一个下拉框 (QComboBox) 供用户选择不同的图像处理功能,并添加了一个按钮 (QPushButton) 以打开图像文件。
图像处理功能:

在 apply_processing 方法中,根据用户在下拉框中选择的处理选项对图像进行处理。支持的选项包括“无处理”、“灰度化”、“模糊”和“边缘检测”。
处理后的图像将显示在窗口中。
侧边面板:

侧边面板使用 QWidget 和 QVBoxLayout 实现固定宽度,并在其中添加处理选项和打开图像按钮。
如何使用
运行上述代码。
在应用程序的侧边面板中选择图像处理功能。
点击“打开图像”按钮选择图像文件。
选择不同的处理选项,观察图像处理效果。
这样,您就可以在图像处理应用程序中添加更多的功能和选项,用户可以通过简单的界面操作选择不同的图像处理功能。如果还有其他需求或问题,请告诉我!

至此,将完整代码copy运行,完美达成目的,有兴趣的话你也可以去试着创建符合你自己需求的软件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隋工AI

感谢您的认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值