如何在图像中找到透明背景位置?

问题描述 投票:0回答:1

我有一个 HTML 代码,我将在其中将文本放置在图像的透明背景区域中。 由于我不知道如何使用 JS 或 HTML 即时获取精确坐标,我能够开始的唯一解决方案是使用

pyautogui
Python 库。但我也找不到正确的透明区域坐标。

从我的 OneDrive 共享的 PSD 和 PNG 文件:图片

我会把坐标传递给这个样式:

.code-container {
    position: absolute;
    top: 250px;
    left: 500px;
    transform: translate(-50%, -50%);
}

我尝试使用 Python,但也可以是另一种解决方案。 我的 Python 代码:

import pyautogui
import os
import pathlib
from PIL import Image

# Load your PNG file and cut out the desired image
Img = os.path.join(os.path.sep, pathlib.Path(__file__).parent.resolve(), 'test.png')

image = Image.open(Img)
image.show()

alpha_channel = image.split()[-1]  # get the alpha channel
transparent_pixels = []

for x in range(image.width):
    for y in range(image.height):
        if alpha_channel.getpixel((x, y)) == 0:
            transparent_pixels.append((x, y))
            pyautogui.moveTo(x, y)

print(transparent_pixels)

输出不在透明区域,不知道怎么回事。

所以我的问题是,无论应用程序打开图像还是 Windows 主题,我如何找到 透明区域坐标 传递给我的 HTML 代码?

在 Photoshop 上我可以看到背景:

当我用画图打开时,我可以看到白色区域:

但是当我使用

PIL
Python 库打开图像时,它从“照片”Windows 应用程序打开,在这种情况下颜色是黑色:

使用

pyautogui.displayMousePosition()
函数找到的颜色是:
X:  966 Y:  634 RGB: ( 28,  32,  44)

javascript python html image pyautogui
1个回答
0
投票

尝试

image = Image.open(Img).convert('RGBA')

© www.soinside.com 2019 - 2024. All rights reserved.