如何随机化图像大小

How can I randomize Image size

感谢之前帮助过我的人,我在生成名片作业上做了很多工作。

我想在处理过程中随机调整 9 张图像的大小,但似乎无法在 Internet 上找到有关如何执行此操作的好示例。图片大小为850x550,也是背景大小。

有谁知道好的且易于学习的教程吗?或者可以举个例子吗?

Processing's documentation on the image() method 涵盖了这一点。

我仍然给你写了一些框架代码来演示:

PImage img;
int w, h;
float scaleModifier = 1;

void setup() {
  size(800, 600);
  img = loadImage("bean.jpeg");
  w = img.width;
  h = img.height;
}

void draw() {
  background(0);
  image(img, 0, 0, w, h); // here is the important line
}

// Every click will resize the image
void mouseClicked() {
  scaleModifier += 0.1;
  if (scaleModifier > 1) {
    scaleModifier = 0.1;
  }
  
  w = (int)(img.width * scaleModifier);
  h = (int)(img.height * scaleModifier);
}

重要的是要知道以下内容:

image() 有 2 个签名:

  1. 图片(img,a,b)
  2. 图片(img, a, b, c, d)

以下情况适用:

  1. img => 您图片的 PImage
  2. a => 绘制图像的 x 坐标
  3. b => 绘制图像的 y 坐标
  4. c => 图像的宽度(如果它与图像的宽度不同,这意味着调整大小)
  5. d => 图像的高度(如果它与“真实”高度不同,也意味着调整大小)

玩得开心!

假设您已将图像存储在 PImage 对象中,image 你可以生成两个 random integers for the img_width and img_height of the image and then resize() the image using resize() 方法

int img_width = foor(random(min_value, max_value));
int img_height = floor(random(min_value, max_value));
    
image.resize(img_width, img_height); //this simple code resizes the image to any dimension

或者如果你想保持不变aspect ratio,那么你可以使用这种方法

//first set either of width or height to a random value
int img_width = floor(random(min_value, max_value));
    
//then proportionally calculate the other dimension of the image
float ratio = (float) image.width/image.height;
int img_height = floor(img_width/ratio);
    
image.resize(img_width, img_height);

您可以查看 this YouTube 播放列表以获取一些图像处理教程。