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]
但是结果是黑白的背景看起来仍然像它从以前的绿色转换,尽管巴布亚新几内亚的没有这样的背景颜色。
我应该如何着手解决我的需要?
发布于 2022-02-05 20:33:02
在你的图像中有第四个频道--透明度。您正在用[...,: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,我建议一个牧师。
您可以通过使用np.where
并查找alpha通道为0的位置来替换透明像素:
result = np.hstack((im1,im2))
result[np.where(result[...,3] == 0)] = [255, 255, 255, 255]
imwrite('result.png', result)
发布于 2022-02-05 20:23:14
如果您想要更改图像的背景,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")
此代码要求像素库更高或与0.6.1
相同
发布于 2022-02-05 20:39:22
如果你想提高图像质量,这里有一个解决方案。@Brondy
# 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")
https://stackoverflow.com/questions/71001664
复制相似问题