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

How set white color background when concatenating images via python?

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连接两者时,定义的背景变成了这个苔绿色:

我尝试修改颜色接收:

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

但是结果是黑白的,背景看起来仍然像是从以前的绿色转换而来,即使 PNG 没有这样的背景色。

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

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

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")

This code requires pixellib to be higher or the same as 0.6.1

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

如果您保留 Alpha 通道,这将正常工作:

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,你会遇到一个问题:

>>> imwrite('test.jpg', result)

OSError: JPEG does not support alpha channel.

这是正确的,因为 JPG 不透明。如果您想使用透明度并且输出为 JPG,我建议您使用牧师。

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

result = np.hstack((im1,im2))
result[np.where(result[...,3] == 0)] = [255, 255, 255, 255]

imwrite('result.png', result)

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

# 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")