【qt】打开图像、保存图像

时间:2024-04-01 19:24:43

c++qt
打开图像

void Widget::on_pushButton_clicked()
{

    QString m_imgPath = QFileDialog::getOpenFileName(this, tr("Open Image"), QCoreApplication::applicationDirPath(), tr("*.png *.jpg"));   //打开图片文件,选择图片

    QImage oriImage;
    QImage enhanceImage;
    if(oriImage.load(m_imgPath))
    {
        qDebug() << "Image loaded successfully!";
    }
    else
    {
        qDebug() << "Failed to load image!";
        return;
    }
}

pyqt
打开图像

    @pyqtSlot()
    def on_btn_input_path_clicked(self):
        input_path, _ = QFileDialog.getOpenFileName(self, "加载图像", "", "Image Files (*.png *.jpg *.tif)")
        if input_path == '':
            return

保存图像

    @pyqtSlot()
    def on_btn_export_img_clicked(self):
        os.makedirs('save_results', exist_ok=True)
        name = os.path.basename(self.input_path).split('.')[0] + '_detect'
        save_path, _ = QFileDialog.getSaveFileName(self, "导出图像", f"{name}",
                                                   "JPEG Files (*.jpg *.jpeg);;PNG Files (*.png);;")
        if save_path:
            self.pixmap.save(save_path)