【发布时间】:2020-10-21 14:02:46
【问题描述】:
感谢以前帮助过我的人,我在生成名片任务上做了很多工作。
我想在处理过程中随机调整 9 个图像的大小,但似乎在互联网上找不到一个很好的例子来说明如何做到这一点。图片大小为 850x550,也是背景大小。
有没有人知道一个很好且易于遵循的教程?或者可以举个例子吗?
【问题讨论】:
标签: image-processing processing image-resizing
感谢以前帮助过我的人,我在生成名片任务上做了很多工作。
我想在处理过程中随机调整 9 个图像的大小,但似乎在互联网上找不到一个很好的例子来说明如何做到这一点。图片大小为 850x550,也是背景大小。
有没有人知道一个很好且易于遵循的教程?或者可以举个例子吗?
【问题讨论】:
标签: image-processing processing image-resizing
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 个签名:
以下适用:
玩得开心!
【讨论】:
假设您已将图像存储在 PImage 对象中,image
您可以为图像的img_width 和img_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);
您可以在 YouTube 播放列表中查看this 以获取一些图像处理教程。
【讨论】: