保存框架以归档而无需渲染

发布于 2025-01-28 18:00:31 字数 643 浏览 3 评论 0原文

在处理中,如何修改现有草图将每个帧保存到图像文件,以免将其渲染到屏幕上?

我在 https://forum.processing.org/one/topic/how-can-i-save-calculate-frame-frame-without-displaying-them-them-in-real-real time.html 可以使用pgraphics将图形绘制到离线缓冲区中,这似乎是我追求的,但是我不确定如何轻松修改现有的草图以使用pgraphics < /代码>以这种方式进行类。

最重要的是,如果需要,我希望通过设置切换变量来呈现屏幕。

是否有一种简单的方法来“翻新”现有草图,以便它可以使用pgraphics而不是默认渲染方法(例如,在setup方法中),所以我我不喜欢t必须进入我的草图并更改每个绘制命令?

In Processing, how can I modify an existing sketch which saves each frame to an image file so that it does not render to the screen?

I saw in https://forum.processing.org/one/topic/how-can-i-save-calculate-frame-without-displaying-them-in-real-time.html that you can use PGraphics to draw graphics into an offline buffer, which seems like it would be what I am after, but I am not sure how I can easily modify my existing sketch to use the PGraphics class in that way.

On top of this, I would like the option to render to screen as well if needed by setting a toggle variable.

Is there an easy way of "retrofitting" an existing sketch so that it can use PGraphics instead of the default rendering method (say, in the setup method) so I don't have to go into my sketch and change every single draw command?

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

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

发布评论

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

评论(1

不羁少年 2025-02-04 18:00:31

您可以使用pgraphics对象,并像使用画布一样借鉴它。这是一个示例草图,没有绘制循环,它输出图像文件:

void setup() {
  PGraphics pg = createGraphics(150, 150);
  
  pg.beginDraw();
  pg.background(255);
  pg.stroke(0);
  pg.fill(color(0, 0, 200));
  pg.rect(50, 50, 50, 50);
  pg.endDraw();
  
  pg.save("fileName.png");
}

在我的草图文件夹中,它创建了此文件:

“保存映像”

关于另一个问题:我认为您不能将书面的拉动循环改造成一个类似的输出而无需在草图的窗口中渲染它而不会大量修改代码,但是...如果您的目标是在绘制到草图窗口或文件之间的选择,则可以在每个帧中绘制Pgraphics并选择是否您想显示Pgraphics或不依赖您的业务规则。您仍然有很多重构要做,但这是不可避免的。

这是比以前相同的例子,但是实施了这个想法:

boolean showFrame = true;
boolean saveFrame = false;

void setup() {
  size(150, 150);
}

void draw() {
  PGraphics pg = createGraphics(150, 150);
  
  pg.beginDraw();
  pg.background(255);
  pg.stroke(0);
  pg.fill(color(0, 0, 200));
  pg.rect(50, 50, 50, 50);
  pg.endDraw();
  
  if (showFrame) {image(pg, 0, 0);}
  if (saveFrame) {pg.save("fileName.png");}
}

希望这会有所帮助。玩得开心!

You can use a PGraphics object and draw on it just like you would with a canvas. Here's an example sketch with no draw loop which outputs an image file:

void setup() {
  PGraphics pg = createGraphics(150, 150);
  
  pg.beginDraw();
  pg.background(255);
  pg.stroke(0);
  pg.fill(color(0, 0, 200));
  pg.rect(50, 50, 50, 50);
  pg.endDraw();
  
  pg.save("fileName.png");
}

In my sketch's folder, it created this file:

Saved image

About the other question: I don't think that you can retrofit a written draw loop into a similar output without rendering it in the sketch's window without heavily modifying the code, BUT... if your goal is yo be able to choose between drawing to the sketch window or to a file, you can draw in PGraphics every frame and choose whether if you want to show the PGraphics or not depending on your business rules. You'll still have a lot of refactoring to do, but it's unavoidable.

Here's the same example than before, but implementing this idea:

boolean showFrame = true;
boolean saveFrame = false;

void setup() {
  size(150, 150);
}

void draw() {
  PGraphics pg = createGraphics(150, 150);
  
  pg.beginDraw();
  pg.background(255);
  pg.stroke(0);
  pg.fill(color(0, 0, 200));
  pg.rect(50, 50, 50, 50);
  pg.endDraw();
  
  if (showFrame) {image(pg, 0, 0);}
  if (saveFrame) {pg.save("fileName.png");}
}

Hope this helps. Have fun!

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