【问题标题】:Pysimplegui resizing imagesPysimplegui 调整图像大小
【发布时间】:2022-08-10 00:16:40
【问题描述】:

我正在尝试调整 pysimplegui 中的图像大小,但是它会裁剪图像而不是调整大小。

我的图像元素写为:

ui.Image(\'{filename}\'), size=(50,50)))

结果如下:

虽然原来的样子:

我在其他地方看到过建议 PIL (link)。但是,这看起来比我喜欢的要长得多,并且想知道是否有更简单的方法可以做到这一点。

  • 尝试使用选项subsample 来减小图像大小。将大小除以该数字。 2=1/2、3=1/3、4=1/4 等,只接受整数。

标签: python user-interface pysimplegui


【解决方案1】:

和平 你好 要调整图像大小,您需要利用枕头库,但您还需要导入其他库以便在需要时将其转换为字节,这是一个示例:

import PIL.Image
import io
import base64

def resize_image(image_path, resize=None): #image_path: "C:User/Image/img.jpg"
    if isinstance(image_path, str):
        img = PIL.Image.open(image_path)
    else:
        try:
            img = PIL.Image.open(io.BytesIO(base64.b64decode(image_path)))
        except Exception as e:
            data_bytes_io = io.BytesIO(image_path)
            img = PIL.Image.open(data_bytes_io)

    cur_width, cur_height = img.size
    if resize:
        new_width, new_height = resize
        scale = min(new_height/cur_height, new_width/cur_width)
        img = img.resize((int(cur_width*scale), int(cur_height*scale)), PIL.Image.ANTIALIAS)
    bio = io.BytesIO()
    img.save(bio, format="PNG")
    del img
    return bio.getvalue()


ui.Image(key="-PHOTO-",size=(50,50) #after some change
elif event == "-IMG-": # the"-IMG-" key is in [ui.I(key="IMG",enable_events=True), ui.FileBrowse()]
        window['-PHOTO-'].update(data=resize_image(value["-IMG-"],resize=(50,50)))

我希望这有帮助

【讨论】:

    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2011-10-24
    相关资源
    最近更新 更多