简体   繁体   English

PySimpleGUI 显示 URL .JPG

[英]PySimpleGUI displaying a URL .JPG

I am using PySimpleGui .我正在使用PySimpleGui I want to have a local place holder image.jpg until the button is pressed to load in a URL based JPG.我想要一个本地占位符 image.jpg,直到按下按钮以加载基于 URL 的 JPG。

From searching around, I see people saying to use the PIL import, however it's a bit unclear currently to me, how to achieve this with my requirements.通过四处搜索,我看到人们说要使用PIL导入,但是目前对我来说有点不清楚,如何根据我的要求实现这一点。

I also am using Cloudscraper as whenever I would make URL request I would get blocked with a 403 error.我也在使用Cloudscraper,因为每当我发出 URL 请求时,我都会被 403 错误阻止。

Here is test code:下面是测试代码:

import PySimpleGUI as sg
from PIL import ImageTk, Image
from PySimpleGUI.PySimpleGUI import Column, HorizontalSeparator, In, VSeperator
from io import BytesIO
import io
import os
import cloudscraper

url = "https://cdnb.artstation.com/p/users/avatars/000/149/439/large/fe2b0699a4a2db62eb2814d44c81a0cf.jpg"
scrapper = cloudscraper.create_scraper(browser={'browser': 'firefox','platform': 'windows','mobile': False}).get(url).content
im = Image.open(scrapper)

print(im.format, im.size, im.mode)


imgViewer = [
    [sg.Image(Image.open(""), key="-ArtistAvatarIMG-")],
    [sg.Button("Get Cover Image", key="Clicky")]
]

layout = [
    [ 
        sg.Column(imgViewer)
    ],
]

window = sg.Window("Testing", layout)

def main():
    while True:
        event, values = window.read()
        if event == "EXIT" or event == sg.WIN_CLOSED:
            break
        if event == "Clicky":
            window['-ArtistAvatarIMG-'].Update(im)
    window.close()

if __name__ == "__main__":
    cDir = os.getcwd()
    main()

sg.Image only supports PNG and GIF formats and since the image is jpg you have to convert it to png and for this you can use PIL: sg.Image仅支持 PNG 和 GIF 格式,由于图像是 jpg,因此您必须将其转换为 png,为此您可以使用 PIL:

import io
import os

import PySimpleGUI as sg
from PySimpleGUI.PySimpleGUI import Column, HorizontalSeparator, In, VSeperator

from PIL import Image

import cloudscraper

url = "https://cdnb.artstation.com/p/users/avatars/000/149/439/large/fe2b0699a4a2db62eb2814d44c81a0cf.jpg"
jpg_data = (
    cloudscraper.create_scraper(
        browser={"browser": "firefox", "platform": "windows", "mobile": False}
    )
    .get(url)
    .content
)

pil_image = Image.open(io.BytesIO(jpg_data))
png_bio = io.BytesIO()
pil_image.save(png_bio, format="PNG")
png_data = png_bio.getvalue()

imgViewer = [
    [sg.Image(data=png_data, key="-ArtistAvatarIMG-")],
    [sg.Button("Get Cover Image", key="Clicky")],
]
# ...

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM