环境:
这是我的代码和结果:
import pyglet
images = []
textures = []
with_textures = True
count = 10
for x in range(count):
image = pyglet.image.load("big.png") # 2.1 Mb source 2400*2400px
images.append(image)
if with_textures:
texture_grid = pyglet.image.ImageGrid(image, 10, 10).get_texture_sequence()
textures.append(texture_grid)
# RES in htop result without textures
# count = 10 - 300Mb
# count = 20 - 553Mb
# count = 30 - 753Mb
# count = 40 - 973Mb
# ~23Mb just for each Image
# RES in htop result with textures
# count = 10 - 996Mb
# count = 20 - 1878Mb
# count = 30 - 2716Mb
# count = 40 - 3597Mb
# ~86Mb for Image and prepared grid
input("Press enter to exit")
PS:第一次将应用程序从pygame
重写为pyglet
,因为我没有考虑pygame
中事件循环的某些方面,现在我没有在我的用例中测试pyglet
库的资源使用情况。
我将ImageGrid
用于顶点的3d部分,以及pyglet.sprite.Sprite的2d部分。
在3D零件中使用的示例:
# texture_group is created once for each sprite sheet, same as texture_grid
texture_group = pyglet.graphics.TextureGroup(texture_grid, order_group)
...
tex_map = texture_grid[self.texture_grid_index].texture.tex_coords
tex_coords = ('t3f', tex_map)
self.entity = self.batch.add(
4, pyglet.gl.GL_QUADS,
texture_group,
('v3f', (x, y, z,
x_, y, z,
x_, y_, z,
x, y_, z)
),
tex_coords)
在2D零件中使用的示例:
pyglet.sprite.Sprite(img=texture_grid[self.texture_grid_index], x=0, y=0,
batch=self.batch, group=some_order_group)
据我了解,允许使用pyglet.image.CompressedImageData的大小为:
1 True
2 True
4 True
8 True
16 True
32 True
64 True
128 True
256 True
512 True
1024 True
2048 True
4096 True
但是无法从CompressedImageData
获取纹理:
big = pyglet.image.load("big.png") # 2048*2048
compressed_format = pyglet.graphics.GL_COMPRESSED_ALPHA
compressed_image = pyglet.image.CompressedImageData(
big.width, big.height, compressed_format, big.data)
compressed_image.texture # exception GLException: b'invalid enumerant'
在pyglet中尝试了所有可能的GL_COMPRESS:
allowed_formats = [x for x in dir(pyglet.graphics) if "GL_COMPRESSED_" in x]
big = pyglet.image.load("big.png") # 2048*2048
for form in allowed_formats:
compressed_image = pyglet.image.CompressedImageData(
big.width, big.height, form, big.data)
try:
compressed_image.texture
print("positive:", form) # 0 positive prints
except Exception:
pass
例外是:
Traceback (most recent call last):
File "<ipython-input-72-1b802ff07742>", line 7, in <module>
compressed_image.texture
File "/<venv>/lib/python3.6/site-packages/pyglet/image/__init__.py", line 410, in texture
return self.get_texture()
File "/<venv>/lib/python3.6/site-packages/pyglet/image/__init__.py", line 1351, in get_texture
len(self.data), self.data)
ctypes.ArgumentError: argument 3: <class 'TypeError'>: wrong type
pyglet中发生了什么
if self._have_extension():
glCompressedTexImage2DARB(texture.target, texture.level,
self.gl_format,
self.width, self.height, 0,
len(self.data), self.data)
ipdb:
> /<venv>/lib/python3.6/site-packages/pyglet/image/__init__.py(1349)get_texture()
1348 import ipdb;ipdb.set_trace()
-> 1349 glCompressedTexImage2DARB(texture.target, texture.level,
1350 self.gl_format,
ipdb> glCompressedTexImage2DARB
<_FuncPtr object at 0x7fca957ee1d8>
ipdb> texture.target
3553
ipdb> texture.level
0
ipdb> self.gl_format
'GL_COMPRESSED_TEXTURE_FORMATS_ARB'
ipdb> self.width
2048
ipdb> self.height
2048
ipdb> len(self.data)
16777216
ipdb> type(self.data)
<class 'bytes'>
答案 0 :(得分:3)
#1的答案很简单:PNG是一种压缩格式。解压缩后,即使磁盘大小为2 MB,2400×2400映像也只需要在RAM中占用2400×2400×32bit = 23040000字节。
解决此问题的可能方法很多,但所有这些都归结为在内存需求,图像质量和访问速度之间找到适当的折衷。
例如,在Pyglet中,有一个名为CompressedImageData的类,当您使用OpenGL进行渲染时,该类允许您使用GPU内置的纹理压缩。如果不是这样,您可能会坚持在软件中实现这些方法之一,因为PNG压缩最不适合快速像素访问。 Here您可以找到有关GPU纹理压缩的更多信息。
作为一种快速而肮脏的解决方法,您可以尝试将图像中的颜色数量减少到≤256,并使用调色板。那将立即使内存增加4倍。