Carte de profondeur pour des planches qui suivent les volumes
La profondeur de la scène est estimée à partir de la photo (MiDaS small ou Depth Anything V2 Small) et mélangée aux gris : les planches suivent les volumes du sujet plutôt que ses lumières et ses ombres. - Onglet « Profondeur » : part de la carte de profondeur, modèle, et détails fins de la photo gravés dans la profondeur (yeux, bouche). - Répartition des niveaux : rapproche les planches d'aires égales quand les valeurs sont tassées sur quelques gris. - Images 16 bits lues sur toute leur plage (elles étaient écrêtées à 255). - Le Python d'Inkscape (MinGW, sans pip) ne peut pas charger onnxruntime : le calcul tourne dans un environnement à part, installé par deploy.ps1 -Depth, lancé en sous-processus, résultat gardé en cache. - Depth Anything sur la carte graphique (DirectML) avec repli sur le processeur ; MiDaS small toujours sur le processeur, DirectML lui faisant rendre une carte fausse. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AumyrbcuQobB3GbPrFjG9i
This commit is contained in:
parent
16589716fd
commit
3ed0fa0987
98
README.md
98
README.md
@ -44,7 +44,18 @@ aplanis mais les yeux, la bouche et le bord du visage restent nets, et les
|
|||||||
reconnaissable. Un flou gaussien simple reste disponible pour les reliefs
|
reconnaissable. Un flou gaussien simple reste disponible pour les reliefs
|
||||||
abstraits.
|
abstraits.
|
||||||
- Quantification à seuils réguliers, de 2 à 64 niveaux, après étirement de la
|
- Quantification à seuils réguliers, de 2 à 64 niveaux, après étirement de la
|
||||||
plage de gris de l'image.
|
plage de gris de l'image. Une répartition réglable rapproche les niveaux
|
||||||
|
d'aires égales, pour les images dont presque toutes les valeurs tiennent
|
||||||
|
dans quelques gris (cartes de profondeur).
|
||||||
|
- Carte de profondeur (facultative) : la profondeur de la scène est estimée
|
||||||
|
à partir de la photo par un réseau de neurones (MiDaS small ou Depth
|
||||||
|
Anything V2 Small), enrichie des contrastes fins de la photo (yeux, bouche)
|
||||||
|
et mélangée aux gris dans la proportion voulue. Les
|
||||||
|
planches suivent alors les volumes du sujet plutôt que ses lumières et ses
|
||||||
|
ombres. Calcul sur la carte graphique quand c'est possible, résultat gardé
|
||||||
|
en cache.
|
||||||
|
- Images 16 bits (cartes de profondeur produites ailleurs) lues sur toute
|
||||||
|
leur plage.
|
||||||
- Contours tracés au sous-pixel sur l'image lissée (« marching squares »), puis
|
- Contours tracés au sous-pixel sur l'image lissée (« marching squares »), puis
|
||||||
simplifiés et convertis en courbes de Bézier : pas d'escalier de pixels.
|
simplifiés et convertis en courbes de Bézier : pas d'escalier de pixels.
|
||||||
- Toutes les formes sont fermées : là où un niveau touche le bord de l'image,
|
- Toutes les formes sont fermées : là où un niveau touche le bord de l'image,
|
||||||
@ -73,8 +84,9 @@ aplanis mais les yeux, la bouche et le bord du visage restent nets, et les
|
|||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Aucune dépendance à installer : l'extension utilise `inkex`, `numpy` et
|
Aucune dépendance à installer pour l'usage de base : l'extension utilise
|
||||||
`Pillow`, tous fournis avec Inkscape.
|
`inkex`, `numpy` et `Pillow`, tous fournis avec Inkscape. Seule la carte de
|
||||||
|
profondeur demande une installation à part (`-Depth`, ci-dessous).
|
||||||
|
|
||||||
### Windows
|
### Windows
|
||||||
|
|
||||||
@ -82,10 +94,41 @@ Aucune dépendance à installer : l'extension utilise `inkex`, `numpy` et
|
|||||||
.\deploy.ps1 # copie dans %APPDATA%\inkscape\extensions\GrayIsoLayers
|
.\deploy.ps1 # copie dans %APPDATA%\inkscape\extensions\GrayIsoLayers
|
||||||
.\deploy.ps1 -Uninstall # retire l'extension
|
.\deploy.ps1 -Uninstall # retire l'extension
|
||||||
.\deploy.ps1 -Force # déploie même si Inkscape est ouvert
|
.\deploy.ps1 -Force # déploie même si Inkscape est ouvert
|
||||||
|
.\deploy.ps1 -Depth # déploie, et installe de quoi calculer la profondeur
|
||||||
```
|
```
|
||||||
|
|
||||||
Inkscape doit être fermé pendant la copie, puis relancé.
|
Inkscape doit être fermé pendant la copie, puis relancé.
|
||||||
|
|
||||||
|
### Carte de profondeur
|
||||||
|
|
||||||
|
Le Python fourni avec Inkscape ne peut pas charger `onnxruntime` (c'est une
|
||||||
|
construction MinGW, sans `pip`). `.\deploy.ps1 -Depth` installe donc, dans
|
||||||
|
`%LOCALAPPDATA%\GrayIsoLayers` :
|
||||||
|
|
||||||
|
- `venv\` : un environnement Python avec `onnxruntime-directml`, `numpy` et
|
||||||
|
`pillow`, créé à partir du Python 3 de la machine (`python` du `PATH`, sinon
|
||||||
|
le lanceur `py` ; `-Python <chemin>` pour en imposer un autre) ;
|
||||||
|
- `models\` : les deux modèles, téléchargés une fois (64 Mo et 94 Mo) ;
|
||||||
|
- `cache\` : les 30 dernières cartes calculées.
|
||||||
|
|
||||||
|
L'extension lance le calcul dans cet environnement. Sans lui, elle le signale
|
||||||
|
et travaille sur les gris seuls. `.\deploy.ps1 -Uninstall` supprime aussi ce
|
||||||
|
dossier. La variable d'environnement `GRAY_ISO_LAYERS_PYTHON` désigne un autre
|
||||||
|
Python que celui de `venv\`.
|
||||||
|
|
||||||
|
Temps mesurés sur le portrait d'essai, lancement du Python compris (Core i7-3770
|
||||||
|
et RTX 4080 SUPER ; à titre indicatif) :
|
||||||
|
|
||||||
|
| Modèle | Calcul | Premier passage | Passages suivants |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| MiDaS small (256 points) | processeur | 1 s environ | cache, sans délai |
|
||||||
|
| Depth Anything V2 Small (518 points) | carte graphique (DirectML) | 2 s environ | cache, sans délai |
|
||||||
|
| Depth Anything V2 Small | processeur | 3 s environ | cache, sans délai |
|
||||||
|
|
||||||
|
MiDaS small est toujours calculé sur le processeur : sous DirectML il rend une
|
||||||
|
carte fausse, et son calcul est plus court que l'initialisation de la carte
|
||||||
|
graphique.
|
||||||
|
|
||||||
### Manuelle
|
### Manuelle
|
||||||
|
|
||||||
Copier dans un sous-dossier du répertoire des extensions utilisateur
|
Copier dans un sous-dossier du répertoire des extensions utilisateur
|
||||||
@ -93,7 +136,8 @@ Copier dans un sous-dossier du répertoire des extensions utilisateur
|
|||||||
sous Linux, `~/Library/Application Support/org.inkscape.Inkscape/config/inkscape/extensions`
|
sous Linux, `~/Library/Application Support/org.inkscape.Inkscape/config/inkscape/extensions`
|
||||||
sous macOS) :
|
sous macOS) :
|
||||||
|
|
||||||
- `gray_iso_layers.inx`, `gray_iso_layers.py`, `gray_iso_layers_core.py` ;
|
- `gray_iso_layers.inx`, `gray_iso_layers.py`, `gray_iso_layers_core.py`,
|
||||||
|
`gray_iso_layers_depth.py`, `depth_estimate.py` ;
|
||||||
- `images/parameters_en.png` ;
|
- `images/parameters_en.png` ;
|
||||||
- le dossier `locale/`.
|
- le dossier `locale/`.
|
||||||
|
|
||||||
@ -127,6 +171,19 @@ vide.
|
|||||||
- **Contours anguleux** : augmenter le lissage ou la simplification.
|
- **Contours anguleux** : augmenter le lissage ou la simplification.
|
||||||
- **Détails perdus, visage méconnaissable** : réduire le lissage (0,5 à 1 %),
|
- **Détails perdus, visage méconnaissable** : réduire le lissage (0,5 à 1 %),
|
||||||
laisser « Préserver les contours » coché, augmenter la résolution de calcul.
|
laisser « Préserver les contours » coché, augmenter la résolution de calcul.
|
||||||
|
- **Relief fidèle aux volumes** : onglet « Profondeur », part de la carte de
|
||||||
|
profondeur à 100 % et modèle « Fin », qui détoure nettement mieux les cheveux
|
||||||
|
et le visage que le modèle « Rapide ». Les planches suivent les volumes (le
|
||||||
|
nez devant, le fond derrière) et le réglage « Détails fins » y grave les
|
||||||
|
yeux, la bouche et les sourcils, que la profondeur seule ne contient pas.
|
||||||
|
Visage trop lisse : monter les détails fins (jusqu'à 150 %). Trop de petites
|
||||||
|
formes dans les cheveux ou la barbe : les baisser, ou augmenter le lissage.
|
||||||
|
- **Planches vides, sujet tassé sur quelques planches** (fréquent avec une
|
||||||
|
carte de profondeur) : augmenter la répartition des niveaux. À 100 %, toutes
|
||||||
|
les planches ont la même surface, fond compris ; une valeur intermédiaire
|
||||||
|
garde plus de planches pour le sujet.
|
||||||
|
- **Carte de profondeur produite ailleurs** : l'importer et la sélectionner
|
||||||
|
comme n'importe quelle image (8 ou 16 bits), part de profondeur à 0.
|
||||||
- **Portrait** : un fond uni donne le meilleur résultat ; un fond en dégradé
|
- **Portrait** : un fond uni donne le meilleur résultat ; un fond en dégradé
|
||||||
produit des anneaux, puisque chaque nuance du dégradé devient un aplat.
|
produit des anneaux, puisque chaque nuance du dégradé devient un aplat.
|
||||||
- **Image liée introuvable** : l'incorporer (clic droit > Incorporer l'image).
|
- **Image liée introuvable** : l'incorporer (clic droit > Incorporer l'image).
|
||||||
@ -142,6 +199,7 @@ Onglet **Niveaux**
|
|||||||
| Paramètre | Défaut | Rôle |
|
| Paramètre | Défaut | Rôle |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| Nombre de niveaux de gris | 16 | Nombre d'aplats, donc de calques produits (2 à 64). |
|
| Nombre de niveaux de gris | 16 | Nombre d'aplats, donc de calques produits (2 à 64). |
|
||||||
|
| Répartition des niveaux (% d'aires égales) | 0 | 0 : seuils régulièrement espacés. 100 : chaque planche couvre la même surface. |
|
||||||
| Contenu de chaque planche | Empilées, la plus claire devant | « Empilées, la plus claire devant » : planche n° 1 = la plus sombre, toute l'image. « Empilées, la plus sombre devant » : planche n° 1 = la plus claire, toute l'image. « Son gris seul » : planches côte à côte, sans recouvrement, planche n° 1 = le gris le plus sombre. |
|
| Contenu de chaque planche | Empilées, la plus claire devant | « Empilées, la plus claire devant » : planche n° 1 = la plus sombre, toute l'image. « Empilées, la plus sombre devant » : planche n° 1 = la plus claire, toute l'image. « Son gris seul » : planches côte à côte, sans recouvrement, planche n° 1 = le gris le plus sombre. |
|
||||||
| Reporter sur chaque planche le contour de la suivante | oui | Repère d'assemblage tracé dans la couleur de marquage (planches empilées uniquement). |
|
| Reporter sur chaque planche le contour de la suivante | oui | Repère d'assemblage tracé dans la couleur de marquage (planches empilées uniquement). |
|
||||||
| Lissage (% du grand côté de l'image) | 1 | Lissage appliqué avant la découpe : plus il est fort, plus les contours sont ronds et simples. |
|
| Lissage (% du grand côté de l'image) | 1 | Lissage appliqué avant la découpe : plus il est fort, plus les contours sont ronds et simples. |
|
||||||
@ -151,6 +209,14 @@ Onglet **Niveaux**
|
|||||||
| Simplification (points de calcul) | 0,4 | Écart maximal toléré lors de la suppression de nœuds ; 0 les conserve tous. |
|
| Simplification (points de calcul) | 0,4 | Écart maximal toléré lors de la suppression de nœuds ; 0 les conserve tous. |
|
||||||
| Lisser les contours avec des courbes de Bézier | oui | Sinon, les contours sont des polygones. |
|
| Lisser les contours avec des courbes de Bézier | oui | Sinon, les contours sont des polygones. |
|
||||||
|
|
||||||
|
Onglet **Profondeur**
|
||||||
|
|
||||||
|
| Paramètre | Défaut | Rôle |
|
||||||
|
| --- | --- | --- |
|
||||||
|
| Part de la carte de profondeur (%) | 0 | 0 : gris de l'image seuls. 100 : profondeur estimée seule. Entre les deux, mélange. Demande `deploy.ps1 -Depth`. |
|
||||||
|
| Modèle de profondeur | Rapide (MiDaS small) | « Rapide » : MiDaS small, carte lisse. « Fin » : Depth Anything V2 Small, contours plus précis. |
|
||||||
|
| Détails fins de la photo (%) | 100 | Grave dans la profondeur les contrastes fins de la photo (yeux, bouche, sourcils). Un fond uni n'apporte rien et reste au fond. 0 : profondeur lisse seule. |
|
||||||
|
|
||||||
Onglet **Style**
|
Onglet **Style**
|
||||||
|
|
||||||
| Paramètre | Défaut | Rôle |
|
| Paramètre | Défaut | Rôle |
|
||||||
@ -169,6 +235,8 @@ Onglet **Style**
|
|||||||
gray_iso_layers.inx Boîte de dialogue (textes source en anglais)
|
gray_iso_layers.inx Boîte de dialogue (textes source en anglais)
|
||||||
gray_iso_layers.py Couche inkex : lecture de l'image, écriture du SVG
|
gray_iso_layers.py Couche inkex : lecture de l'image, écriture du SVG
|
||||||
gray_iso_layers_core.py Noyau de calcul, sans inkex (numpy seul)
|
gray_iso_layers_core.py Noyau de calcul, sans inkex (numpy seul)
|
||||||
|
gray_iso_layers_depth.py Carte de profondeur : lancement du calcul, cache
|
||||||
|
depth_estimate.py Calcul de la profondeur (onnxruntime), hors Inkscape
|
||||||
i18n.py Extraction / mise à jour / compilation des traductions
|
i18n.py Extraction / mise à jour / compilation des traductions
|
||||||
po/ Catalogues grayisolayers.pot, en.po, fr.po
|
po/ Catalogues grayisolayers.pot, en.po, fr.po
|
||||||
locale/ Catalogues compilés (.mo), déployés
|
locale/ Catalogues compilés (.mo), déployés
|
||||||
@ -194,13 +262,17 @@ gris (0 noir, 1 blanc) ; un anneau est un tableau `(n, 2)` de points, en
|
|||||||
| `edge_preserving_blur(field, radius, contrast)` | Lissage qui respecte les contours (filtre guidé). |
|
| `edge_preserving_blur(field, radius, contrast)` | Lissage qui respecte les contours (filtre guidé). |
|
||||||
| `smooth(field, blur, preserve_edges)` | Lissage avant découpe, puis étirement sur 0..1. |
|
| `smooth(field, blur, preserve_edges)` | Lissage avant découpe, puis étirement sur 0..1. |
|
||||||
| `normalize(field)` | Étire le champ sur 0..1. |
|
| `normalize(field)` | Étire le champ sur 0..1. |
|
||||||
|
| `spread_levels(field, amount)` | Répartit les valeurs pour que les niveaux tendent vers des aires égales. |
|
||||||
|
| `guided_filter(guide, source, radius, eps)` | Lisse `source` en lui donnant les contours de `guide` (carte de profondeur recalée sur la photo). |
|
||||||
|
| `add_detail(depth, gray, amount, sigma)` | Grave dans la profondeur les contrastes fins des gris (yeux, bouche). |
|
||||||
|
| `blend_fields(gray, depth, mix)` | Mélange des gris et de la profondeur. |
|
||||||
| `thresholds(levels)` | Seuils réguliers séparant les niveaux. |
|
| `thresholds(levels)` | Seuils réguliers séparant les niveaux. |
|
||||||
| `contour_rings(field, threshold, above=True)` | Contours fermés de la région au-dessus (ou au-dessous) du seuil. |
|
| `contour_rings(field, threshold, above=True)` | Contours fermés de la région au-dessus (ou au-dessous) du seuil. |
|
||||||
| `ring_area(ring)` | Aire d'un anneau. |
|
| `ring_area(ring)` | Aire d'un anneau. |
|
||||||
| `simplify_ring(ring, tolerance)` / `simplify_chain(chain, tolerance)` | Simplification de Douglas-Peucker d'un anneau fermé, d'une ligne ouverte. |
|
| `simplify_ring(ring, tolerance)` / `simplify_chain(chain, tolerance)` | Simplification de Douglas-Peucker d'un anneau fermé, d'une ligne ouverte. |
|
||||||
| `contour_lines(field, threshold, tolerance, min_area)` | Ligne de niveau d'un seuil : boucles fermées et lignes ouvertes aboutissant au bord. |
|
| `contour_lines(field, threshold, tolerance, min_area)` | Ligne de niveau d'un seuil : boucles fermées et lignes ouvertes aboutissant au bord. |
|
||||||
| `region_rings(field, lines, inside)` | Contour fermé d'une région bornée par des lignes de niveau, refermé le long du bord de l'image. |
|
| `region_rings(field, lines, inside)` | Contour fermé d'une région bornée par des lignes de niveau, refermé le long du bord de l'image. |
|
||||||
| `iso_boards(field, levels, blur, min_area, tolerance, shapes, preserve_edges)` | Une planche par niveau : liste de `(niveau, gris, anneaux, repère)`, le repère étant la ligne de niveau qui borde la planche suivante. |
|
| `iso_boards(field, levels, blur, min_area, tolerance, shapes, preserve_edges, equalize)` | Une planche par niveau : liste de `(niveau, gris, anneaux, repère)`, le repère étant la ligne de niveau qui borde la planche suivante. |
|
||||||
| `iso_layers(…)` | Comme `iso_boards`, sans les repères. |
|
| `iso_layers(…)` | Comme `iso_boards`, sans les repères. |
|
||||||
| `image_ring(width, height)` | Rectangle de l'image. |
|
| `image_ring(width, height)` | Rectangle de l'image. |
|
||||||
| `scale_rings(rings, scale_x, scale_y, offset_x, offset_y)` | Passage au repère de destination. |
|
| `scale_rings(rings, scale_x, scale_y, offset_x, offset_y)` | Passage au repère de destination. |
|
||||||
@ -215,18 +287,26 @@ pip install pytest numpy pillow
|
|||||||
python -m pytest -q
|
python -m pytest -q
|
||||||
```
|
```
|
||||||
|
|
||||||
Sans `inkex` : 41 tests passent, 13 sont ignorés. Avec `inkex` :
|
Sans `inkex` : 51 tests passent, 17 sont ignorés. Avec `inkex` :
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
pip install lxml tinycss2 cssselect2 cssselect
|
pip install lxml tinycss2 cssselect2 cssselect
|
||||||
$env:PYTHONPATH = 'C:\Program Files\Inkscape\share\inkscape\extensions'
|
$env:PYTHONPATH = 'C:\Program Files\Inkscape\share\inkscape\extensions'
|
||||||
python -m pytest -q # 54 tests passent
|
python -m pytest -q # 67 tests passent, 1 est ignoré
|
||||||
```
|
```
|
||||||
|
|
||||||
Les tests couvrent le noyau (lissage qui respecte les contours, contours d'un cône et d'une rampe, fermeture le
|
Le test ignoré est le calcul réel d'une carte de profondeur, qui demande
|
||||||
|
`onnxruntime` et le modèle. Il se lance dans l'environnement de `-Depth` :
|
||||||
|
|
||||||
|
```powershell
|
||||||
|
& "$env:LOCALAPPDATA\GrayIsoLayers\venv\Scripts\python.exe" -m pip install pytest
|
||||||
|
& "$env:LOCALAPPDATA\GrayIsoLayers\venv\Scripts\python.exe" -m pytest -q -k depth
|
||||||
|
```
|
||||||
|
|
||||||
|
Les tests couvrent le noyau (lissage qui respecte les contours, répartition des niveaux, filtre guidé, détails fins gravés dans la profondeur, contours d'un cône et d'une rampe, fermeture le
|
||||||
long des bords, cas selle, simplification, aplats conformes à l'image
|
long des bords, cas selle, simplification, aplats conformes à l'image
|
||||||
quantifiée, frontières communes aux aplats voisins, feuilles à empiler, îlots
|
quantifiée, frontières communes aux aplats voisins, feuilles à empiler, îlots
|
||||||
écartés, écriture des chemins), l'extension de bout en bout (un calque par niveau, repères d'assemblage, image incorporée, liée ou
|
écartés, écriture des chemins), la carte de profondeur avec un calcul simulé (cache, environnement transmis, causes d'échec), l'extension de bout en bout (image 16 bits, mélange de la profondeur et repli sur les gris, un calque par niveau, repères d'assemblage, image incorporée, liée ou
|
||||||
tournée, groupe sélectionné, styles, messages d'erreur) et la cohérence des traductions et du
|
tournée, groupe sélectionné, styles, messages d'erreur) et la cohérence des traductions et du
|
||||||
`.inx`.
|
`.inx`.
|
||||||
|
|
||||||
|
|||||||
98
deploy.ps1
98
deploy.ps1
@ -16,14 +16,28 @@
|
|||||||
.PARAMETER FolderName
|
.PARAMETER FolderName
|
||||||
Nom du sous-dossier de l'extension. Par défaut : GrayIsoLayers
|
Nom du sous-dossier de l'extension. Par défaut : GrayIsoLayers
|
||||||
|
|
||||||
|
.PARAMETER Depth
|
||||||
|
Installe aussi de quoi calculer les cartes de profondeur, dans
|
||||||
|
%LOCALAPPDATA%\GrayIsoLayers : un environnement Python (onnxruntime-directml,
|
||||||
|
numpy, pillow) et les deux modèles (MiDaS small, Depth Anything V2 Small,
|
||||||
|
environ 165 Mo à télécharger). Le Python d'Inkscape ne peut pas charger
|
||||||
|
onnxruntime : il faut un Python 3 installé sur la machine.
|
||||||
|
|
||||||
|
.PARAMETER Python
|
||||||
|
Python qui crée l'environnement de -Depth. Par défaut : « python » du PATH,
|
||||||
|
sinon le lanceur « py ». À indiquer si le Python trouvé est trop récent
|
||||||
|
pour onnxruntime.
|
||||||
|
|
||||||
.PARAMETER Uninstall
|
.PARAMETER Uninstall
|
||||||
Supprime le sous-dossier de l'extension au lieu de le créer.
|
Supprime le sous-dossier de l'extension au lieu de le créer, ainsi que
|
||||||
|
%LOCALAPPDATA%\GrayIsoLayers (environnement, modèles, cache).
|
||||||
|
|
||||||
.PARAMETER Force
|
.PARAMETER Force
|
||||||
Déploie même si Inkscape est en cours d'exécution.
|
Déploie même si Inkscape est en cours d'exécution.
|
||||||
|
|
||||||
.EXAMPLE
|
.EXAMPLE
|
||||||
.\deploy.ps1
|
.\deploy.ps1
|
||||||
|
.\deploy.ps1 -Depth
|
||||||
.\deploy.ps1 -Uninstall
|
.\deploy.ps1 -Uninstall
|
||||||
.\deploy.ps1 -FolderName 'autre-nom'
|
.\deploy.ps1 -FolderName 'autre-nom'
|
||||||
#>
|
#>
|
||||||
@ -32,17 +46,30 @@
|
|||||||
param(
|
param(
|
||||||
[string] $ExtensionsDir = (Join-Path $env:APPDATA 'inkscape\extensions'),
|
[string] $ExtensionsDir = (Join-Path $env:APPDATA 'inkscape\extensions'),
|
||||||
[string] $FolderName = 'GrayIsoLayers',
|
[string] $FolderName = 'GrayIsoLayers',
|
||||||
|
[switch] $Depth,
|
||||||
|
[string] $Python,
|
||||||
[switch] $Uninstall,
|
[switch] $Uninstall,
|
||||||
[switch] $Force
|
[switch] $Force
|
||||||
)
|
)
|
||||||
|
|
||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
|
|
||||||
$files = @('gray_iso_layers.inx', 'gray_iso_layers.py', 'gray_iso_layers_core.py', 'images\parameters_en.png')
|
$files = @('gray_iso_layers.inx', 'gray_iso_layers.py', 'gray_iso_layers_core.py',
|
||||||
|
'gray_iso_layers_depth.py', 'depth_estimate.py', 'images\parameters_en.png')
|
||||||
$localeDir = 'locale'
|
$localeDir = 'locale'
|
||||||
$source = $PSScriptRoot
|
$source = $PSScriptRoot
|
||||||
$destination = Join-Path $ExtensionsDir $FolderName
|
$destination = Join-Path $ExtensionsDir $FolderName
|
||||||
|
|
||||||
|
# Cartes de profondeur : environnement Python, modèles et cache, hors du
|
||||||
|
# dossier des extensions (Inkscape le parcourt en entier au démarrage).
|
||||||
|
# Mêmes noms que dans gray_iso_layers_depth.py.
|
||||||
|
$dataDir = Join-Path $env:LOCALAPPDATA 'GrayIsoLayers'
|
||||||
|
$packages = @('onnxruntime-directml', 'numpy', 'pillow')
|
||||||
|
$models = [ordered]@{
|
||||||
|
'midas_v21_small_256.onnx' = 'https://github.com/isl-org/MiDaS/releases/download/v2_1/model-small.onnx'
|
||||||
|
'depth_anything_v2_small.onnx' = 'https://huggingface.co/onnx-community/depth-anything-v2-small/resolve/main/onnx/model.onnx'
|
||||||
|
}
|
||||||
|
|
||||||
# --- Inkscape doit être fermé : les extensions sont lues au démarrage ---------
|
# --- Inkscape doit être fermé : les extensions sont lues au démarrage ---------
|
||||||
$running = Get-Process -Name 'inkscape' -ErrorAction SilentlyContinue
|
$running = Get-Process -Name 'inkscape' -ErrorAction SilentlyContinue
|
||||||
if ($running -and -not $Force) {
|
if ($running -and -not $Force) {
|
||||||
@ -59,6 +86,10 @@ if ($Uninstall) {
|
|||||||
else {
|
else {
|
||||||
Write-Host "Rien à supprimer : $destination n'existe pas." -ForegroundColor Yellow
|
Write-Host "Rien à supprimer : $destination n'existe pas." -ForegroundColor Yellow
|
||||||
}
|
}
|
||||||
|
if (Test-Path -LiteralPath $dataDir) {
|
||||||
|
Remove-Item -LiteralPath $dataDir -Recurse -Force
|
||||||
|
Write-Host "Supprimé : $dataDir" -ForegroundColor Green
|
||||||
|
}
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,6 +133,69 @@ if (Test-Path -LiteralPath $pycache) {
|
|||||||
Remove-Item -LiteralPath $pycache -Recurse -Force
|
Remove-Item -LiteralPath $pycache -Recurse -Force
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# --- Cartes de profondeur ----------------------------------------------------
|
||||||
|
if ($Depth) {
|
||||||
|
Write-Host ""
|
||||||
|
Write-Host "Cartes de profondeur : $dataDir"
|
||||||
|
$venvPython = Join-Path $dataDir 'venv\Scripts\python.exe'
|
||||||
|
if (-not (Test-Path -LiteralPath $venvPython)) {
|
||||||
|
# « python » avant le lanceur « py » : ce dernier prend la version la
|
||||||
|
# plus récente, parfois une préversion pour laquelle onnxruntime
|
||||||
|
# n'est pas encore publié.
|
||||||
|
$command = @()
|
||||||
|
if ($Python) {
|
||||||
|
$command = @($Python)
|
||||||
|
}
|
||||||
|
elseif (Get-Command python -ErrorAction SilentlyContinue) {
|
||||||
|
$command = @((Get-Command python).Source)
|
||||||
|
}
|
||||||
|
elseif (Get-Command py -ErrorAction SilentlyContinue) {
|
||||||
|
$command = @((Get-Command py).Source, '-3')
|
||||||
|
}
|
||||||
|
if ($command.Count -eq 0) {
|
||||||
|
Write-Error "Aucun Python 3 trouvé (python ou py) : installez-le depuis python.org, ou indiquez-le avec -Python."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
$arguments = @($command | Select-Object -Skip 1) + @('-m', 'venv', (Join-Path $dataDir 'venv'))
|
||||||
|
& $command[0] $arguments
|
||||||
|
if ($LASTEXITCODE -ne 0 -or -not (Test-Path -LiteralPath $venvPython)) {
|
||||||
|
Write-Error "Création de l'environnement Python impossible avec $($command -join ' ')."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
Write-Host " créé venv\"
|
||||||
|
}
|
||||||
|
& $venvPython -m pip install --quiet --disable-pip-version-check --upgrade @packages
|
||||||
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
Write-Error "Installation de $($packages -join ', ') impossible."
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
Write-Host " installé $($packages -join ', ')"
|
||||||
|
|
||||||
|
$modelsDir = Join-Path $dataDir 'models'
|
||||||
|
New-Item -ItemType Directory -Path $modelsDir -Force | Out-Null
|
||||||
|
foreach ($name in $models.Keys) {
|
||||||
|
$to = Join-Path $modelsDir $name
|
||||||
|
if (Test-Path -LiteralPath $to) {
|
||||||
|
Write-Host " présent models\$name"
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
# Téléchargement dans un fichier provisoire : un modèle tronqué par une
|
||||||
|
# coupure ne passe pas pour un modèle installé.
|
||||||
|
$partial = "$to.part"
|
||||||
|
$previous = $ProgressPreference
|
||||||
|
$ProgressPreference = 'SilentlyContinue' # la barre ralentit beaucoup PowerShell 5.1
|
||||||
|
try {
|
||||||
|
Invoke-WebRequest -Uri $models[$name] -OutFile $partial -UseBasicParsing
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
$ProgressPreference = $previous
|
||||||
|
}
|
||||||
|
Move-Item -LiteralPath $partial -Destination $to -Force
|
||||||
|
$size = [math]::Round((Get-Item -LiteralPath $to).Length / 1MB)
|
||||||
|
Write-Host " téléchargé models\$name ($size Mo)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "Extension déployée dans $destination" -ForegroundColor Green
|
Write-Host "Extension déployée dans $destination" -ForegroundColor Green
|
||||||
Write-Host "Redémarrez Inkscape, puis : Extensions > AlexDesign > Iso-couches par nuances de gris..."
|
Write-Host "Redémarrez Inkscape, puis : Extensions > AlexDesign > Iso-couches par nuances de gris..."
|
||||||
|
|||||||
122
depth_estimate.py
Normal file
122
depth_estimate.py
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# coding=utf-8
|
||||||
|
"""
|
||||||
|
Calcul d'une carte de profondeur, pour l'extension « Gray Iso-Layers ».
|
||||||
|
|
||||||
|
python depth_estimate.py image.png sortie.npy modele.onnx --model midas_small
|
||||||
|
|
||||||
|
Ce script ne tourne pas dans le Python d'Inkscape (une construction MinGW,
|
||||||
|
sans pip, ou onnxruntime ne s'installe pas) mais dans un environnement a
|
||||||
|
part, cree par `deploy.ps1 -Depth` ; l'extension le lance en sous-processus
|
||||||
|
(voir `gray_iso_layers_depth`). Dependances : onnxruntime (ou
|
||||||
|
onnxruntime-directml pour le calcul sur carte graphique), numpy, Pillow.
|
||||||
|
|
||||||
|
La sortie est un tableau numpy 2D float32 de profondeur inverse : plus la
|
||||||
|
valeur est haute, plus le point est proche.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
# Cote du carre presente a chaque modele.
|
||||||
|
MODELS = {"midas_small": 256, "depth_anything_v2_small": 518}
|
||||||
|
|
||||||
|
# Modeles calcules sur le processeur meme si une carte graphique est la :
|
||||||
|
# sous DirectML, MiDaS small rend une carte fausse, et son calcul est de
|
||||||
|
# toute facon plus court que l'initialisation de la carte graphique.
|
||||||
|
CPU_ONLY = {"midas_small"}
|
||||||
|
|
||||||
|
# Normalisation ImageNet, commune aux deux modeles.
|
||||||
|
_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
||||||
|
_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
||||||
|
|
||||||
|
_GPU = "DmlExecutionProvider"
|
||||||
|
_CPU = "CPUExecutionProvider"
|
||||||
|
|
||||||
|
|
||||||
|
def preprocess(picture, size):
|
||||||
|
"""Image PIL -> tenseur (1, 3, size, size) float32 attendu par le modele."""
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
rgb = picture.convert("RGB").resize((size, size), Image.BICUBIC)
|
||||||
|
pixels = (np.asarray(rgb, dtype=np.float32) / 255.0 - _MEAN) / _STD
|
||||||
|
return np.ascontiguousarray(pixels.transpose(2, 0, 1)[None], dtype=np.float32)
|
||||||
|
|
||||||
|
|
||||||
|
def choose_providers(available, cpu=False):
|
||||||
|
"""Fournisseurs de calcul a essayer, dans l'ordre : la carte graphique
|
||||||
|
(DirectML) si elle est disponible et non ecartee par `cpu`, puis le
|
||||||
|
processeur, toujours present."""
|
||||||
|
if cpu or _GPU not in available:
|
||||||
|
return [_CPU]
|
||||||
|
return [_GPU, _CPU]
|
||||||
|
|
||||||
|
|
||||||
|
def _run(weights, tensor, provider):
|
||||||
|
import onnxruntime
|
||||||
|
|
||||||
|
options = onnxruntime.SessionOptions()
|
||||||
|
options.log_severity_level = 3
|
||||||
|
if provider == _GPU:
|
||||||
|
# Exigences de DirectML.
|
||||||
|
options.enable_mem_pattern = False
|
||||||
|
options.execution_mode = onnxruntime.ExecutionMode.ORT_SEQUENTIAL
|
||||||
|
# Carte designee explicitement : sans cela, l'initialisation echoue
|
||||||
|
# sur une machine a deux cartes graphiques.
|
||||||
|
provider = (_GPU, {"device_id": 0})
|
||||||
|
session = onnxruntime.InferenceSession(weights, options, providers=[provider])
|
||||||
|
name = session.get_inputs()[0].name
|
||||||
|
# onnxruntime se replie de lui-meme sur le processeur si la carte
|
||||||
|
# graphique ne s'initialise pas : on rend le fournisseur reellement pris.
|
||||||
|
return session.run(None, {name: tensor})[0], session.get_providers()[0]
|
||||||
|
|
||||||
|
|
||||||
|
def estimate(picture, weights, model, cpu=False):
|
||||||
|
"""Profondeur inverse de l'image : `(tableau 2D float32, fournisseur)`.
|
||||||
|
|
||||||
|
Le calcul se fait sur la carte graphique si le modele s'y prete (voir
|
||||||
|
`CPU_ONLY`) ; s'il y echoue (pilote, memoire), il est refait sur le
|
||||||
|
processeur.
|
||||||
|
"""
|
||||||
|
import onnxruntime
|
||||||
|
|
||||||
|
tensor = preprocess(picture, MODELS[model])
|
||||||
|
providers = choose_providers(onnxruntime.get_available_providers(),
|
||||||
|
cpu or model in CPU_ONLY)
|
||||||
|
for provider in providers:
|
||||||
|
try:
|
||||||
|
depth, provider = _run(weights, tensor, provider)
|
||||||
|
depth = np.squeeze(depth)
|
||||||
|
except Exception:
|
||||||
|
if provider == providers[-1]:
|
||||||
|
raise
|
||||||
|
continue
|
||||||
|
if depth.ndim != 2:
|
||||||
|
raise ValueError("sortie du modele inattendue : {}".format(depth.shape))
|
||||||
|
return depth.astype(np.float32), provider
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0])
|
||||||
|
parser.add_argument("image")
|
||||||
|
parser.add_argument("output")
|
||||||
|
parser.add_argument("weights")
|
||||||
|
parser.add_argument("--model", choices=sorted(MODELS), default="midas_small")
|
||||||
|
parser.add_argument("--cpu", action="store_true",
|
||||||
|
help="ne pas utiliser la carte graphique")
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
with Image.open(args.image) as picture:
|
||||||
|
depth, provider = estimate(picture, args.weights, args.model, args.cpu)
|
||||||
|
with open(args.output, "wb") as handle:
|
||||||
|
np.save(handle, depth, allow_pickle=False)
|
||||||
|
print(provider)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
@ -13,6 +13,9 @@
|
|||||||
<page name="levels" gui-text="Levels">
|
<page name="levels" gui-text="Levels">
|
||||||
<param name="levels" type="int" min="2" max="64"
|
<param name="levels" type="int" min="2" max="64"
|
||||||
gui-text="Number of gray levels:">16</param>
|
gui-text="Number of gray levels:">16</param>
|
||||||
|
<param name="equalize" type="float" precision="0" min="0" max="100"
|
||||||
|
gui-text="Level distribution (% of equal areas):"
|
||||||
|
gui-description="0 keeps evenly spaced thresholds. 100 spreads the levels so that every board covers the same area: use it when most of the image sits in a few grays, as in a depth map.">0</param>
|
||||||
<param name="shapes" type="optiongroup" appearance="combo"
|
<param name="shapes" type="optiongroup" appearance="combo"
|
||||||
gui-text="Content of each board:">
|
gui-text="Content of each board:">
|
||||||
<option value="light">Stacked, lightest in front (board no. 1 = darkest, whole image)</option>
|
<option value="light">Stacked, lightest in front (board no. 1 = darkest, whole image)</option>
|
||||||
@ -40,6 +43,22 @@
|
|||||||
gui-text="Smooth the outlines with Bézier curves">true</param>
|
gui-text="Smooth the outlines with Bézier curves">true</param>
|
||||||
</page>
|
</page>
|
||||||
|
|
||||||
|
<page name="depth" gui-text="Depth">
|
||||||
|
<param name="depth_mix" type="float" precision="0" min="0" max="100"
|
||||||
|
gui-text="Depth map share (%):"
|
||||||
|
gui-description="0 uses the grays of the image alone, 100 its estimated depth alone. In between, the depth gives the volumes and the grays keep details such as the eyes and the mouth. Needs the depth models, installed by deploy.ps1 -Depth.">0</param>
|
||||||
|
<param name="depth_model" type="optiongroup" appearance="combo"
|
||||||
|
gui-text="Depth model:">
|
||||||
|
<option value="midas_small">Fast (MiDaS small)</option>
|
||||||
|
<option value="depth_anything_v2_small">Fine (Depth Anything V2 Small)</option>
|
||||||
|
</param>
|
||||||
|
<param name="depth_detail" type="float" precision="0" min="0" max="200"
|
||||||
|
gui-text="Fine details of the photo (%):"
|
||||||
|
gui-description="Carves the fine contrasts of the photo (eyes, mouth, eyebrows, strands of hair) into the depth, which has none of them. A plain background brings nothing and stays at the back. 0 keeps the smooth depth alone.">100</param>
|
||||||
|
<label>The depth of the scene is estimated from the photo: near areas become light, far areas dark. With boards stacked lightest in front, the nearest part of the subject ends up in front.</label>
|
||||||
|
<label>The estimate runs on the graphics card when possible and is kept in a cache: only the first run on an image takes a few seconds.</label>
|
||||||
|
</page>
|
||||||
|
|
||||||
<page name="style" gui-text="Style">
|
<page name="style" gui-text="Style">
|
||||||
<param name="fill_mode" type="optiongroup" appearance="combo" gui-text="Fill:">
|
<param name="fill_mode" type="optiongroup" appearance="combo" gui-text="Fill:">
|
||||||
<option value="gray">Gray of each level</option>
|
<option value="gray">Gray of each level</option>
|
||||||
@ -80,6 +99,8 @@ The image is smoothed, then split into the requested number of gray levels with
|
|||||||
|
|
||||||
Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With "Its own gray only", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.
|
Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With "Its own gray only", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.
|
||||||
|
|
||||||
|
For a relief that follows the real volumes of a portrait rather than its light and shadows, raise the depth map share (Depth tab): at 100, the boards follow the volumes, and the fine details setting carves the eyes and the mouth into them. A depth map made elsewhere (8 or 16 bits) can also be imported and selected like any image, with a share of 0. In both cases, if most boards come out empty or the subject only gets a few of them, raise the level distribution.
|
||||||
|
|
||||||
For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep "Preserve edges" checked.</label>
|
For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep "Preserve edges" checked.</label>
|
||||||
</page>
|
</page>
|
||||||
</param>
|
</param>
|
||||||
|
|||||||
@ -11,6 +11,7 @@ La logique vit dans `gray_iso_layers_core`, sans dependance a inkex.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
import hashlib
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@ -25,8 +26,16 @@ from inkex.localization import localize
|
|||||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
|
||||||
from gray_iso_layers_core import ( # noqa: E402
|
from gray_iso_layers_core import ( # noqa: E402
|
||||||
gray_to_hex, iso_boards, lines_to_d, parse_color, rings_to_d, scale_lines,
|
add_detail, blend_fields, gray_to_hex, guided_filter, iso_boards, lines_to_d, normalize,
|
||||||
scale_rings)
|
parse_color, rings_to_d, scale_lines, scale_rings)
|
||||||
|
from gray_iso_layers_depth import DepthUnavailable, depth_field # noqa: E402
|
||||||
|
|
||||||
|
# Modes Pillow a plus de 8 bits par canal (carte de profondeur en 16 bits).
|
||||||
|
_DEEP_MODES = ("I", "F")
|
||||||
|
|
||||||
|
# Grand cote de l'image envoyee au calcul de profondeur : les modeles
|
||||||
|
# travaillent a 518 points au plus.
|
||||||
|
_DEPTH_SIZE = 768
|
||||||
|
|
||||||
# Installe _() dans les builtins : catalogue « grayisolayers » de la langue
|
# Installe _() dans les builtins : catalogue « grayisolayers » de la langue
|
||||||
# de l'interface (Inkscape transmet domaine et dossier par l'environnement),
|
# de l'interface (Inkscape transmet domaine et dossier par l'environnement),
|
||||||
@ -43,6 +52,7 @@ class GrayIsoLayers(inkex.EffectExtension):
|
|||||||
# Un argument par <param> du .inx, memes noms et memes valeurs par defaut.
|
# Un argument par <param> du .inx, memes noms et memes valeurs par defaut.
|
||||||
pars.add_argument("--tab", default="levels")
|
pars.add_argument("--tab", default="levels")
|
||||||
pars.add_argument("--levels", type=int, default=16)
|
pars.add_argument("--levels", type=int, default=16)
|
||||||
|
pars.add_argument("--equalize", type=float, default=0.0)
|
||||||
pars.add_argument("--shapes", default="light")
|
pars.add_argument("--shapes", default="light")
|
||||||
pars.add_argument("--mark", type=inkex.Boolean, default=True)
|
pars.add_argument("--mark", type=inkex.Boolean, default=True)
|
||||||
pars.add_argument("--blur", type=float, default=1.0)
|
pars.add_argument("--blur", type=float, default=1.0)
|
||||||
@ -51,6 +61,9 @@ class GrayIsoLayers(inkex.EffectExtension):
|
|||||||
pars.add_argument("--min_area", type=float, default=0.02)
|
pars.add_argument("--min_area", type=float, default=0.02)
|
||||||
pars.add_argument("--simplify", type=float, default=0.4)
|
pars.add_argument("--simplify", type=float, default=0.4)
|
||||||
pars.add_argument("--smooth", type=inkex.Boolean, default=True)
|
pars.add_argument("--smooth", type=inkex.Boolean, default=True)
|
||||||
|
pars.add_argument("--depth_mix", type=float, default=0.0)
|
||||||
|
pars.add_argument("--depth_model", default="midas_small")
|
||||||
|
pars.add_argument("--depth_detail", type=float, default=100.0)
|
||||||
pars.add_argument("--fill_mode", default="gray")
|
pars.add_argument("--fill_mode", default="gray")
|
||||||
pars.add_argument("--fill_color", default="4294967295")
|
pars.add_argument("--fill_color", default="4294967295")
|
||||||
pars.add_argument("--stroke_width", type=float, default=0.2)
|
pars.add_argument("--stroke_width", type=float, default=0.2)
|
||||||
@ -93,12 +106,15 @@ class GrayIsoLayers(inkex.EffectExtension):
|
|||||||
"""Traite une image : calcul dans gray_iso_layers_core, ecriture dans le SVG."""
|
"""Traite une image : calcul dans gray_iso_layers_core, ecriture dans le SVG."""
|
||||||
opt = self.options
|
opt = self.options
|
||||||
try:
|
try:
|
||||||
field = self.load_field(image, opt.resolution)
|
picture, digest = self.load_picture(image)
|
||||||
|
field = self.gray_field(picture, opt.resolution)
|
||||||
except Exception as error: # fichier absent, format inconnu, lien casse...
|
except Exception as error: # fichier absent, format inconnu, lien casse...
|
||||||
inkex.errormsg(_("Cannot read the image '{}' ({}). Embed it in the "
|
inkex.errormsg(_("Cannot read the image '{}' ({}). Embed it in the "
|
||||||
"document (right-click > Embed Image) or check its "
|
"document (right-click > Embed Image) or check its "
|
||||||
"link.").format(image.get_id(), error))
|
"link.").format(image.get_id(), error))
|
||||||
return
|
return
|
||||||
|
if opt.depth_mix > 0:
|
||||||
|
field = self.add_depth(image, picture, digest, field)
|
||||||
|
|
||||||
rows, cols = field.shape
|
rows, cols = field.shape
|
||||||
boards = iso_boards(
|
boards = iso_boards(
|
||||||
@ -107,7 +123,8 @@ class GrayIsoLayers(inkex.EffectExtension):
|
|||||||
preserve_edges=opt.edges,
|
preserve_edges=opt.edges,
|
||||||
min_area=opt.min_area / 100.0,
|
min_area=opt.min_area / 100.0,
|
||||||
tolerance=opt.simplify,
|
tolerance=opt.simplify,
|
||||||
shapes=opt.shapes)
|
shapes=opt.shapes,
|
||||||
|
equalize=opt.equalize / 100.0)
|
||||||
if sum(1 for board in boards if board[2]) < 2:
|
if sum(1 for board in boards if board[2]) < 2:
|
||||||
inkex.errormsg(_("The image '{}' is uniform: only the base level has "
|
inkex.errormsg(_("The image '{}' is uniform: only the base level has "
|
||||||
"a shape. Reduce the smoothing or pick a more "
|
"a shape. Reduce the smoothing or pick a more "
|
||||||
@ -200,36 +217,104 @@ class GrayIsoLayers(inkex.EffectExtension):
|
|||||||
width, height = fitted_width, fitted_height
|
width, height = fitted_width, fitted_height
|
||||||
return left, top, width, height
|
return left, top, width, height
|
||||||
|
|
||||||
def load_field(self, image, resolution):
|
def add_depth(self, image, picture, digest, field):
|
||||||
"""Lit le bitmap et renvoie ses gris (0 noir, 1 blanc) en tableau 2D,
|
"""Melange aux gris `field` la profondeur estimee de l'image, dans la
|
||||||
ramene a `resolution` points sur son grand cote."""
|
proportion `depth_mix`. Si elle ne peut pas etre calculee, le dit et
|
||||||
|
rend les gris seuls."""
|
||||||
import numpy
|
import numpy
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
opt = self.options
|
||||||
|
try:
|
||||||
|
depth = depth_field(self.color_picture(picture), digest, opt.depth_model)
|
||||||
|
except DepthUnavailable as error:
|
||||||
|
inkex.errormsg(_("No depth map for the image '{}' ({}). Install the "
|
||||||
|
"depth models with 'deploy.ps1 -Depth'. The grays "
|
||||||
|
"of the image are used alone.").format(
|
||||||
|
image.get_id(), error))
|
||||||
|
return field
|
||||||
|
rows, cols = field.shape
|
||||||
|
depth = Image.fromarray(numpy.asarray(depth, dtype=numpy.float32))
|
||||||
|
depth = normalize(numpy.asarray(depth.resize((cols, rows), Image.BICUBIC),
|
||||||
|
dtype=float))
|
||||||
|
# La carte sort du modele en basse resolution : ses bords flous sont
|
||||||
|
# recales sur ceux de la photo.
|
||||||
|
depth = guided_filter(field, depth, max(2, int(round(max(rows, cols) / 100.0))))
|
||||||
|
# Les yeux et la bouche, absents de la profondeur, viennent des gris.
|
||||||
|
depth = add_detail(depth, field, opt.depth_detail / 100.0,
|
||||||
|
max(rows, cols) / 40.0)
|
||||||
|
return blend_fields(field, depth, opt.depth_mix / 100.0)
|
||||||
|
|
||||||
|
def load_picture(self, image):
|
||||||
|
"""Lit le bitmap : `(image PIL, empreinte de ses octets)`."""
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
href = image.get("xlink:href") or image.get("href") or ""
|
href = image.get("xlink:href") or image.get("href") or ""
|
||||||
if href.startswith("data:"):
|
if href.startswith("data:"):
|
||||||
header, _sep, payload = href.partition(",")
|
header, _sep, payload = href.partition(",")
|
||||||
data = base64.b64decode(payload) if ";base64" in header \
|
data = base64.b64decode(payload) if ";base64" in header \
|
||||||
else unquote_to_bytes(payload)
|
else unquote_to_bytes(payload)
|
||||||
source = io.BytesIO(data)
|
|
||||||
else:
|
else:
|
||||||
if href.startswith("file:"):
|
if href.startswith("file:"):
|
||||||
path = url2pathname(urlparse(href).path)
|
path = url2pathname(urlparse(href).path)
|
||||||
else:
|
else:
|
||||||
path = unquote(href)
|
path = unquote(href)
|
||||||
# Lien relatif : il part du dossier du document.
|
# Lien relatif : il part du dossier du document.
|
||||||
source = self.absolute_href(path)
|
with open(self.absolute_href(path), "rb") as handle:
|
||||||
|
data = handle.read()
|
||||||
|
picture = Image.open(io.BytesIO(data))
|
||||||
|
picture.load()
|
||||||
|
return picture, hashlib.sha1(data).hexdigest()
|
||||||
|
|
||||||
with Image.open(source) as picture:
|
@staticmethod
|
||||||
# La transparence se lit comme du blanc (fond de page).
|
def gray_picture(picture):
|
||||||
rgba = picture.convert("RGBA")
|
"""Gris de l'image, en image PIL : mode « L » (0 a 255), ou « F »
|
||||||
white = Image.new("RGBA", rgba.size, (255, 255, 255, 255))
|
(0 a 1) pour une image a plus de 8 bits par canal."""
|
||||||
gray = Image.alpha_composite(white, rgba).convert("L")
|
import numpy
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
if picture.mode.startswith(_DEEP_MODES):
|
||||||
|
# Carte de profondeur en 16 bits : Pillow l'ecreterait a 255 en la
|
||||||
|
# convertissant, on l'etire nous-memes sur 0..1.
|
||||||
|
values = normalize(numpy.asarray(picture, dtype=float))
|
||||||
|
return Image.fromarray(values.astype(numpy.float32))
|
||||||
|
# La transparence se lit comme du blanc (fond de page).
|
||||||
|
rgba = picture.convert("RGBA")
|
||||||
|
white = Image.new("RGBA", rgba.size, (255, 255, 255, 255))
|
||||||
|
return Image.alpha_composite(white, rgba).convert("L")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def gray_field(cls, picture, resolution):
|
||||||
|
"""Gris de l'image (0 noir, 1 blanc) en tableau 2D, ramene a
|
||||||
|
`resolution` points sur son grand cote."""
|
||||||
|
import numpy
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
gray = cls.gray_picture(picture)
|
||||||
zoom = resolution / float(max(gray.size))
|
zoom = resolution / float(max(gray.size))
|
||||||
size = (max(2, int(round(gray.width * zoom))),
|
size = (max(2, int(round(gray.width * zoom))),
|
||||||
max(2, int(round(gray.height * zoom))))
|
max(2, int(round(gray.height * zoom))))
|
||||||
gray = gray.resize(size, Image.LANCZOS)
|
gray = gray.resize(size, Image.LANCZOS)
|
||||||
return numpy.asarray(gray, dtype=float) / 255.0
|
scale = 1.0 if gray.mode == "F" else 255.0
|
||||||
|
return numpy.asarray(gray, dtype=float) / scale
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def color_picture(cls, picture):
|
||||||
|
"""Image RGB envoyee au calcul de profondeur, transparence en blanc,
|
||||||
|
reduite a `_DEPTH_SIZE` points sur son grand cote."""
|
||||||
|
import numpy
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
if picture.mode.startswith(_DEEP_MODES):
|
||||||
|
gray = numpy.asarray(cls.gray_picture(picture), dtype=float)
|
||||||
|
color = Image.fromarray((gray * 255.0).round().astype(numpy.uint8))
|
||||||
|
color = color.convert("RGB")
|
||||||
|
else:
|
||||||
|
rgba = picture.convert("RGBA")
|
||||||
|
white = Image.new("RGBA", rgba.size, (255, 255, 255, 255))
|
||||||
|
color = Image.alpha_composite(white, rgba).convert("RGB")
|
||||||
|
color.thumbnail((_DEPTH_SIZE, _DEPTH_SIZE), Image.LANCZOS)
|
||||||
|
return color
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@ -163,6 +163,70 @@ def normalize(field):
|
|||||||
return (field - low) / (high - low)
|
return (field - low) / (high - low)
|
||||||
|
|
||||||
|
|
||||||
|
def spread_levels(field, amount=1.0, bins=1024):
|
||||||
|
"""Repartit les valeurs du champ pour que les niveaux aient la meme aire.
|
||||||
|
|
||||||
|
Le champ est remappe par sa fonction de repartition (egalisation
|
||||||
|
d'histogramme) : des seuils reguliers decoupent alors des surfaces egales,
|
||||||
|
meme si presque toutes les valeurs sont tassees sur une plage etroite
|
||||||
|
(carte de profondeur : fond lointain etendu, sujet resserre). `amount`
|
||||||
|
dose l'effet, de 0 (champ etire sur 0..1, sans plus) a 1 (aires egales).
|
||||||
|
"""
|
||||||
|
field = normalize(field)
|
||||||
|
amount = min(1.0, max(0.0, float(amount)))
|
||||||
|
if amount <= 0:
|
||||||
|
return field
|
||||||
|
counts, edges = np.histogram(field, bins=bins, range=(0.0, 1.0))
|
||||||
|
share = np.concatenate([[0.0], np.cumsum(counts)]) / float(field.size)
|
||||||
|
return (1.0 - amount) * field + amount * np.interp(field, edges, share)
|
||||||
|
|
||||||
|
|
||||||
|
def guided_filter(guide, source, radius, eps=1e-3):
|
||||||
|
"""Lisse `source` en lui donnant les contours de `guide` (filtre guide).
|
||||||
|
|
||||||
|
Dans chaque fenetre de `radius` echantillons, `source` est approche par
|
||||||
|
une fonction affine de `guide` : un bord flou de `source` (carte de
|
||||||
|
profondeur calculee en basse resolution) se recale sur le bord net
|
||||||
|
correspondant de `guide` (la photo). `eps` est la variance de `guide` en
|
||||||
|
dessous de laquelle une zone est tenue pour calme, donc simplement lissee.
|
||||||
|
"""
|
||||||
|
guide = np.asarray(guide, dtype=float)
|
||||||
|
source = np.asarray(source, dtype=float)
|
||||||
|
radius = int(radius)
|
||||||
|
if radius < 1:
|
||||||
|
return source.copy()
|
||||||
|
mean_guide, mean_source = box_blur(guide, radius), box_blur(source, radius)
|
||||||
|
covariance = box_blur(guide * source, radius) - mean_guide * mean_source
|
||||||
|
variance = np.maximum(box_blur(guide * guide, radius) - mean_guide ** 2, 0.0)
|
||||||
|
slope = covariance / (variance + eps)
|
||||||
|
offset = mean_source - slope * mean_guide
|
||||||
|
return box_blur(slope, radius) * guide + box_blur(offset, radius)
|
||||||
|
|
||||||
|
|
||||||
|
def add_detail(depth, gray, amount, sigma):
|
||||||
|
"""Grave dans la profondeur les contrastes fins des gris.
|
||||||
|
|
||||||
|
Une carte de profondeur donne les volumes mais gomme les yeux, la bouche,
|
||||||
|
les sourcils. On lui ajoute les gris prives de leurs grandes variations
|
||||||
|
(ecart a leur moyenne locale, sur `sigma` echantillons) : un trait sombre
|
||||||
|
se creuse, un reflet ressort, alors qu'un fond uni, clair ou sombre,
|
||||||
|
n'apporte rien et reste a sa profondeur. `amount` dose l'effet (0 : la
|
||||||
|
profondeur seule). Le resultat reste sur 0..1.
|
||||||
|
"""
|
||||||
|
depth = normalize(depth)
|
||||||
|
if amount <= 0:
|
||||||
|
return depth
|
||||||
|
gray = normalize(gray)
|
||||||
|
return np.clip(depth + amount * (gray - gaussian_blur(gray, sigma)), 0.0, 1.0)
|
||||||
|
|
||||||
|
|
||||||
|
def blend_fields(gray, depth, mix):
|
||||||
|
"""Melange les gris et la profondeur, chacun etire sur 0..1 : `mix` = 0
|
||||||
|
garde les gris seuls, 1 la profondeur seule."""
|
||||||
|
mix = min(1.0, max(0.0, float(mix)))
|
||||||
|
return (1.0 - mix) * normalize(gray) + mix * normalize(depth)
|
||||||
|
|
||||||
|
|
||||||
def thresholds(levels):
|
def thresholds(levels):
|
||||||
"""Seuils reguliers separant `levels` niveaux de gris sur 0..1."""
|
"""Seuils reguliers separant `levels` niveaux de gris sur 0..1."""
|
||||||
return [k / float(levels) for k in range(1, levels)]
|
return [k / float(levels) for k in range(1, levels)]
|
||||||
@ -536,13 +600,15 @@ def region_rings(field, lines, inside):
|
|||||||
# --------------------------------------------------------------------------
|
# --------------------------------------------------------------------------
|
||||||
|
|
||||||
def iso_boards(field, levels=16, blur=0.0, min_area=0.0, tolerance=0.0,
|
def iso_boards(field, levels=16, blur=0.0, min_area=0.0, tolerance=0.0,
|
||||||
shapes="light", preserve_edges=False):
|
shapes="light", preserve_edges=False, equalize=0.0):
|
||||||
"""Decoupe un champ de gris en une planche par niveau.
|
"""Decoupe un champ de gris en une planche par niveau.
|
||||||
|
|
||||||
Le champ est lisse (`blur` en echantillons, `preserve_edges` : voir
|
Le champ est lisse (`blur` en echantillons, `preserve_edges` : voir
|
||||||
`smooth`), etire sur 0..1 puis quantifie en `levels` niveaux a seuils
|
`smooth`), etire sur 0..1 puis quantifie en `levels` niveaux a seuils
|
||||||
reguliers : le niveau k couvre les valeurs de k / levels a
|
reguliers : le niveau k couvre les valeurs de k / levels a
|
||||||
(k + 1) / levels. Renvoie les planches du fond vers l'avant de la pile :
|
(k + 1) / levels. Avec `equalize` (0 a 1, voir `spread_levels`), les
|
||||||
|
valeurs sont d'abord reparties pour que les niveaux tendent vers des
|
||||||
|
aires egales. Renvoie les planches du fond vers l'avant de la pile :
|
||||||
`(niveau, gris, rings, mark)` avec `niveau` de 0 (le plus sombre) a
|
`(niveau, gris, rings, mark)` avec `niveau` de 0 (le plus sombre) a
|
||||||
levels - 1 et `gris` entre 0 et 1.
|
levels - 1 et `gris` entre 0 et 1.
|
||||||
|
|
||||||
@ -570,6 +636,8 @@ def iso_boards(field, levels=16, blur=0.0, min_area=0.0, tolerance=0.0,
|
|||||||
height, width = field.shape
|
height, width = field.shape
|
||||||
levels = max(2, int(levels))
|
levels = max(2, int(levels))
|
||||||
field = smooth(field, blur, preserve_edges)
|
field = smooth(field, blur, preserve_edges)
|
||||||
|
if equalize > 0:
|
||||||
|
field = spread_levels(field, equalize)
|
||||||
smallest = min_area * (width - 1) * (height - 1)
|
smallest = min_area * (width - 1) * (height - 1)
|
||||||
|
|
||||||
cuts = thresholds(levels)
|
cuts = thresholds(levels)
|
||||||
@ -601,11 +669,12 @@ def iso_boards(field, levels=16, blur=0.0, min_area=0.0, tolerance=0.0,
|
|||||||
|
|
||||||
|
|
||||||
def iso_layers(field, levels=16, blur=0.0, min_area=0.0, tolerance=0.0,
|
def iso_layers(field, levels=16, blur=0.0, min_area=0.0, tolerance=0.0,
|
||||||
shapes="band", preserve_edges=False):
|
shapes="band", preserve_edges=False, equalize=0.0):
|
||||||
"""Comme `iso_boards`, sans les reperes d'assemblage : liste de
|
"""Comme `iso_boards`, sans les reperes d'assemblage : liste de
|
||||||
`(niveau, gris, rings)`."""
|
`(niveau, gris, rings)`."""
|
||||||
return [board[:3] for board in iso_boards(field, levels, blur, min_area,
|
return [board[:3] for board in iso_boards(field, levels, blur, min_area,
|
||||||
tolerance, shapes, preserve_edges)]
|
tolerance, shapes, preserve_edges,
|
||||||
|
equalize)]
|
||||||
|
|
||||||
|
|
||||||
def image_ring(width, height):
|
def image_ring(width, height):
|
||||||
|
|||||||
133
gray_iso_layers_depth.py
Normal file
133
gray_iso_layers_depth.py
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
# coding=utf-8
|
||||||
|
"""
|
||||||
|
Carte de profondeur d'une image, pour l'extension « Gray Iso-Layers ».
|
||||||
|
|
||||||
|
Le Python d'Inkscape ne peut pas charger onnxruntime : le calcul est confie
|
||||||
|
au script `depth_estimate.py`, lance avec le Python d'un environnement a part
|
||||||
|
(cree par `deploy.ps1 -Depth`). Le resultat est garde sur disque, car
|
||||||
|
Inkscape relance l'extension a chaque « Appliquer » : seul le premier calcul
|
||||||
|
d'une image coute l'inference.
|
||||||
|
|
||||||
|
Dossier de donnees (hors du dossier des extensions, qu'Inkscape parcourt en
|
||||||
|
entier au demarrage) : `venv/`, `models/` et `cache/`.
|
||||||
|
|
||||||
|
Sans dependance a inkex. Dependances : numpy et Pillow (fournis avec Inkscape).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
# Modeles proposes : nom (parametre depth_model du .inx) -> fichier de poids.
|
||||||
|
MODELS = {
|
||||||
|
"midas_small": "midas_v21_small_256.onnx",
|
||||||
|
"depth_anything_v2_small": "depth_anything_v2_small.onnx",
|
||||||
|
}
|
||||||
|
|
||||||
|
CACHE_LIMIT = 30 # cartes gardees sur disque
|
||||||
|
TIMEOUT = 120 # secondes laissees au calcul
|
||||||
|
|
||||||
|
|
||||||
|
class DepthUnavailable(Exception):
|
||||||
|
"""La carte de profondeur ne peut pas etre calculee (environnement ou
|
||||||
|
modele absent, echec du calcul). Le message en donne la cause."""
|
||||||
|
|
||||||
|
|
||||||
|
def data_dir():
|
||||||
|
"""Dossier de l'environnement Python, des modeles et du cache."""
|
||||||
|
if os.name == "nt":
|
||||||
|
base = os.environ.get("LOCALAPPDATA") or os.path.expanduser("~")
|
||||||
|
else:
|
||||||
|
base = os.path.join(os.path.expanduser("~"), ".local", "share")
|
||||||
|
return os.path.join(base, "GrayIsoLayers")
|
||||||
|
|
||||||
|
|
||||||
|
def find_python():
|
||||||
|
"""Python qui execute `depth_estimate.py` : celui de la variable
|
||||||
|
GRAY_ISO_LAYERS_PYTHON, sinon celui de l'environnement du dossier de
|
||||||
|
donnees."""
|
||||||
|
chosen = os.environ.get("GRAY_ISO_LAYERS_PYTHON")
|
||||||
|
if chosen:
|
||||||
|
return chosen
|
||||||
|
if os.name == "nt":
|
||||||
|
return os.path.join(data_dir(), "venv", "Scripts", "python.exe")
|
||||||
|
return os.path.join(data_dir(), "venv", "bin", "python")
|
||||||
|
|
||||||
|
|
||||||
|
def depth_field(picture, digest, model="midas_small"):
|
||||||
|
"""Profondeur inverse de `picture` (image PIL RGB) : tableau 2D, plus la
|
||||||
|
valeur est haute, plus le point est proche. Sa taille est celle de la
|
||||||
|
sortie du modele, pas celle de l'image.
|
||||||
|
|
||||||
|
`digest` identifie l'image (empreinte de ses octets) : avec le nom du
|
||||||
|
modele, c'est la cle du cache. Leve `DepthUnavailable` si le calcul est
|
||||||
|
impossible.
|
||||||
|
"""
|
||||||
|
if model not in MODELS:
|
||||||
|
raise DepthUnavailable("unknown model '{}'".format(model))
|
||||||
|
cache = os.path.join(data_dir(), "cache")
|
||||||
|
target = os.path.join(cache, "{}-{}.npy".format(digest, model))
|
||||||
|
if os.path.isfile(target):
|
||||||
|
try:
|
||||||
|
depth = np.load(target, allow_pickle=False)
|
||||||
|
os.utime(target) # carte resservie : la derniere a etre purgee
|
||||||
|
return depth
|
||||||
|
except (OSError, ValueError):
|
||||||
|
pass # fichier abime : on recalcule
|
||||||
|
|
||||||
|
python = find_python()
|
||||||
|
script = os.path.join(HERE, "depth_estimate.py")
|
||||||
|
weights = os.path.join(data_dir(), "models", MODELS[model])
|
||||||
|
for path in (python, script, weights):
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
raise DepthUnavailable("missing file: {}".format(path))
|
||||||
|
|
||||||
|
# Inkscape pointe PYTHONPATH et PYTHONHOME vers son propre Python : ils
|
||||||
|
# feraient charger ses modules par celui de l'environnement.
|
||||||
|
env = {key: value for key, value in os.environ.items()
|
||||||
|
if key.upper() not in ("PYTHONPATH", "PYTHONHOME")}
|
||||||
|
with tempfile.TemporaryDirectory() as folder:
|
||||||
|
source = os.path.join(folder, "image.png")
|
||||||
|
output = os.path.join(folder, "depth.npy")
|
||||||
|
picture.save(source)
|
||||||
|
try:
|
||||||
|
done = subprocess.run(
|
||||||
|
[python, script, source, output, weights, "--model", model],
|
||||||
|
env=env, capture_output=True, encoding="utf-8", errors="replace",
|
||||||
|
timeout=TIMEOUT,
|
||||||
|
# Pas de fenetre de console sous Windows.
|
||||||
|
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0))
|
||||||
|
except (OSError, subprocess.TimeoutExpired) as error:
|
||||||
|
raise DepthUnavailable(str(error))
|
||||||
|
if done.returncode != 0 or not os.path.isfile(output):
|
||||||
|
lines = (done.stderr or "").strip().splitlines()
|
||||||
|
raise DepthUnavailable(lines[-1] if lines else
|
||||||
|
"exit code {}".format(done.returncode))
|
||||||
|
try:
|
||||||
|
depth = np.load(output, allow_pickle=False)
|
||||||
|
except (OSError, ValueError) as error:
|
||||||
|
raise DepthUnavailable(str(error))
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.makedirs(cache, exist_ok=True)
|
||||||
|
np.save(target, depth, allow_pickle=False)
|
||||||
|
_prune(cache)
|
||||||
|
except OSError as error:
|
||||||
|
# Le cache n'est qu'un confort : la carte est calculee, on la rend.
|
||||||
|
print("gray_iso_layers_depth: {}".format(error), file=sys.stderr)
|
||||||
|
return depth
|
||||||
|
|
||||||
|
|
||||||
|
def _prune(cache, limit=None):
|
||||||
|
"""Ne garde que les `limit` cartes les plus recemment servies."""
|
||||||
|
limit = CACHE_LIMIT if limit is None else limit
|
||||||
|
files = [os.path.join(cache, name) for name in os.listdir(cache)
|
||||||
|
if name.endswith(".npy")]
|
||||||
|
files.sort(key=os.path.getmtime, reverse=True)
|
||||||
|
for path in files[limit:]:
|
||||||
|
os.remove(path)
|
||||||
Binary file not shown.
Binary file not shown.
56
po/en.po
56
po/en.po
@ -21,6 +21,14 @@ msgstr "Levels"
|
|||||||
msgid "Number of gray levels:"
|
msgid "Number of gray levels:"
|
||||||
msgstr "Number of gray levels:"
|
msgstr "Number of gray levels:"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Level distribution (% of equal areas):"
|
||||||
|
msgstr "Level distribution (% of equal areas):"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "0 keeps evenly spaced thresholds. 100 spreads the levels so that every board covers the same area: use it when most of the image sits in a few grays, as in a depth map."
|
||||||
|
msgstr "0 keeps evenly spaced thresholds. 100 spreads the levels so that every board covers the same area: use it when most of the image sits in a few grays, as in a depth map."
|
||||||
|
|
||||||
#: gray_iso_layers.inx
|
#: gray_iso_layers.inx
|
||||||
msgid "Content of each board:"
|
msgid "Content of each board:"
|
||||||
msgstr "Content of each board:"
|
msgstr "Content of each board:"
|
||||||
@ -85,6 +93,46 @@ msgstr "Largest deviation allowed when removing nodes. 0 keeps every node."
|
|||||||
msgid "Smooth the outlines with Bézier curves"
|
msgid "Smooth the outlines with Bézier curves"
|
||||||
msgstr "Smooth the outlines with Bézier curves"
|
msgstr "Smooth the outlines with Bézier curves"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth"
|
||||||
|
msgstr "Depth"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth map share (%):"
|
||||||
|
msgstr "Depth map share (%):"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "0 uses the grays of the image alone, 100 its estimated depth alone. In between, the depth gives the volumes and the grays keep details such as the eyes and the mouth. Needs the depth models, installed by deploy.ps1 -Depth."
|
||||||
|
msgstr "0 uses the grays of the image alone, 100 its estimated depth alone. In between, the depth gives the volumes and the grays keep details such as the eyes and the mouth. Needs the depth models, installed by deploy.ps1 -Depth."
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth model:"
|
||||||
|
msgstr "Depth model:"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fast (MiDaS small)"
|
||||||
|
msgstr "Fast (MiDaS small)"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fine (Depth Anything V2 Small)"
|
||||||
|
msgstr "Fine (Depth Anything V2 Small)"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fine details of the photo (%):"
|
||||||
|
msgstr "Fine details of the photo (%):"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Carves the fine contrasts of the photo (eyes, mouth, eyebrows, strands of hair) into the depth, which has none of them. A plain background brings nothing and stays at the back. 0 keeps the smooth depth alone."
|
||||||
|
msgstr "Carves the fine contrasts of the photo (eyes, mouth, eyebrows, strands of hair) into the depth, which has none of them. A plain background brings nothing and stays at the back. 0 keeps the smooth depth alone."
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "The depth of the scene is estimated from the photo: near areas become light, far areas dark. With boards stacked lightest in front, the nearest part of the subject ends up in front."
|
||||||
|
msgstr "The depth of the scene is estimated from the photo: near areas become light, far areas dark. With boards stacked lightest in front, the nearest part of the subject ends up in front."
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "The estimate runs on the graphics card when possible and is kept in a cache: only the first run on an image takes a few seconds."
|
||||||
|
msgstr "The estimate runs on the graphics card when possible and is kept in a cache: only the first run on an image takes a few seconds."
|
||||||
|
|
||||||
#: gray_iso_layers.inx
|
#: gray_iso_layers.inx
|
||||||
msgid "Style"
|
msgid "Style"
|
||||||
msgstr "Style"
|
msgstr "Style"
|
||||||
@ -141,6 +189,8 @@ msgid ""
|
|||||||
"\n"
|
"\n"
|
||||||
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"For a relief that follows the real volumes of a portrait rather than its light and shadows, raise the depth map share (Depth tab): at 100, the boards follow the volumes, and the fine details setting carves the eyes and the mouth into them. A depth map made elsewhere (8 or 16 bits) can also be imported and selected like any image, with a share of 0. In both cases, if most boards come out empty or the subject only gets a few of them, raise the level distribution.\n"
|
||||||
|
"\n"
|
||||||
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Select one or more bitmap images (imported photo, height map, blurred noise), then run the extension. Colors are read as grays and transparency as white.\n"
|
"Select one or more bitmap images (imported photo, height map, blurred noise), then run the extension. Colors are read as grays and transparency as white.\n"
|
||||||
@ -149,6 +199,8 @@ msgstr ""
|
|||||||
"\n"
|
"\n"
|
||||||
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"For a relief that follows the real volumes of a portrait rather than its light and shadows, raise the depth map share (Depth tab): at 100, the boards follow the volumes, and the fine details setting carves the eyes and the mouth into them. A depth map made elsewhere (8 or 16 bits) can also be imported and selected like any image, with a share of 0. In both cases, if most boards come out empty or the subject only gets a few of them, raise the level distribution.\n"
|
||||||
|
"\n"
|
||||||
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
||||||
|
|
||||||
#: gray_iso_layers.py
|
#: gray_iso_layers.py
|
||||||
@ -178,3 +230,7 @@ msgstr "Cut"
|
|||||||
#: gray_iso_layers.py
|
#: gray_iso_layers.py
|
||||||
msgid "Marking of board no. {}"
|
msgid "Marking of board no. {}"
|
||||||
msgstr "Marking of board no. {}"
|
msgstr "Marking of board no. {}"
|
||||||
|
|
||||||
|
#: gray_iso_layers.py
|
||||||
|
msgid "No depth map for the image '{}' ({}). Install the depth models with 'deploy.ps1 -Depth'. The grays of the image are used alone."
|
||||||
|
msgstr "No depth map for the image '{}' ({}). Install the depth models with 'deploy.ps1 -Depth'. The grays of the image are used alone."
|
||||||
|
|||||||
56
po/fr.po
56
po/fr.po
@ -21,6 +21,14 @@ msgstr "Niveaux"
|
|||||||
msgid "Number of gray levels:"
|
msgid "Number of gray levels:"
|
||||||
msgstr "Nombre de niveaux de gris :"
|
msgstr "Nombre de niveaux de gris :"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Level distribution (% of equal areas):"
|
||||||
|
msgstr "Répartition des niveaux (% d'aires égales) :"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "0 keeps evenly spaced thresholds. 100 spreads the levels so that every board covers the same area: use it when most of the image sits in a few grays, as in a depth map."
|
||||||
|
msgstr "0 garde des seuils régulièrement espacés. 100 répartit les niveaux pour que chaque planche couvre la même surface : à utiliser quand presque toute l'image tient dans quelques gris, comme dans une carte de profondeur."
|
||||||
|
|
||||||
#: gray_iso_layers.inx
|
#: gray_iso_layers.inx
|
||||||
msgid "Content of each board:"
|
msgid "Content of each board:"
|
||||||
msgstr "Contenu de chaque planche :"
|
msgstr "Contenu de chaque planche :"
|
||||||
@ -85,6 +93,46 @@ msgstr "Écart maximal toléré lors de la suppression de nœuds. 0 conserve tou
|
|||||||
msgid "Smooth the outlines with Bézier curves"
|
msgid "Smooth the outlines with Bézier curves"
|
||||||
msgstr "Lisser les contours avec des courbes de Bézier"
|
msgstr "Lisser les contours avec des courbes de Bézier"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth"
|
||||||
|
msgstr "Profondeur"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth map share (%):"
|
||||||
|
msgstr "Part de la carte de profondeur (%) :"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "0 uses the grays of the image alone, 100 its estimated depth alone. In between, the depth gives the volumes and the grays keep details such as the eyes and the mouth. Needs the depth models, installed by deploy.ps1 -Depth."
|
||||||
|
msgstr "0 utilise les gris de l'image seuls, 100 sa profondeur estimée seule. Entre les deux, la profondeur donne les volumes et les gris gardent les détails comme les yeux et la bouche. Nécessite les modèles de profondeur, installés par deploy.ps1 -Depth."
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth model:"
|
||||||
|
msgstr "Modèle de profondeur :"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fast (MiDaS small)"
|
||||||
|
msgstr "Rapide (MiDaS small)"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fine (Depth Anything V2 Small)"
|
||||||
|
msgstr "Fin (Depth Anything V2 Small)"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fine details of the photo (%):"
|
||||||
|
msgstr "Détails fins de la photo (%) :"
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Carves the fine contrasts of the photo (eyes, mouth, eyebrows, strands of hair) into the depth, which has none of them. A plain background brings nothing and stays at the back. 0 keeps the smooth depth alone."
|
||||||
|
msgstr "Grave dans la profondeur les contrastes fins de la photo (yeux, bouche, sourcils, mèches de cheveux), qu'elle ne contient pas. Un fond uni n'apporte rien et reste au fond. 0 garde la profondeur lisse seule."
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "The depth of the scene is estimated from the photo: near areas become light, far areas dark. With boards stacked lightest in front, the nearest part of the subject ends up in front."
|
||||||
|
msgstr "La profondeur de la scène est estimée à partir de la photo : les zones proches deviennent claires, les zones lointaines sombres. Avec des planches empilées, la plus claire devant, la partie la plus proche du sujet se retrouve devant."
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "The estimate runs on the graphics card when possible and is kept in a cache: only the first run on an image takes a few seconds."
|
||||||
|
msgstr "L'estimation se fait sur la carte graphique quand c'est possible et elle est gardée en cache : seul le premier calcul d'une image prend quelques secondes."
|
||||||
|
|
||||||
#: gray_iso_layers.inx
|
#: gray_iso_layers.inx
|
||||||
msgid "Style"
|
msgid "Style"
|
||||||
msgstr "Style"
|
msgstr "Style"
|
||||||
@ -141,6 +189,8 @@ msgid ""
|
|||||||
"\n"
|
"\n"
|
||||||
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"For a relief that follows the real volumes of a portrait rather than its light and shadows, raise the depth map share (Depth tab): at 100, the boards follow the volumes, and the fine details setting carves the eyes and the mouth into them. A depth map made elsewhere (8 or 16 bits) can also be imported and selected like any image, with a share of 0. In both cases, if most boards come out empty or the subject only gets a few of them, raise the level distribution.\n"
|
||||||
|
"\n"
|
||||||
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Sélectionnez une ou plusieurs images bitmap (photo importée, carte de relief, bruit flouté), puis lancez l'extension. Les couleurs sont lues comme des gris et la transparence comme du blanc.\n"
|
"Sélectionnez une ou plusieurs images bitmap (photo importée, carte de relief, bruit flouté), puis lancez l'extension. Les couleurs sont lues comme des gris et la transparence comme du blanc.\n"
|
||||||
@ -149,6 +199,8 @@ msgstr ""
|
|||||||
"\n"
|
"\n"
|
||||||
"Chaque niveau de gris va dans son propre calque, nommé Planche n° 1, Planche n° 2… du fond vers l'avant, ajouté au-dessus de l'image : il y a donc autant de nouveaux calques que de niveaux de gris. Les planches sont faites pour être découpées puis empilées : la planche n° 1 couvre toute l'image et chaque planche suivante, plus petite, se pose devant la précédente, la plus claire (ou la plus sombre) devant. Dans chaque calque, le contour de découpe de la planche est tracé dans la couleur de découpe (rouge), et le contour de la planche suivante est reporté dans la couleur de marquage (noir), pour montrer où la coller. Avec « Son gris seul », une planche contient uniquement l'aplat de son gris : les planches sont côte à côte sans se recouvrir et ne portent aucun marquage. Plusieurs images sélectionnées partagent les mêmes calques.\n"
|
"Chaque niveau de gris va dans son propre calque, nommé Planche n° 1, Planche n° 2… du fond vers l'avant, ajouté au-dessus de l'image : il y a donc autant de nouveaux calques que de niveaux de gris. Les planches sont faites pour être découpées puis empilées : la planche n° 1 couvre toute l'image et chaque planche suivante, plus petite, se pose devant la précédente, la plus claire (ou la plus sombre) devant. Dans chaque calque, le contour de découpe de la planche est tracé dans la couleur de découpe (rouge), et le contour de la planche suivante est reporté dans la couleur de marquage (noir), pour montrer où la coller. Avec « Son gris seul », une planche contient uniquement l'aplat de son gris : les planches sont côte à côte sans se recouvrir et ne portent aucun marquage. Plusieurs images sélectionnées partagent les mêmes calques.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"Pour un relief qui suit les vrais volumes d'un portrait plutôt que ses lumières et ses ombres, augmentez la part de la carte de profondeur (onglet Profondeur) : à 100, les planches suivent les volumes, et le réglage des détails fins y grave les yeux et la bouche. Une carte de profondeur produite ailleurs (8 ou 16 bits) peut aussi être importée et sélectionnée comme n'importe quelle image, avec une part de 0. Dans les deux cas, si la plupart des planches sortent vides ou si le sujet n'en reçoit que quelques-unes, augmentez la répartition des niveaux.\n"
|
||||||
|
"\n"
|
||||||
"Pour la découpe laser, choisissez aucun remplissage : il ne reste que les traits de découpe et de marquage. Pour un dessin au trait seul, choisissez un remplissage de couleur unique blanche, ou aucun remplissage, avec un trait noir et sans marquage. Si les contours sont trop chargés, augmentez le lissage ou le plus petit îlot ; s'ils paraissent anguleux, augmentez le lissage ou la simplification. Si un visage n'est plus reconnaissable, réduisez le lissage et laissez « Préserver les contours » coché."
|
"Pour la découpe laser, choisissez aucun remplissage : il ne reste que les traits de découpe et de marquage. Pour un dessin au trait seul, choisissez un remplissage de couleur unique blanche, ou aucun remplissage, avec un trait noir et sans marquage. Si les contours sont trop chargés, augmentez le lissage ou le plus petit îlot ; s'ils paraissent anguleux, augmentez le lissage ou la simplification. Si un visage n'est plus reconnaissable, réduisez le lissage et laissez « Préserver les contours » coché."
|
||||||
|
|
||||||
#: gray_iso_layers.py
|
#: gray_iso_layers.py
|
||||||
@ -178,3 +230,7 @@ msgstr "Découpe"
|
|||||||
#: gray_iso_layers.py
|
#: gray_iso_layers.py
|
||||||
msgid "Marking of board no. {}"
|
msgid "Marking of board no. {}"
|
||||||
msgstr "Repère de la planche n° {}"
|
msgstr "Repère de la planche n° {}"
|
||||||
|
|
||||||
|
#: gray_iso_layers.py
|
||||||
|
msgid "No depth map for the image '{}' ({}). Install the depth models with 'deploy.ps1 -Depth'. The grays of the image are used alone."
|
||||||
|
msgstr "Pas de carte de profondeur pour l'image « {} » ({}). Installez les modèles de profondeur avec « deploy.ps1 -Depth ». Les gris de l'image sont utilisés seuls."
|
||||||
|
|||||||
@ -19,6 +19,14 @@ msgstr ""
|
|||||||
msgid "Number of gray levels:"
|
msgid "Number of gray levels:"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Level distribution (% of equal areas):"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "0 keeps evenly spaced thresholds. 100 spreads the levels so that every board covers the same area: use it when most of the image sits in a few grays, as in a depth map."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: gray_iso_layers.inx
|
#: gray_iso_layers.inx
|
||||||
msgid "Content of each board:"
|
msgid "Content of each board:"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@ -83,6 +91,46 @@ msgstr ""
|
|||||||
msgid "Smooth the outlines with Bézier curves"
|
msgid "Smooth the outlines with Bézier curves"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth map share (%):"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "0 uses the grays of the image alone, 100 its estimated depth alone. In between, the depth gives the volumes and the grays keep details such as the eyes and the mouth. Needs the depth models, installed by deploy.ps1 -Depth."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Depth model:"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fast (MiDaS small)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fine (Depth Anything V2 Small)"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Fine details of the photo (%):"
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "Carves the fine contrasts of the photo (eyes, mouth, eyebrows, strands of hair) into the depth, which has none of them. A plain background brings nothing and stays at the back. 0 keeps the smooth depth alone."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "The depth of the scene is estimated from the photo: near areas become light, far areas dark. With boards stacked lightest in front, the nearest part of the subject ends up in front."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.inx
|
||||||
|
msgid "The estimate runs on the graphics card when possible and is kept in a cache: only the first run on an image takes a few seconds."
|
||||||
|
msgstr ""
|
||||||
|
|
||||||
#: gray_iso_layers.inx
|
#: gray_iso_layers.inx
|
||||||
msgid "Style"
|
msgid "Style"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
@ -139,6 +187,8 @@ msgid ""
|
|||||||
"\n"
|
"\n"
|
||||||
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
"Each gray level goes into its own layer, named Board no. 1, Board no. 2... from the back to the front, added above the image: as many new layers as gray levels. The boards are meant to be cut and stacked: board no. 1 covers the whole image and each following board is smaller and lies in front of the previous one, lightest (or darkest) in front. In each layer, the cut outline of the board is drawn in the cut color (red), and the outline of the next board is carried over in the marking color (black), to show where to glue it. With \"Its own gray only\", a board holds the flat area of its gray alone: the boards sit side by side without overlapping and carry no marking. Several selected images share the same layers.\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
"For a relief that follows the real volumes of a portrait rather than its light and shadows, raise the depth map share (Depth tab): at 100, the boards follow the volumes, and the fine details setting carves the eyes and the mouth into them. A depth map made elsewhere (8 or 16 bits) can also be imported and selected like any image, with a share of 0. In both cases, if most boards come out empty or the subject only gets a few of them, raise the level distribution.\n"
|
||||||
|
"\n"
|
||||||
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
"For laser cutting, choose no fill: only the cut and marking strokes remain. For line art only, choose a white single-color fill, or no fill, with a black stroke and no marking. If the outlines are too busy, raise the smoothing or the smallest island; if they look angular, raise the smoothing or the simplification. If a face is no longer recognizable, lower the smoothing and keep \"Preserve edges\" checked."
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
@ -169,3 +219,7 @@ msgstr ""
|
|||||||
#: gray_iso_layers.py
|
#: gray_iso_layers.py
|
||||||
msgid "Marking of board no. {}"
|
msgid "Marking of board no. {}"
|
||||||
msgstr ""
|
msgstr ""
|
||||||
|
|
||||||
|
#: gray_iso_layers.py
|
||||||
|
msgid "No depth map for the image '{}' ({}). Install the depth models with 'deploy.ps1 -Depth'. The grays of the image are used alone."
|
||||||
|
msgstr ""
|
||||||
|
|||||||
@ -11,11 +11,13 @@ import xml.etree.ElementTree as ET
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import depth_estimate
|
||||||
|
import gray_iso_layers_depth
|
||||||
from gray_iso_layers_core import (
|
from gray_iso_layers_core import (
|
||||||
box_blur, contour_lines, contour_rings, edge_preserving_blur, gaussian_blur,
|
add_detail, blend_fields, box_blur, contour_lines, contour_rings, edge_preserving_blur,
|
||||||
gray_to_hex, image_ring, iso_boards, iso_layers, lines_to_d, normalize,
|
gaussian_blur, gray_to_hex, guided_filter, image_ring, iso_boards, iso_layers,
|
||||||
parse_color, ring_area, rings_to_d, scale_lines, scale_rings, simplify_chain,
|
lines_to_d, normalize, parse_color, ring_area, rings_to_d, scale_lines,
|
||||||
simplify_ring, smooth, thresholds)
|
scale_rings, simplify_chain, simplify_ring, smooth, spread_levels, thresholds)
|
||||||
|
|
||||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
@ -171,6 +173,80 @@ def test_iso_layers_preserve_edges_keeps_a_thin_feature():
|
|||||||
assert dark[0][:, 1].max() - dark[0][:, 1].min() <= 7
|
assert dark[0][:, 1].max() - dark[0][:, 1].min() <= 7
|
||||||
|
|
||||||
|
|
||||||
|
def test_spread_levels_gives_equal_areas():
|
||||||
|
# Rampe : les valeurs sont deja reparties, rien ne bouge.
|
||||||
|
assert np.allclose(spread_levels(ramp(200, 20)), ramp(200, 20), atol=0.01)
|
||||||
|
# Champ tasse vers le noir (carte de profondeur : fond etendu, sujet
|
||||||
|
# resserre) : a seuils reguliers, le premier niveau prend presque tout.
|
||||||
|
packed = ramp(200, 20) ** 4
|
||||||
|
assert np.array_equal(spread_levels(packed, 0), packed)
|
||||||
|
assert (packed < 0.25).mean() > 0.7
|
||||||
|
spread = spread_levels(packed)
|
||||||
|
assert spread.min() >= 0.0 and spread.max() == 1.0
|
||||||
|
for cut in (0.25, 0.5, 0.75):
|
||||||
|
assert math.isclose((spread < cut).mean(), cut, abs_tol=0.01)
|
||||||
|
# Dosage : a mi-chemin entre le champ et sa repartition.
|
||||||
|
assert np.allclose(spread_levels(packed, 0.5), (packed + spread) / 2.0)
|
||||||
|
assert not spread_levels(np.full((5, 5), 0.3)).any()
|
||||||
|
|
||||||
|
|
||||||
|
def test_iso_layers_equalize_fills_every_level():
|
||||||
|
packed = ramp(200, 20) ** 4
|
||||||
|
width = [evenodd_area(rings) / 19.0
|
||||||
|
for _level, _gray, rings in iso_layers(packed, levels=4)]
|
||||||
|
assert width[0] > 140 and width[3] < 15
|
||||||
|
width = [evenodd_area(rings) / 19.0
|
||||||
|
for _level, _gray, rings in iso_layers(packed, levels=4, equalize=1.0)]
|
||||||
|
assert np.allclose(width, 199 / 4.0, atol=2.0)
|
||||||
|
|
||||||
|
|
||||||
|
def test_guided_filter_snaps_a_blurred_edge_to_the_guide():
|
||||||
|
step = np.where(np.arange(80) < 40, 0.25, 0.75) * np.ones((60, 1))
|
||||||
|
blurred = gaussian_blur(step, 2.0)
|
||||||
|
|
||||||
|
def width(result):
|
||||||
|
"""Largeur de la transition entre les deux aplats, en echantillons."""
|
||||||
|
row = result.mean(axis=0)
|
||||||
|
return int(np.count_nonzero((row > 0.3) & (row < 0.7)))
|
||||||
|
|
||||||
|
# Le bord se recale si la fenetre est nettement plus large que le flou.
|
||||||
|
assert width(blurred) >= 6
|
||||||
|
assert width(guided_filter(step, blurred, 12)) <= 2
|
||||||
|
# Source uniforme : rien a recaler. Guide uniforme : simple lissage.
|
||||||
|
assert np.allclose(guided_filter(step, np.full(step.shape, 0.4), 8), 0.4)
|
||||||
|
assert np.allclose(guided_filter(np.zeros(step.shape), blurred, 3),
|
||||||
|
box_blur(box_blur(blurred, 3), 3))
|
||||||
|
assert np.array_equal(guided_filter(step, blurred, 0), blurred)
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_detail_carves_fine_contrasts_only():
|
||||||
|
depth = ramp(80, 80)
|
||||||
|
# Fond uni, clair ou sombre : rien a graver, la profondeur ne bouge pas.
|
||||||
|
for level in (0.1, 0.9):
|
||||||
|
assert np.allclose(add_detail(depth, np.full((80, 80), level), 1.0, 4.0), depth)
|
||||||
|
# Trait sombre fin sur fond clair (une bouche) : il se creuse, et loin de
|
||||||
|
# lui la profondeur est intacte.
|
||||||
|
gray = np.full((80, 80), 0.8)
|
||||||
|
gray[38:42, 20:60] = 0.1
|
||||||
|
carved = add_detail(depth, gray, 0.5, 4.0)
|
||||||
|
assert (carved[39:41, 30:50] < depth[39:41, 30:50] - 0.2).all()
|
||||||
|
assert np.allclose(carved[:15], depth[:15], atol=1e-3)
|
||||||
|
assert carved.min() >= 0.0 and carved.max() <= 1.0
|
||||||
|
# Dosage : sans detail, la profondeur seule, etiree sur 0..1.
|
||||||
|
assert np.allclose(add_detail(depth * 3.0, gray, 0, 4.0), depth)
|
||||||
|
lighter = add_detail(depth, gray, 0.25, 4.0)
|
||||||
|
assert (lighter[39:41, 30:50] > carved[39:41, 30:50]).all()
|
||||||
|
|
||||||
|
|
||||||
|
def test_blend_fields():
|
||||||
|
gray, depth = ramp(), ramp().T[:40, :40].repeat(2, axis=1)[:, :60] * 3.0
|
||||||
|
assert np.array_equal(blend_fields(gray, depth, 0), normalize(gray))
|
||||||
|
assert np.array_equal(blend_fields(gray, depth, 1), normalize(depth))
|
||||||
|
assert np.array_equal(blend_fields(gray, depth, 7), normalize(depth))
|
||||||
|
assert np.allclose(blend_fields(gray, depth, 0.25),
|
||||||
|
0.75 * normalize(gray) + 0.25 * normalize(depth))
|
||||||
|
|
||||||
|
|
||||||
def test_contour_of_cone_is_a_circle():
|
def test_contour_of_cone_is_a_circle():
|
||||||
# Seuil 0.5 sur un cone de rayon 40 : cercle de rayon 20.
|
# Seuil 0.5 sur un cone de rayon 40 : cercle de rayon 20.
|
||||||
rings = contour_rings(cone(), 0.5)
|
rings = contour_rings(cone(), 0.5)
|
||||||
@ -501,6 +577,141 @@ def test_rings_to_d_keeps_the_image_border_straight():
|
|||||||
assert "L " not in d and d.count("C ") == len(circle)
|
assert "L " not in d and d.count("C ") == len(circle)
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------------
|
||||||
|
# Carte de profondeur (sans inkex ni onnxruntime : le calcul est simule)
|
||||||
|
# --------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def depth_home(tmp_path, monkeypatch):
|
||||||
|
"""Dossier de donnees factice, avec son Python et ses modeles, et un
|
||||||
|
calcul simule qui note ses appels."""
|
||||||
|
pytest.importorskip("PIL.Image")
|
||||||
|
home = tmp_path / "GrayIsoLayers"
|
||||||
|
(home / "models").mkdir(parents=True)
|
||||||
|
for name in gray_iso_layers_depth.MODELS.values():
|
||||||
|
(home / "models" / name).write_bytes(b"onnx")
|
||||||
|
monkeypatch.setattr(gray_iso_layers_depth, "data_dir", lambda: str(home))
|
||||||
|
monkeypatch.delenv("GRAY_ISO_LAYERS_PYTHON", raising=False)
|
||||||
|
python = gray_iso_layers_depth.find_python()
|
||||||
|
os.makedirs(os.path.dirname(python))
|
||||||
|
with open(python, "wb"):
|
||||||
|
pass
|
||||||
|
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def run(command, env=None, **_options):
|
||||||
|
calls.append((command, env))
|
||||||
|
np.save(command[3], np.full((4, 6), float(len(calls)), dtype=np.float32))
|
||||||
|
return type("Done", (), {"returncode": 0, "stderr": ""})()
|
||||||
|
|
||||||
|
monkeypatch.setattr(gray_iso_layers_depth.subprocess, "run", run)
|
||||||
|
return home, calls
|
||||||
|
|
||||||
|
|
||||||
|
def photo():
|
||||||
|
from PIL import Image
|
||||||
|
return Image.new("RGB", (12, 8), (200, 120, 40))
|
||||||
|
|
||||||
|
|
||||||
|
def test_depth_field_runs_the_helper_once_then_reads_the_cache(depth_home, monkeypatch):
|
||||||
|
home, calls = depth_home
|
||||||
|
monkeypatch.setenv("PYTHONPATH", "inkscape")
|
||||||
|
monkeypatch.setenv("PYTHONHOME", "inkscape")
|
||||||
|
depth = gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
|
||||||
|
assert depth.shape == (4, 6) and (depth == 1).all()
|
||||||
|
command, env = calls[0]
|
||||||
|
assert command[0] == gray_iso_layers_depth.find_python()
|
||||||
|
assert command[1].endswith("depth_estimate.py") and os.path.isfile(command[1])
|
||||||
|
assert command[4].endswith("midas_v21_small_256.onnx")
|
||||||
|
assert command[5:] == ["--model", "midas_small"]
|
||||||
|
# Les variables du Python d'Inkscape ne passent pas a l'autre Python.
|
||||||
|
assert "PYTHONPATH" not in env and "PYTHONHOME" not in env and "PATH" in env
|
||||||
|
# Meme image, meme modele : le cache repond. Autre modele ou autre
|
||||||
|
# image : nouveau calcul.
|
||||||
|
assert (gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small") == 1).all()
|
||||||
|
assert len(calls) == 1
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "abc", "depth_anything_v2_small")
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "def", "midas_small")
|
||||||
|
assert len(calls) == 3
|
||||||
|
assert len(os.listdir(str(home / "cache"))) == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_depth_field_prunes_the_cache(depth_home, monkeypatch):
|
||||||
|
home, _calls = depth_home
|
||||||
|
monkeypatch.setattr(gray_iso_layers_depth, "CACHE_LIMIT", 2)
|
||||||
|
for k, digest in enumerate(("a", "b", "c")):
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), digest, "midas_small")
|
||||||
|
os.utime(str(home / "cache" / (digest + "-midas_small.npy")), (k, k))
|
||||||
|
gray_iso_layers_depth._prune(str(home / "cache"))
|
||||||
|
assert sorted(os.listdir(str(home / "cache"))) == [
|
||||||
|
"b-midas_small.npy", "c-midas_small.npy"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_depth_field_reports_what_is_missing(depth_home, monkeypatch):
|
||||||
|
home, calls = depth_home
|
||||||
|
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="unknown model"):
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "abc", "autre")
|
||||||
|
# Calcul en echec : la derniere ligne de son erreur est rapportee.
|
||||||
|
monkeypatch.setattr(
|
||||||
|
gray_iso_layers_depth.subprocess, "run",
|
||||||
|
lambda *_a, **_k: type("Done", (), {"returncode": 1,
|
||||||
|
"stderr": "Traceback\nboum\n"})())
|
||||||
|
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="^boum$"):
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
|
||||||
|
|
||||||
|
def too_long(*_args, **_options):
|
||||||
|
raise gray_iso_layers_depth.subprocess.TimeoutExpired("python", 120)
|
||||||
|
|
||||||
|
monkeypatch.setattr(gray_iso_layers_depth.subprocess, "run", too_long)
|
||||||
|
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="timed out"):
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
|
||||||
|
# Modele absent, puis environnement Python absent.
|
||||||
|
os.remove(str(home / "models" / "midas_v21_small_256.onnx"))
|
||||||
|
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="midas_v21"):
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "abc", "midas_small")
|
||||||
|
os.remove(gray_iso_layers_depth.find_python())
|
||||||
|
with pytest.raises(gray_iso_layers_depth.DepthUnavailable, match="python"):
|
||||||
|
gray_iso_layers_depth.depth_field(photo(), "abc", "depth_anything_v2_small")
|
||||||
|
assert calls == [] and not (home / "cache").exists()
|
||||||
|
|
||||||
|
|
||||||
|
def test_find_python_honours_the_environment_variable(monkeypatch):
|
||||||
|
monkeypatch.setenv("GRAY_ISO_LAYERS_PYTHON", "ailleurs/python")
|
||||||
|
assert gray_iso_layers_depth.find_python() == "ailleurs/python"
|
||||||
|
monkeypatch.delenv("GRAY_ISO_LAYERS_PYTHON")
|
||||||
|
assert gray_iso_layers_depth.find_python().startswith(
|
||||||
|
gray_iso_layers_depth.data_dir())
|
||||||
|
|
||||||
|
|
||||||
|
def test_depth_estimate_preprocess_and_providers():
|
||||||
|
pytest.importorskip("PIL.Image")
|
||||||
|
assert sorted(depth_estimate.MODELS) == sorted(gray_iso_layers_depth.MODELS)
|
||||||
|
for size in depth_estimate.MODELS.values():
|
||||||
|
tensor = depth_estimate.preprocess(photo(), size)
|
||||||
|
assert tensor.shape == (1, 3, size, size) and tensor.dtype == np.float32
|
||||||
|
# Normalisation ImageNet du rouge : (200 / 255 - 0.485) / 0.229.
|
||||||
|
assert math.isclose(tensor[0, 0, 0, 0], (200 / 255.0 - 0.485) / 0.229, rel_tol=1e-4)
|
||||||
|
gpu, cpu = "DmlExecutionProvider", "CPUExecutionProvider"
|
||||||
|
assert depth_estimate.choose_providers([gpu, cpu]) == [gpu, cpu]
|
||||||
|
assert depth_estimate.choose_providers([gpu, cpu], cpu=True) == [cpu]
|
||||||
|
assert depth_estimate.choose_providers([cpu]) == [cpu]
|
||||||
|
|
||||||
|
|
||||||
|
def test_depth_estimate_real_model(tmp_path):
|
||||||
|
"""Calcul reel, quand onnxruntime et le modele sont installes."""
|
||||||
|
pytest.importorskip("onnxruntime")
|
||||||
|
pytest.importorskip("PIL.Image")
|
||||||
|
weights = os.path.join(gray_iso_layers_depth.data_dir(), "models",
|
||||||
|
gray_iso_layers_depth.MODELS["midas_small"])
|
||||||
|
if not os.path.isfile(weights):
|
||||||
|
pytest.skip("modele absent : deploy.ps1 -Depth")
|
||||||
|
source, output = str(tmp_path / "photo.png"), str(tmp_path / "depth.npy")
|
||||||
|
photo().save(source)
|
||||||
|
assert depth_estimate.main([source, output, weights, "--cpu"]) == 0
|
||||||
|
depth = np.load(output)
|
||||||
|
assert depth.shape == (256, 256) and np.isfinite(depth).all()
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------------
|
# --------------------------------------------------------------------------
|
||||||
# Bout en bout (necessite inkex et Pillow)
|
# Bout en bout (necessite inkex et Pillow)
|
||||||
# --------------------------------------------------------------------------
|
# --------------------------------------------------------------------------
|
||||||
@ -522,10 +733,13 @@ SVG = """<?xml version="1.0" encoding="UTF-8"?>
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def make_document(tmp_path, linked=False, transform=""):
|
def make_document(tmp_path, linked=False, transform="", bits=8):
|
||||||
"""Document de test : une image en cone, incorporee ou liee."""
|
"""Document de test : une image en cone, incorporee ou liee."""
|
||||||
image_module = pytest.importorskip("PIL.Image")
|
image_module = pytest.importorskip("PIL.Image")
|
||||||
picture = image_module.fromarray((cone(64, 28.0) * 255).astype("uint8"), "L")
|
if bits == 16:
|
||||||
|
picture = image_module.fromarray((cone(64, 28.0) * 65535).astype("uint16"))
|
||||||
|
else:
|
||||||
|
picture = image_module.fromarray((cone(64, 28.0) * 255).astype("uint8"))
|
||||||
if linked:
|
if linked:
|
||||||
picture.save(str(tmp_path / "cone.png"))
|
picture.save(str(tmp_path / "cone.png"))
|
||||||
href = "cone.png"
|
href = "cone.png"
|
||||||
@ -704,6 +918,75 @@ def test_end_to_end_plain_blur(tmp_path):
|
|||||||
assert paths(kept) != paths(plain)
|
assert paths(kept) != paths(plain)
|
||||||
|
|
||||||
|
|
||||||
|
def test_end_to_end_16_bit_image(tmp_path):
|
||||||
|
# Carte de profondeur en 16 bits : memes planches que la meme image en
|
||||||
|
# 8 bits, la ou une conversion directe l'ecreterait en image blanche.
|
||||||
|
args = ("--id=img1", "--levels=4", "--blur=0", "--shapes=band")
|
||||||
|
deep = run_extension(tmp_path, *args, document=make_document(tmp_path, bits=16))
|
||||||
|
plain = run_extension(tmp_path, *args)
|
||||||
|
assert [path.count("M ") for path in paths(deep)] == [2, 2, 2, 1]
|
||||||
|
|
||||||
|
def extent(path):
|
||||||
|
"""Etendue en x des points du chemin."""
|
||||||
|
xs = [float(x) for x, _y in re.findall(r"(-?[\d.]+),(-?[\d.]+)", path)]
|
||||||
|
return min(xs), max(xs)
|
||||||
|
|
||||||
|
for a, b in zip(paths(deep), paths(plain)):
|
||||||
|
assert np.allclose(extent(a), extent(b), atol=0.5)
|
||||||
|
|
||||||
|
|
||||||
|
def test_end_to_end_equalize(tmp_path):
|
||||||
|
# Cone : les disques a seuils reguliers ont des rayons reguliers ; a
|
||||||
|
# aires egales, le niveau le plus clair s'etend.
|
||||||
|
def top_width(*extra):
|
||||||
|
svg = run_extension(tmp_path, "--id=img1", "--levels=4", "--blur=0",
|
||||||
|
"--shapes=band", *extra)
|
||||||
|
xs = [float(x) for x, _y in re.findall(r"(-?[\d.]+),(-?[\d.]+)", paths(svg)[3])]
|
||||||
|
return max(xs) - min(xs)
|
||||||
|
|
||||||
|
assert top_width("--equalize=100") > 1.5 * top_width()
|
||||||
|
|
||||||
|
|
||||||
|
def test_end_to_end_depth(tmp_path, monkeypatch, capsys):
|
||||||
|
pytest.importorskip("inkex")
|
||||||
|
import gray_iso_layers
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def fake(picture, digest, model):
|
||||||
|
calls.append((picture.mode, picture.size, len(digest), model))
|
||||||
|
return ramp(32, 32) # profondeur : proche a droite
|
||||||
|
|
||||||
|
monkeypatch.setattr(gray_iso_layers, "depth_field", fake)
|
||||||
|
args = ("--id=img1", "--levels=4", "--blur=0", "--shapes=band")
|
||||||
|
cone_only = run_extension(tmp_path, *args)
|
||||||
|
assert calls == [] # part de profondeur nulle : aucun calcul
|
||||||
|
assert all("L " not in path for path in paths(cone_only)[1:])
|
||||||
|
|
||||||
|
svg = run_extension(tmp_path, *args, "--depth_mix=100", "--depth_detail=0",
|
||||||
|
"--depth_model=depth_anything_v2_small")
|
||||||
|
assert calls == [("RGB", (64, 64), 40, "depth_anything_v2_small")]
|
||||||
|
# Profondeur seule : quatre bandes verticales, bordees par l'image.
|
||||||
|
assert [path.count("M ") for path in paths(svg)] == [1, 1, 1, 1]
|
||||||
|
assert all("L " in path for path in paths(svg))
|
||||||
|
# Melange : ni le cone seul, ni la rampe seule.
|
||||||
|
mixed = run_extension(tmp_path, *args, "--depth_mix=50", "--depth_detail=0")
|
||||||
|
assert paths(mixed) != paths(svg) and paths(mixed) != paths(cone_only)
|
||||||
|
# Details fins : les contrastes du cone se gravent dans la rampe.
|
||||||
|
carved = run_extension(tmp_path, *args, "--depth_mix=100")
|
||||||
|
assert paths(carved) != paths(svg) and paths(carved) != paths(cone_only)
|
||||||
|
assert capsys.readouterr().err == ""
|
||||||
|
|
||||||
|
def missing(*_args):
|
||||||
|
raise gray_iso_layers.DepthUnavailable("missing file: venv")
|
||||||
|
|
||||||
|
# Profondeur indisponible : c'est dit, et les gris servent seuls.
|
||||||
|
monkeypatch.setattr(gray_iso_layers, "depth_field", missing)
|
||||||
|
svg = run_extension(tmp_path, *args, "--depth_mix=100")
|
||||||
|
assert paths(svg) == paths(cone_only)
|
||||||
|
error = capsys.readouterr().err
|
||||||
|
assert "missing file: venv" in error and "deploy.ps1 -Depth" in error
|
||||||
|
|
||||||
|
|
||||||
def test_end_to_end_without_image(tmp_path, capsys):
|
def test_end_to_end_without_image(tmp_path, capsys):
|
||||||
svg = run_extension(tmp_path, "--id=rect1")
|
svg = run_extension(tmp_path, "--id=rect1")
|
||||||
assert "Board no. 1" not in svg
|
assert "Board no. 1" not in svg
|
||||||
@ -772,7 +1055,7 @@ def test_inx_matches_arguments():
|
|||||||
options = GrayIsoLayers().arg_parser.parse_args([])
|
options = GrayIsoLayers().arg_parser.parse_args([])
|
||||||
for name, text in params.items():
|
for name, text in params.items():
|
||||||
default = getattr(options, name)
|
default = getattr(options, name)
|
||||||
if name in ("tab", "shapes", "fill_mode", "unit"):
|
if name in ("tab", "shapes", "depth_model", "fill_mode", "unit"):
|
||||||
continue # listes : la valeur par defaut est la premiere option
|
continue # listes : la valeur par defaut est la premiere option
|
||||||
if isinstance(default, bool):
|
if isinstance(default, bool):
|
||||||
assert text == str(default).lower(), name
|
assert text == str(default).lower(), name
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user