首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >通过python连接图像时如何设置白色背景?

通过python连接图像时如何设置白色背景?
EN

Stack Overflow用户
提问于 2022-02-05 19:58:06
回答 3查看 367关注 0票数 1
代码语言:javascript
运行
复制
import numpy as np
from imageio import imread, imwrite

im1 = imread('https://api.sofascore.app/api/v1/team/2697/image')[...,:3]
im2 = imread('https://api.sofascore.app/api/v1/team/2692/image')[...,:3]

result = np.hstack((im1,im2))

imwrite('result.jpg', result)

原始图像直接从url中打开(我试图将这两个图像连接为一个,并使背景保持白色):

可以看到,两者都没有背景,但是当通过Python将两者连接起来时,定义的背景变成了这个苔藓绿色:

我试着修改颜色接收:

代码语言:javascript
运行
复制
im1 = imread('https://api.sofascore.app/api/v1/team/2697/image')[...,:1]
im2 = imread('https://api.sofascore.app/api/v1/team/2692/image')[...,:1]

但是结果是黑白的背景看起来仍然像它从以前的绿色转换,尽管巴布亚新几内亚的没有这样的背景颜色。

我应该如何着手解决我的需要?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-02-05 20:33:02

在你的图像中有第四个频道--透明度。您正在用[...,:1]丢弃该频道。这是一个错误。

如果您保留alpha通道,这将很好地工作:

代码语言:javascript
运行
复制
import numpy as np
from imageio import imread, imwrite

im1 = imread('https://api.sofascore.app/api/v1/team/2697/image')
im2 = imread('https://api.sofascore.app/api/v1/team/2692/image')

result = np.hstack((im1,im2))

imwrite('result.png', result)

但是,如果您试图制作一个jpg,您将遇到一个问题:

代码语言:javascript
运行
复制
>>> imwrite('test.jpg', result)

OSError: JPEG does not support alpha channel.

这是正确的,因为日圆不具有透明度。如果你想使用透明,并让你的输出是一个JPG,我建议一个牧师。

您可以通过使用np.where并查找alpha通道为0的位置来替换透明像素:

代码语言:javascript
运行
复制
result = np.hstack((im1,im2))
result[np.where(result[...,3] == 0)] = [255, 255, 255, 255]

imwrite('result.png', result)
票数 1
EN

Stack Overflow用户

发布于 2022-02-05 20:23:14

如果您想要更改图像的背景,pixellib是最好的解决方案,因为它似乎是最合理和最容易使用的库。

代码语言:javascript
运行
复制
import pixellib
from pixellib.tune_bg import alter_bg

change_bg = alter_bg()
change_bg.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
change_bg.color_bg("sample.png", colors=(255,255,255), output_image_name="colored_bg.png")

此代码要求像素库更高或与0.6.1

相同

票数 1
EN

Stack Overflow用户

发布于 2022-02-05 20:39:22

如果你想提高图像质量,这里有一个解决方案。@Brondy

代码语言:javascript
运行
复制
# External libraries used for
# Image IO
from PIL import Image

# Morphological filtering
from skimage.morphology import opening
from skimage.morphology import disk

# Data handling
import numpy as np

# Connected component filtering
import cv2

black = 0
white = 255
threshold = 160

# Open input image in grayscale mode and get its pixels.
img = Image.open("image.jpg").convert("LA")
pixels = np.array(img)[:,:,0]

# Remove pixels above threshold
pixels[pixels > threshold] = white
pixels[pixels < threshold] = black


# Morphological opening
blobSize = 1 # Select the maximum radius of the blobs you would like to remove
structureElement = disk(blobSize)  # you can define different shapes, here we take a disk shape
# We need to invert the image such that black is background and white foreground to perform the opening
pixels = np.invert(opening(np.invert(pixels), structureElement))


# Create and save new image.
newImg = Image.fromarray(pixels).convert('RGB')
newImg.save("newImage1.PNG")

# Find the connected components (black objects in your image)
# Because the function searches for white connected components on a black background, we need to invert the image
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(np.invert(pixels), connectivity=8)

# For every connected component in your image, you can obtain the number of pixels from the stats variable in the last
# column. We remove the first entry from sizes, because this is the entry of the background connected component
sizes = stats[1:,-1]
nb_components -= 1

# Define the minimum size (number of pixels) a component should consist of
minimum_size = 100

# Create a new image
newPixels = np.ones(pixels.shape)*255

# Iterate over all components in the image, only keep the components larger than minimum size
for i in range(1, nb_components):
    if sizes[i] > minimum_size:
        newPixels[output == i+1] = 0

# Create and save new image.
newImg = Image.fromarray(newPixels).convert('RGB')
newImg.save("new_img.PNG")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71001664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档