Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving layers as svg fails with text labels and color images #50

Open
Micro-Sandworms opened this issue Jan 9, 2025 · 4 comments
Open
Labels
bug Something isn't working

Comments

@Micro-Sandworms
Copy link
Contributor

Micro-Sandworms commented Jan 9, 2025

🐛 Bug Report

When saving layers as svg, the program crashes if one of the layers contains a color image. In addition, text labels are not saved.
The former can be solved by changing the code at the layer_to_xml.py, function
def extrema_image(image, meta):
"""Compute the extrema of an image layer, accounting for transforms."""
coords = np.array([[0, 0], list(image.shape)]) # This does not work for color images
#coords = np.array([[0, 0], list(image.shape[:2])]) # This works
return extrema_coords(coords, meta)

The later, I don't see how to solve, and I appreciate your help in advance.

💡 Steps to Reproduce

import napari
import numpy as np

# Create a viewer
viewer = napari.Viewer()

# Create a color noise image
noise_image = np.random.random((100, 51, 3))
# noise_image = np.random.random((100, 51))  # this one works to save the image as is not a color image

# Add the noise image layer first
viewer.add_image(noise_image, name='noise_image')

# Add a layer with a single point
points = np.array([[100, 200]])
viewer.add_points(points, size=10, face_color='red', name='points_layer')

# Add a text layer
text = ['Sample Text']
viewer.add_points(points, size=0, text=text, name='text_layer')

# Set the zoom level to 0.3
viewer.camera.zoom = 0.7

# Start the Napari event loop
napari.run()

💡 Expected Behavior

No response

🌎 Environment

napari: 0.5.5
Platform: Windows-10-10.0.19045-SP0
Python: 3.11.11 | packaged by conda-forge | (main, Dec 5 2024, 14:06:23) [MSC v.1942 64 bit (AMD64)]
Qt: 5.15.2
PyQt5: 5.15.11
NumPy: 1.24.0
SciPy: 1.15.0
Dask: 2024.12.1
VisPy: 0.14.3
magicgui: 0.10.0
superqt: 0.7.1
in-n-out: 0.2.1
app-model: 0.3.1
psygnal: 0.11.1
npe2: 0.7.7
pydantic: 2.10.4

OpenGL:

  • GL version: 4.6.0 - Build 30.0.101.1692
  • MAX_TEXTURE_SIZE: 16384
  • GL_MAX_3D_TEXTURE_SIZE: 2048

Screens:

  • screen 1: resolution 1920x1200, scale 1.0
  • screen 2: resolution 1680x1050, scale 1.0
  • screen 3: resolution 1707x960, scale 1.0

Optional:

  • numba: 0.60.0
  • triangle: 20250106
  • napari-plugin-manager: 0.1.4

Settings path:

  • C:\Users\alvarez\AppData\Local\napari\Visualization_Gene_Expression_d453efcbacc35064a38aa632634bb2eff004ecff\settings.yaml
    Plugins:
  • napari: 0.5.5 (81 contributions)
  • napari-console: 0.1.3 (0 contributions)
  • napari-svg: 0.2.0 (2 contributions)

💡 Additional Context

No response

@Micro-Sandworms Micro-Sandworms added the bug Something isn't working label Jan 9, 2025
@jni jni transferred this issue from napari/napari Jan 9, 2025
@jni
Copy link
Member

jni commented Jan 9, 2025

Thanks for the issue @Micro-Sandworms! I've transferred it to napari-svg which is its own repo.

Are you able to contribute a fix for the image issue? That would be much appreciated!

I don't know all that much about svg but I imagine it's not too hard to add text, so it would be a matter of (a) looking for any text annotations on the layer, and (b) saving those in the appropriate part of the xml.

@Micro-Sandworms
Copy link
Contributor Author

Micro-Sandworms commented Jan 9, 2025

Thank you, Juan. As I mentioned, replacing the function 'extrema_image' in 'layer_to_xml.py' with

def extrema_image(image, meta):
    """Compute the extrema of an image layer, accounting for transforms."""
    coords = np.array([[0, 0], list(image.shape[:2])])
    return extrema_coords(coords, meta)

It worked for me, but I am not in a position to foresee possible negative consequences down the line. Still, I made a commitment with this suggestion.
Regarding the text labels, I have no clue how to solve the issue. I'm sorry.

@jni
Copy link
Member

jni commented Jan 9, 2025

@Micro-Sandworms oh thanks, I more meant, would you like to make a pull request to the repo? Here's a good guide for how to do that, if you are not familiar with the process: https://www.dataschool.io/how-to-contribute-on-github/

@Micro-Sandworms
Copy link
Contributor Author

I made two pull requests. One is for a possible solution regarding the color images, and the other is for the text labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants