首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从内存缓冲区创建pySDL2映像

如何从内存缓冲区创建pySDL2映像
EN

Stack Overflow用户
提问于 2021-11-11 20:57:34
回答 1查看 94关注 0票数 0

我有一个pySimpleGUI程序,它从包含JPEG图像的内存缓冲区创建图像。

代码语言:javascript
运行
复制
from PIL import ImageTk
image = ImageTk.PhotoImage( data=buf) # PhotoImage will detect a JPEG format
self.win["-IMAGE-'].update( data=image)

我想将我的应用程序转换为pySDL2。

由于pySimpleGUI和pySDL都是基于PIL的,所以我希望可以轻松地进行转换,但我无法找到从缓冲区创建SDL映像的方法。

有办法吗?

EN

回答 1

Stack Overflow用户

发布于 2022-05-11 18:17:56

不确定这是可行的,未经测试,但可能会使你走上正确的轨道。如果你想要更多的细节,请访问https://rubato.app/。图像类的完整源代码就在那里。

代码语言:javascript
运行
复制
@staticmethod
def from_surface(surface: sdl2.surface.SDL_Surface) -> "Image":
    """
    Creates an image from an SDL surface.

    Args:
        surface: the surface to create the image from.

    Returns:
        The created image.
    """
    # untested
    image = Image()
    image.image = surface
    return image

@staticmethod
def from_buffer(buffer: bytes) -> "Image":
    """
    Creates an image from a buffer.

    Args:
        buffer: bytes containing the image data.

    Returns:
        The image created from the buffer.
    """
    # untested
    rw = sdl2.SDL_RWFromMem(buffer, len(buffer))
    surface_temp = sdl2.sdlimage.IMG_Load_RW(rw, 1)

    if surface_temp is None:
        raise Exception(sdl2.sdlimage.IMG_GetError())

    surface = sdl2.SDL_ConvertSurfaceFormat(surface_temp, sdl2.SDL_PIXELFORMAT_RGBA8888, 0).contents
    sdl2.SDL_FreeSurface(surface_temp)

    return Image.from_SDL_Surface(surface)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69934827

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档