我正在尝试在Python中调整图像大小,然后使用调整大小的图像加载cocos2d精灵。但是,尝试初始化 cocos2d 精灵会导致找不到资源的错误。重现问题的示例代码:
from pathlib import Path
import cocos
from PIL import Image
im = Image.open("in.jpg")
im.thumbnail((600, 900))
im.save("out.jpg", "JPEG")
im.close()
file = Path("out.jpg")
if file.is_file():
print("File exists")
sprite = cocos.sprite.Sprite("out.jpg")
这会导致错误pyglet.resource.ResourceNotFoundException: Resource "out.jpg" was not found on the path. Ensure that the filename has the correct captialisation.
但是,输出为:
File exists
第二次运行它不会出错,因为out.jpg
是在上一次运行中创建的。删除out.jpg
并再次运行它会产生错误。
添加im.close()
并不能解决问题。
操作系统是Windows 10,Python 版本为 3.6.4。
原来是 pyglet 中用来加载资源的方法。我不得不重新索引图像。动态添加和 pyglet 的 images 目录中的文件创建现有图像的索引。有关答案,请参阅 https://stackoverflow.com/a/16438410/6350693。