如何优化基于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
相关产品推荐
相关产品推荐

