使用Processing保存图像

时间:2014-07-30 08:07:47

标签: image image-processing save processing

我试图在一定时间后保存图像,问题是图像尺寸大于显示器,因此当我使用save或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 + "%");
}

代码做了很多事情,但是当我检查百分比是否已经增加以保存图像时,它不能正常运行的部分是最终的

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分: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");