如何将 Pyglet 图像的区域放入 PIL 图像中?

发布于 2024-07-20 01:11:23 字数 456 浏览 5 评论 0原文

我的应用程序从视频中读取帧(使用 pyglet),提取一个“条带”(即区域 - 全宽,由 1 到 6 条视频线组成),然后将每个条带(附加它)粘贴到图像中,该图像稍后可以写出 *. png 文件。

问题是 Pyglet GL 图像存储在图形内存中,因此尺寸非常有限。 我当前笨拙的解决方法是将未来的 png 图像构建为小图块(即在 pyglet GL 大小限制内),当图块已满时,将其写出到 *png 文件,将其作为 PIL 图像文件读回,然后将平铺图像粘贴/附加到最终的 PIL Image * png 文件。

我认为应该可以将 pyglet-GL strip 转换为 PIL 图像友好格式,并将其直接粘贴/附加到最终的 PIL 图像中,从而不需要图块业务,这会使事情变得非常复杂并且对性能产生影响。

但我找不到如何将条带数据转换为可以粘贴到 PIL 图像中的表单。 见过很多请求以另一种方式重新转换,但我从来没有随波逐流。

My app reads frames from video (using pyglet), extracts a 'strip' (i.e. region - full width by 1 to 6 video lines), then pastes each strip (appends it) into an image that can later be written out an *.png file.

Problem is Pyglet GL images are stored in graphics memory, so are very limited re size.
My current klunky workaround is build up the to-be png image as small tiles (i.e. within the pyglet GL size limit), when the tile is full, write it out to a *png file, read that back in as a PIL Image file, then paste/append the tile image to the final PIL Image * png file.

I reckon it should be possible to convert the pyglet-GL strip to a PIL Image-friendly format, and paste/append that straight into the final PIL Image, thus having no need for the tile business, which greatly complicates things and has performance impact.

But I can't find how to get the strip data into a form that I can paste into a PIL Image.
Have seen many requests re converting the other way, but I've never run with the herd.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冧九 2024-07-27 01:11:23

我知道回答自己的问题是不好的形式,但我找到了一个可能对其他人有用的答案,所以这里是...

nextframe = source.get_next_video_frame() # 获取第一个视频帧
返回 None

#
### extract strip data, conver to PIL Image-friendly form, paste itn into travo Image
#
strip = nextframe.get_region(0, (vid_frame_h//2)-(strip_h//2), strip_w, strip_h) # Extract strip from this frame
strip_image_data = strip.get_image_data() 
strip_data = strip_image_data.get_data(strip_image_data.format, strip_image_data.pitch)
strip_Image = Image.frombuffer("RGB", (strip_w, strip_h), strip_data)
travo_image.paste(strip_Image, (0, travo_filled_to, strip_w, travo_filled_to + strip_h)) # paste strip image into travo_image 
travo_filled_to = travo_filled_to + strip_h     # update travo_filled pointer
nextframe = source.get_next_video_frame()       # get next video frame (EOF returns None)    

while nextframe != None:# 在“while nextframe != None:”的 EOF 末尾

I know it's bad form to reply to one's own question, but I worked out an answer which may be useful to others, so here it is...

nextframe = source.get_next_video_frame() # get first video frame
while nextframe != None:# returns None at EOF

#
### extract strip data, conver to PIL Image-friendly form, paste itn into travo Image
#
strip = nextframe.get_region(0, (vid_frame_h//2)-(strip_h//2), strip_w, strip_h) # Extract strip from this frame
strip_image_data = strip.get_image_data() 
strip_data = strip_image_data.get_data(strip_image_data.format, strip_image_data.pitch)
strip_Image = Image.frombuffer("RGB", (strip_w, strip_h), strip_data)
travo_image.paste(strip_Image, (0, travo_filled_to, strip_w, travo_filled_to + strip_h)) # paste strip image into travo_image 
travo_filled_to = travo_filled_to + strip_h     # update travo_filled pointer
nextframe = source.get_next_video_frame()       # get next video frame (EOF returns None)    

end of "while nextframe != None:"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文