简体   繁体   English

如何在图像中创建透明形状

[英]How to create a transparent shape in the image

I have an image now I want to create a transparent shape in the image so that I can put another image in that shape. 我现在有一个图像,我想在图像中创建一个透明形状,以便可以在该形状中放置另一个图像。

I want to achieve something like this 我想实现这样的目标

在此处输入图片说明

Is there any way to do this programmatically in java or python. 有什么方法可以用Java或python编程地做到这一点。 If not How can I do this manually. 如果没有,我该如何手动执行此操作。

I have linux environment and some image editor But I am not able to do this even manually. 我有linux环境和一些图像编辑器,但是我什至无法手动执行此操作。

So, the following makes use of: 因此,以下使用:

So, starting with this... 所以,从这个开始...

前景

I want to add this behind it (but peeking through) 我想在它后面添加它(但可以窥视一下)

PEEK-A-嘘

The first thing we need to do is make a "alpha" based mask of the area we want to become transparent. 我们需要做的第一件事是为要变得透明的区域制作一个基于“ alpha”的蒙版。 For this, I'm using a RadialGradientPaint , as it allows me to produce a "fading" effect around the edges. 为此,我使用了RadialGradientPaint ,因为它允许我在边缘周围产生“褪色”效果。

Now, I've already done all the math (used a simple image editor to work it out), so I know where I want the "hole" appear 现在,我已经完成了所有的数学运算(使用一个简单的图像编辑器进行计算),所以我知道我希望“孔”出现在哪里

BufferedImage city = ImageIO.read(new File("/Users/swhitehead/Downloads/City.jpg"));

BufferedImage mask = new BufferedImage(city.getWidth(), city.getHeight(), BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = mask.createGraphics();
Color transparent = new Color(255, 0, 0, 0);
Color fill = Color.RED;
RadialGradientPaint rgp = new RadialGradientPaint(
                new Point2D.Double(955, 185),
                185,
                new float[]{0f, 0.75f, 1f},
                new Color[]{transparent, transparent, fill});
g2d.setPaint(rgp);
g2d.fill(new Rectangle(0, 0, mask.getWidth(), mask.getHeight()));
g2d.dispose();

Next, we use a AlphaComposite to paint the mask over the original (city) image... 接下来,我们使用AlphaComposite在原始(城市)图像上绘制蒙版...

BufferedImage masked = new BufferedImage(city.getWidth(), city.getHeight(), BufferedImage.TYPE_INT_ARGB);
g2d = masked.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, masked.getWidth(), masked.getHeight());
g2d.drawImage(city, 0, 0, null);
g2d.setComposite(AlphaComposite.DstAtop);
g2d.drawImage(mask, 0, 0, null);
g2d.dispose();

This generates something like... 这会产生类似...

蒙面

The "white" hole is actually transparent, it's just the page is also white :/ “白色”孔实际上是透明的,只是页面也是白色的:/

Finally, we combine the masked image with the background image... 最后,我们将masked图像与背景图像结合起来...

BufferedImage composite = new BufferedImage(city.getWidth(), city.getHeight(), BufferedImage.TYPE_INT_ARGB);
g2d = composite.createGraphics();
g2d.drawImage(background, city.getWidth() - background.getWidth(), 0, null);
g2d.drawImage(masked, 0, 0, null);
g2d.dispose();

to produce something like... 产生类似...的东西

他一直在看

So, based on your description, this is what I assume you mean 所以,根据您的描述,这就是我的意思

From here : 这里

# import the necessary packages
from __future__ import print_function
import numpy as np
import cv2

# load the image
image = cv2.imread("pic.jpg")

# loop over the alpha transparency values
for alpha in np.arange(0, 1.1, 0.1)[::-1]:
    # create two copies of the original image -- one for
    # the overlay and one for the final output image
    overlay = image.copy()
    output = image.copy()

    # draw a red rectangle surrounding Adrian in the image
    # along with the text "PyImageSearch" at the top-left
    # corner
    cv2.rectangle(overlay, (420, 205), (595, 385),
        (0, 0, 255), -1)
    cv2.putText(overlay, "PyImageSearch: alpha={}".format(alpha),
        (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 3)
# apply the overlay
    cv2.addWeighted(overlay, alpha, output, 1 - alpha,
        0, output)
# show the output image
    print("alpha={}, beta={}".format(alpha, 1 - alpha))
    cv2.imshow("Output", output)
    cv2.waitKey(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM