如何以编程方式将图像裁剪为非矩形形状?

发布于 2024-08-19 01:45:32 字数 419 浏览 4 评论 0原文

我想将矩形图像裁剪为非矩形形状。我意识到,如果你完全按照字面意思理解,那是不可能的。我想要最终得到的是图像 X,在透明背景上裁剪为形状 Y。

举例来说,我想拍摄一张爱达荷州旗的照片并将其裁剪为爱达荷州的形状。我想我会做这样的事情:

  1. 创建一个图像,该图像具有爱达荷州形状的不透明像素,其他地方的透明像素
  2. 读取并存储此爱达荷州图像的某种位图
  3. 对于爱达荷州图像中的每个不透明像素位置,复制爱达荷州旗图像中的相应像素并将其放置在空白的透明画布上

步骤 1 显然是手动完成的,但其余部分将以编程方式完成。我认为总体上我的想法是正确的,但我不知道如何处理具体细节。有人能指出我正确的方向吗?

就实现技术而言,我是一名 PHP 人员,因此使用 gdLibrary 或与 PHP 配合使用的东西可能是我的最佳方法。

I want to crop a rectangular image to a non-rectangular shape. I realize that if you take that completely literally, it's not possible. What I want to end up with is image X, cropped to shape Y, on a transparent background.

Let's say for example that I want to take a picture of the Idaho flag and crop it to the shape of the state of Idaho. I imagine I would do something like this:

  1. Create an image that has opaque pixels for the shape of Idaho, transparent pixels everywhere else
  2. Read and store some kind of bitmap for this Idaho state image
  3. For each opaque pixel location in the Idaho state image, copy the corresponding pixel from the Idaho state flag image and place it on a blank, transparent canvas

Step 1 would obviously be done manually, but the rest would be done programatically. I think I have the right idea in general but I don't know how I'd approach the specifics. Can anyone point me in the right direction?

As far as implementation technology goes, I'm a PHP guy, so using gdLibrary or something that works with PHP would probably be the best way for me to go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕花眠 2024-08-26 01:45:32

我认为通过简单地添加位掩码或 Alpha 通道会更容易。在这种情况下,您可以使用形状的负片蒙版图像,然后将其作为蒙版应用到常规图像上,然后以透明格式保存。我实际上从未使用 GD 或 ImageMagick 这样做过,但我认为它可以像 Jerry 所建议的那样可用。

实际上,这是一篇类似的博客文章,可能会有所帮助: http://about.phalacee.com/geek/creating-mask-layers-using-php-gd

I would think it would be much easier to do by simply adding a bitmask or alpha channel. In that case you would use a negative mask image of your shape and then simply apply it to he regular image as a mask and then save out in a transparent format. Ive never actually done this with GD or ImageMagick but i would think its available as Jerry suggests.

Acutally here is a blog post form a similar SO question that might help: http://about.phalacee.com/geek/creating-mask-layers-using-php-gd

半城柳色半声笛 2024-08-26 01:45:32

执行此类操作的通常方法是使用白色(全 1)和黑色(全 0)作为轮廓,而不是透明和不透明。然后将该图像与您要裁剪的图像进行“AND”操作。结果是轮廓图像有 0 的地方是 0,而轮廓图像有 1 的地方是 0。

您可能还需要反转轮廓图像,并将反转版本与背景图像相与。然后,您将背景图像与前景图像进行“或”操作,以生成爱达荷州形状的前景图像(使用您的示例)和其他地方的背景图像的合成图像。

根据您所使用的库的功能,很有可能可以直接使用这种功能。举例来说,Windows 在 MaskBlt 函数中具有此功能。

The usual way to do something like this would be to use white (all 1's) and black (all 0's) for your outline instead of transparent and opaque. Then you AND that image with the image you're trying to crop. The result is 0's where the outline image had 0's, and the other image where the outline had 1's.

You may also need to invert your outline image, and AND the inverted version with the background image. Then you OR the background image with the foreground image to produce a composite of the foreground image in the shape of Idaho (to use your example) and the background image everywhere else.

Depending on the capabilities of the library you're using, there's a good chance that kind of capability is directly available though. Just for example, Windows has this capability in the MaskBlt function.

好倦 2024-08-26 01:45:32

在 php 中使用 ImageMagick 可能是你最好的选择:

$source = IMagick("sourcefile");
$mask = IMagick("maskfile");
$mask->adaptiveResizeImage($source->getImageWidth(), $source->getImageHeight(), true);
$source->compositeImage($mask, imagick::COMPOSITE_MULTIPLY, 0, 0);
$source->writeImage("newfile");
$source->clear();
$source->destroy();

sourcefile 必须是你喜欢的源图像,maskfile 必须是一个遮罩文件,它的 alpha 通道正确设置为你想要的形状。

In php using ImageMagick is probably your best bet:

$source = IMagick("sourcefile");
$mask = IMagick("maskfile");
$mask->adaptiveResizeImage($source->getImageWidth(), $source->getImageHeight(), true);
$source->compositeImage($mask, imagick::COMPOSITE_MULTIPLY, 0, 0);
$source->writeImage("newfile");
$source->clear();
$source->destroy();

sourcefile must be the source image as you like, maskfile must be a mask file that has the alpha channel set correctly for the shape you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文