简体   繁体   中英

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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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