将图像作为临时文件下载并将其设置为PixMap

发布于 2025-01-28 20:22:50 字数 851 浏览 1 评论 0原文

我正在下载YouTube视频缩略图并将其保存为tempfile。然后,我从那个tempfile中得到一个pixmap,后来我可以将其设置为标签,它将显示为YouTube视频缩略图。 我的问题是,相同的方法对YouTube #Shorts缩略图不起作用,它不会引发任何错误,但标签将是空的。我不知道为什么

这是YouTube视频缩略图图像URL的一个示例:

url =“ https://i.ytimg.com/vi/mtn1ynol46q/hqdefault.jpg?sqp = -oaymwejcoadei4cfryequln> LF_ZTYFJ1I9VW9HQEM5D5D5ZL9Q “

这是YouTube短路缩略图图像URL:

url =“ https://i.ytimg.com/vi/vt_7ubaf3vc/2.jpg”

这是代码,适用于YouTube视频缩略图,但不适合短裤,

from PyQt5.QtGui import QIcon, QPixmap
from urllib.request import urlopen
import base64
import tempfile

url = .......
image = base64.b64encode(urlopen(url).read()).decode("ascii")
imgdata = base64.b64decode(image)
with tempfile.NamedTemporaryFile(mode="wb") as img:
     img.write(imgdata)
     pixmap = QPixmap(img.name)

我该如何使其适用于YouTube短裤图像URL?

I'm downloading a YouTube video thumbnail and saving it as a tempfile. I'm then getting a pixmap out of that tempfile and later I can set it to a label, it will appear for the YouTube video thumbnail.
My problem is that the same method won't work for YouTube #shorts thumbnail, it wont throw any error but the label will be empty. I dont know why

This is an example of a YouTube video thumbnail image url:

url = "https://i.ytimg.com/vi/MtN1YnoL46Q/hqdefault.jpg?sqp=-oaymwEjCOADEI4CSFryq4qpAxUIARUAAAAAGAElAADIQj0AgKJDeAE=&rs=AOn4CLDlF_ZTyfJ1i9Vw9HqEm5d5D5zL9Q"

This is an example of a YouTube shorts thumbnail image url:

url = "https://i.ytimg.com/vi/VT_7UBAf3Vc/2.jpg"

This is the code that works for the YouTube video thumbnails but not for the shorts

from PyQt5.QtGui import QIcon, QPixmap
from urllib.request import urlopen
import base64
import tempfile

url = .......
image = base64.b64encode(urlopen(url).read()).decode("ascii")
imgdata = base64.b64decode(image)
with tempfile.NamedTemporaryFile(mode="wb") as img:
     img.write(imgdata)
     pixmap = QPixmap(img.name)

How can I make it work for YouTube shorts image URLs too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵情客 2025-02-04 20:22:50

使用urllib.parse模块从URL末端删除参数/args。

from PyQt5.QtGui import QIcon, QPixmap
from urllib.request import urlopen
from urllib.parse import urlparse, urlunparse  # add parsing funcs
import base64
import tempfile

url = .......
url = urlunparse(list(urlparse(url)[:3]) + ['','',''])  # try removing the arguments
image = base64.b64encode(urlopen(url).read()).decode("ascii")
imgdata = base64.b64decode(image)
with tempfile.NamedTemporaryFile(mode="wb") as img:
     img.write(imgdata)
     pixmap = QPixmap(img.name)

use the urllib.parse module to remove the parameters/args from the end of the url.

from PyQt5.QtGui import QIcon, QPixmap
from urllib.request import urlopen
from urllib.parse import urlparse, urlunparse  # add parsing funcs
import base64
import tempfile

url = .......
url = urlunparse(list(urlparse(url)[:3]) + ['','',''])  # try removing the arguments
image = base64.b64encode(urlopen(url).read()).decode("ascii")
imgdata = base64.b64decode(image)
with tempfile.NamedTemporaryFile(mode="wb") as img:
     img.write(imgdata)
     pixmap = QPixmap(img.name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文