使用Processing保存图像

3
我正在尝试在一段时间后保存一幅图片,问题是这幅图片的大小超过了屏幕显示范围,所以当我使用保存或saveFrame函数时,它只会保存在屏幕上可见的部分。有没有其他方法可以保存整个图片?
以下是我的代码:
PImage picture, pictureFilter, img;
int total, cont, current;
ArrayList<ArrayList<Position>> columns;
String[] fontList;

public class Position {
  public int x;
  public int y;
}

void setup() {
  fontList = PFont.list();
  picture = loadImage("DSC05920b.JPG");
  pictureFilter = loadImage("filtrePort2.jpg");
  frame.setResizable(true);
  size(picture.width, picture.height);
  columns = new ArrayList<ArrayList<Position>>();
  for(int i = 0; i < picture.width; i++) {
    ArrayList<Position> row = new ArrayList<Position>();
    for(int j = 0; j < picture.height; j++){
      Position p = new Position();
      p.x = i;
      p.y = j;
      row.add(p);
    }
    columns.add(row);
 }

 total = picture.width * picture.height;
 cont = total;
 current = 0;

 img = createImage(picture.width, picture.height, RGB);
}

float randomLetter() {
  float value = 23;
  boolean found = false;
  while(!found) {
    value = random(48, 122);
    if(value >48 && value <58) found = true;
    if(value >65 && value <91) found = true;
    if(value >97 && value <123) found = true;
  }
  return value;
}

void draw() {
  int x = int(random(0, columns.size()));
  ArrayList<Position> rows = columns.get(x);
  int y = int(random(0, rows.size()));
  Position p = rows.get(y);

  color c = pictureFilter.get(p.x, p.y);
  int r = (c >> 16) & 0xFF;    // Faster way of getting red(argb)
  if(r < 240) {
    PFont f = createFont(fontList[int(random(0,fontList.length))],random(5, 24),true);
    textFont(f);    
    fill(picture.get(p.x,p.y));
    char letter = (char) int(randomLetter());
    text(letter, p.x, p.y);
  }

  if(rows.size() == 1) {
    if(columns.size() == 1) {
      saveFrame("lol.jpg");
      columns.remove(x);
    } else {
      columns.remove(x);
    }
  } else {
    println(rows.size());
    rows.remove(y);
  }
  --cont;
  float percent = float(total-cont)/float(total)*100;
  if(int(percent) != current) {
    current = int(percent);
    save("image_" + current + ".jpg");
  }
  println("DONE: " + (total-cont) + "/" + total + " Progress: " + percent + "%");
}

这段代码做了很多事情,但是在最后检查百分比是否已经增加以保存图像的部分存在问题。


1
Kevin的意思是,使用picture.save("name.jpg")而不是save("blah,blah")版本... - v.k.
2个回答

3

您可以将此内容写入 PGraphics 上下文——也称为图形缓冲区。缓冲区可以根据需要调整大小,并且您可以选择是否在屏幕上绘制它。

// Create the buffer at the size you need, and choose the renderer
PGraphics pg = createGraphics(myImage.width, myImage.height, P2D);

// Wrap all your drawing functions in the pg context - e.g.
    PFont f = createFont(fontList[int(random(0,fontList.length))],random(5, 24),true);
    textFont(f);    
    pg.beginDraw();
      pg.fill(picture.get(p.x,p.y));
      char letter = (char) int(randomLetter());
      pg.text(letter, p.x, p.y);
   pg.endDraw();

// Draw your PG to the screen and resize the representation of it to the screen bounds
image(pg, 0, 0, width, height); // <-- this wont actually clip/resize the image

// Save it
pg.save("image_" + current + ".jpg");

3

PImage类包含一个save()函数,可以导出为文件。对于这样的问题,API 应该是您的第一站。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接