带 transloadit 的旋转圆圈。是否可以使用水印或图像旋转?

Spinning circle with transloadit. Is it possible with watermark or image rotation?

我正在尝试用艺术品作为蒙版来实现一个旋转的圆圈。 据我所见,无法使用移动水印或自动旋转图像。 transloadit 有可能吗?

结果应该是 "vinyl" 旋转。

这个问题回答起来相当复杂,但是使用 Transloadit 可以很好地解决这个问题。我将使用 python 主要针对 OpenCV 来回答它,但当然您可以使用您喜欢的语言 - 我会尽量使其与语言无关。 我使用了 4 种不同的模板来获得我们想要的结果,以及一些本地 python 魔法将所有内容联系在一起 - 在这里。

  1. 首先我们需要调整图像的大小,使其适合黑胶唱片。
{
  "steps": {
    ":original": {
      "robot": "/upload/handle"
    },
    "resize": {
      "use": ":original",
      "robot": "/image/resize",
      "format": "png",
      "resize_strategy": "fillcrop",
      "width": 350,
      "height": 350,
      "imagemagick_stack": "v2.0.7"
    }
  }
}
  1. 现在,我们要使用 OpenCV 和 NumPy 屏蔽此图像,如下所示:
# Mask the image
# Reads the input image
img = cv2.imread(img_path)
# Creates a mask with the same size as the image
mask = np.zeros(img.shape, dtype=np.uint8)
# Creates a white circle in the centre
mask = cv2.circle(mask, (175, 175), 175, (255, 255, 255), -1)
# Makes a small whole in the centre of the mask
mask = cv2.circle(mask, (175, 175), 20, (0, 0, 0), -1)
result = cv2.bitwise_and(img, mask)

这将拍摄一张图像,并为其创建一个应该看起来像甜甜圈的遮罩。

然后,在图像和蒙版之间使用按位与运算,最终得到原始图像的千篇一律

  1. 但我们仍然需要删除黑色背景 - 这就是我们使用此模板的目的:
{
  "steps": {
    ":original": {
      "robot": "/upload/handle"
    },
    "trimmed": {
      "use": ":original",
      "robot": "/image/resize",
      "alpha": "Activate",
      "type": "TrueColor",
      "transparent": "0,0,0",
      "imagemagick_stack": "v2.0.7"
    }
  }
}

这只会使所有黑色像素透明。

  1. 我们现在可以使用 Transloadit's watermark feature 将此图像叠加到我们的黑胶唱片上
{
  "steps": {
    ":original": {
      "robot": "/upload/handle"
    },
    "watermark": {
      "use": ":original",
      "robot": "/image/resize",
      "watermark_size": "33%",
      "watermark_position": "center",
      "imagemagick_stack": "v2.0.7"
    }
  }
}
  1. 现在,剩下的就是让它旋转了。我们可以做的是创建 60 帧,然后旋转图像,然后使用 /video/merge 机器人 - 将它们拼接成一个无缝的 GIF。
{
  "steps": {
    ":original": {
      "robot": "/upload/handle"
    },
    "rotate_image": {
      "use": ":original",
      "robot": "/image/resize",
      "rotation": "${file.basename}",
      "resize_strategy": "crop",
      "imagemagick_stack": "v2.0.7"
    },
    "animated": {
      "robot": "/video/merge",
      "use": {
        "steps": [
          {
            "name": "rotate_image",
            "as": "image"
          }
        ],
        "bundle_steps": true
      },
      "result": true,
      "ffmpeg_stack": "v4.3.1",
      "ffmpeg": {
        "f": "gif",
        "pix_fmt": "rgb24"
      }
    }
  }
}

在这里,我使用图像的名称来确定将其旋转多少度 - 因此,当我将文件上传到机器人时,我将使用此 [=59= 根据其在数组中的索引来命名图像]代码:

# Now we make a list of images that represent each frame
no_of_frames = 60
assembly = tl.new_assembly({'template_id': [SPINNING_VINYL_TEMPLATE_ID]})
directory = 'Assets/Frames/{image}'.format(image=img_name)
# Length of our animation in seconds
length = 2

for i in range(no_of_frames):
    if not os.path.exists(directory):
        os.mkdir(directory)
    # Creates an image based on the index in the animation
    # We pass this to the robot so it knows how many degrees to rotate the image by
    location = '{directory}/{index}.png'.format(directory=directory, index=round(i*360/no_of_frames))
    cv2.imwrite(location, finished_vinyl)
    assembly.add_file(open(location, 'rb'))

This is my end result