image - 使用 Processing 保存图像

标签 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 + "%");
}

代码做了很多事情,但它运行不佳的部分是在最后检查百分比是否已增加以保存图像时

最佳答案

您可以将其写入 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");

关于image - 使用 Processing 保存图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25031724/

相关文章:

css - 具有多种背景选择的响应图像

java - 如何在 JTextPane 中添加图片?

复制 yuv420 缓冲区

python - 当我通过 python 的 PIL 将 .tif 合并到 RGB 带时出错

html - 为什么父div仍然保持其子图像原始宽度的宽度?

php - 使用 PHP 将 JavaScript 数组保存到服务器

java - 检查文件是否已经保存java

javascript - 如何从客户端浏览器中的内容生成并提示保存文件?

python - 导入 CIFAR - 10 数据集到 R

c++ - 如何使用 opencv (c++) 分割光滑表面上的异常