【问题标题】:How can I randomize Image size如何随机化图像大小
【发布时间】:2020-10-21 14:02:46
【问题描述】:

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

我想在处理过程中随机调整 9 个图像的大小,但似乎在互联网上找不到一个很好的例子来说明如何做到这一点。图片大小为 850x550,也是背景大小。

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

【问题讨论】:

    标签: image-processing processing image-resizing


    【解决方案1】:

    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 => 图片的高度(如果它与“真实”高度不同,也意味着调整大小)

    玩得开心!

    【讨论】:

      【解决方案2】:

      假设您已将图像存储在 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);
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-13
        • 2017-12-07
        • 2018-07-27
        • 1970-01-01
        • 2021-12-08
        • 2013-02-04
        • 1970-01-01
        相关资源
        最近更新 更多