图片 download/save 通过 QFileDialog

Image download/save through QFileDialog

我搜索了,但找不到解决方案,可怜我吧。我想下载一个图像并通过 PyQt5 的 QFileDialog 决定保存位置。

首先我下载带有模块请求的文件:

url = 'https://landscape.jpg'
r = requests.get(url)
image = r.content

...然后是代码的另一部分,QFileDialog:

options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(self,"Save Image","H:\Image","All Files (*)",options=options)

我应该在 QFileDialog 语句中的什么地方放置图像变量?

QFileDialog 只是一个小部件,它允许您获取文件名并且不知道如何保存任何文件,您应该做的是使用 open() 和 write() 将其保存为任何文件:

url = "https://landscape.jpg"
r = requests.get(url)
image = r.content

options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(
    self, "Save Image", r"H:\Image", "All Files (*)", options=options
)

if fileName:
    with open(fileName, "wb") as f:
        f.write(image)