简体   繁体   English

更改图像大小以使用 PySimpleGUI 显示

[英]Change size of image to display with PySimpleGUI

I'm trying to build a header/navigation bar with an image before the title.我正在尝试在标题前使用图像构建标题/导航栏。

Trying to replicate this mockup Image of mockup试图复制这个模型 模型的图像

The image is displaying like this (only show the center of the image, the headphone) How it looks图像是这样显示的(只显示图像的中心,耳机)它看起来如何

And I want to display like this How it should to looks我想像这样显示它看起来应该如何

here is my code这是我的代码

import PySimpleGUI as sg

# list columns of the view
header = [
    # show icon and name of the store
    [
        sg.Image(r'./images/lofigirl.png',size=(100,100)),
        sg.Text("App title", size=(10, 1), justification='center', font=("Helvetica", 12)),
        sg.Text("Option 1", size=(10, 1), justification='center', font=("Helvetica", 12)),
        sg.Text("Option 2", size=(10, 1), justification='center', font=("Helvetica", 12)),
    ]
]

content = [
    [
        sg.Text("Content", size=(60, 1), justification='center', font=("Helvetica", 25)),
    ]
]

# create window
window = sg.Window(
    'Window title',
    header + content,
    location=(100, 100)
)

# event loop
while True:
    event, values = window.read(timeout=20)
    
    if event == 'Exit' or event == sg.WIN_CLOSED:
        break

Resize by PIL通过 PIL 调整大小

from io import BytesIO
from pathlib import Path
from PIL import Image, UnidentifiedImageError
import PySimpleGUI as sg

def resize(image_file, new_size, encode_format='PNG'):
    im = Image.open(image_file)
    new_im = im.resize(new_size, Image.ANTIALIAS)
    with BytesIO() as buffer:
        new_im.save(buffer, format=encode_format)
        data = buffer.getvalue()
    return data

size = (500, 500)

layout = [
    [sg.Input(enable_events=True, readonly=True, expand_x=True, key='-FILENAME-'),
     sg.FileBrowse()],
    [sg.Image(size=size, background_color='green', key='-IMAGE-')],
    [sg.Text(expand_x=True, background_color='gray', key='Status')],
]
window = sg.Window('Title', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    elif event == '-FILENAME-':
        window['Status'].update('')
        image_file = values[event]
        if Path(image_file).is_file():
            try:
                window['-IMAGE-'].update(data=resize(image_file, size))
            except UnidentifiedImageError:
                window['Status'].update('Image file unidentified !')

window.close()

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

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