Libvips: Insérer un PNG avec un fond transparent sur JPEG - Obtenir un fond blanc

Créé le 25 oct. 2017  ·  4Commentaires  ·  Source: libvips/libvips

J'ai un JPEG:

tile

Je veux insérer le PNG:

pin

Sur la position 128x128 sur le JPEG.

En faisant

vips insert tile.jpg pin.png newimage.jpg 128 128

Cela me donne Error Output: insert: images must have the same number of bands, or one must be single-band puisque pin.jpg a un canal alpha et tile.jpg pas.

Alors je convertis le jpg en png:

convert tile.jpg png32:tile.png    

Maintenant, j'ai un tile.png avec Alpha Channel:
tile

Mais quand j'appelle maintenant:

vips insert tile.png pin.png newimage.png 128 128

J'obtiens ce qui suit:
inserted

L'arrière-plan du pin.png inséré est blanc.

J'ai vérifié vips insert --help-all mais je n'ai pas vraiment trouvé de paramètre de ligne de commande utile. Les deux images ont un canal alpha. Mes questions:

  • La commande libvips insert charge l'arrière-plan transparent?
  • Si oui: quelqu'un peut-il alors me montrer comment utiliser cette fonctionnalité?
  • ... ou est-ce que je fais quelque chose de mal ici?

LG
codécitoyen

question

Commentaire le plus utile

Vous pouvez agrandir la superposition pour qu'elle corresponde à la grande image, puis composer:

width=$(vipsheader -f width background.jpg)
height=$(vipsheader -f height background.jpg)
vips embed marker.png overlay.png 128 128 $width $height
vips composite "background.jpg overlay.png" final.jpg 2

Ou découpez une partie de l'arrière-plan, composez, puis réinsérez (comme ci-dessus).

Cela semble être une chose courante à faire, peut-être devrait-il y avoir une option pour spécifier la position.

Tous les 4 commentaires

Bonjour @codecitizen ,

insert est une opération de très bas niveau: elle met juste une image sur une autre, elle ne fait aucun mélange.

Dans les libvips actuelles, vous devez utiliser ifthenelse avec l'option blend . C'est assez horrible:

# get the alpha channel from the overlay
vips extract_band marker.png alpha.png 3 --n 1

# get RGB from the overlay
vips extract_band marker.png rgb.png 0 --n 3

# find the size of the overlay
width=$(vipsheader -f width marker.png)
height=$(vipsheader -f height marker.png)

# cut out an area the size of marker from the background
vips extract_area background.jpg bg.png 128 128 $width $height

# blend the marker on top of the chunk of background
vips ifthenelse alpha.png rgb.png bg.png blended.png --blend

# insert the blended image back into the background
vips insert background.jpg blended.png final.jpg 128 128 

Pour générer ceci:

final

libvips 8.6 a un nouvel opérateur sophistiqué composite qui peut faire tous les modes de fusion habituels. Tu peux faire:

vips composite "background.jpg marker.png" final.jpg 2

2 signifie over mode de fusion. Il prend en charge tous les modes de fusion PDF. Il devrait sortir dans une semaine environ.

@jcupitt Hé, merci. J'ai installé libvips 8.6 et vips composite fonctionnait très bien. Je ne sais tout simplement pas comment spécifier un emplacement de la deuxième image, si possible.

Vous pouvez agrandir la superposition pour qu'elle corresponde à la grande image, puis composer:

width=$(vipsheader -f width background.jpg)
height=$(vipsheader -f height background.jpg)
vips embed marker.png overlay.png 128 128 $width $height
vips composite "background.jpg overlay.png" final.jpg 2

Ou découpez une partie de l'arrière-plan, composez, puis réinsérez (comme ci-dessus).

Cela semble être une chose courante à faire, peut-être devrait-il y avoir une option pour spécifier la position.

Impressionnant! Merci beaucoup pour l'aide rapide!

Cette page vous a été utile?
0 / 5 - 0 notes