如何随机化图像大小

问题描述

感谢以前帮助过我的人,我已经成功地完成了我的商务名片分配工作。

我想在处理中随机调整9张图像的大小,但似乎无法在Internet上找到如何做到这一点的好例子。图片的尺寸为850x550,这也是背景尺寸。

有人知道一个很好且易于遵循的教程吗?或可以举个例子吗?

解决方法

Processing's documentation on the image() method对此进行了说明。

我仍然为您编写了一些框架代码来演示:

Resizing beans!

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,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. image(img,a,b)
  2. image(img,a,b,c,d)

在以下情况下适用:

  1. img =>您图片的PImage
  2. a => x坐标绘制图像的位置
  3. b => y坐标绘制图像的位置
  4. c =>图片的宽度(如果与图片的宽度不同,则表示需要调整大小)
  5. d =>图像的高度(如果与“实际”高度不同,则还意味着要调整大小)

玩得开心!

,

假设您已将图像存储在PImage对象image中 您可以为图像的img_widthimg_height生成两个random整数,然后使用resize()方法resize() image

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播放列表中查看一些图像处理教程。