You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化基于CFFI的Raylib图像切片函数?

图像切片函数优化问题

我正在用CFFI和Raylib开发一款小型游戏引擎,实现了一个图像切片功能函数,功能正常但运行速度极慢。原函数代码如下:

def slice(path, size):
    slices = []
    base_image = ffi.new("Image*", LoadImage(path.encode("ascii")))
    for y in range(0, base_image.height, size[1]):
        for x in range(0, base_image.width, size[0]):
            image = ffi.new("Image*", LoadImage(path.encode("ascii"))) # 问题出在这里
            ImageCrop(image, (x, y, x+size[0], y+size[1]))
            slices.append(image[0])
    return slices

我尝试了两种修改方式,但都无法正常运行:

方式一

def slice(path, size):
    slices = []
    base_image = ffi.new("Image*", LoadImage(path.encode("ascii")))
    for y in range(0, base_image.height, size[1]):
        for x in range(0, base_image.width, size[0]):
            image = base_image 
            ImageCrop(image, (x, y, x+size[0], y+size[1]))
            slices.append(image[0])
    return slices

方式二

import copy
def slice(path, size):
    slices = []
    base_image = ffi.new("Image*", LoadImage(path.encode("ascii")))
    for y in range(0, base_image.height, size[1]):
        for x in range(0, base_image.width, size[0]):
            image = copy.copy(base_image) 
            ImageCrop(image, (x, y, x+size[0], y+size[1]))
            slices.append(image[0])
    return slices

内容的提问来源于stack exchange,提问作者Ayoub Meziane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 10:54:51