Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
opening the image leads to funny action - cannot see any thing but a black box
#1
opening the image leads to funny action - cannot see any thing but a black box

what is aimed: i want to work on a output of a scatterplot.  This should have a transparent background. but duhhh - i am not able to open it i n Gimp.

what goes wrong here ? 

look forward to hear from you 

   
Reply
#2
hm strange, some programs show transparency as black but you should display a checkered background in gimp to show transparency. What mode is your picture? Greyscale, indexed, rgb, or other?
To check this, once you have your picture opened in gimp, check 'image' in the top menu - mode and if it is any other put it to rgb. Oh, and make sure you have an alpha channel too. right click on your picture in the layers dialog and add alpha channel.
Found this here
https://www.gimp-forum.net/Thread-gimp-r...d-to-black

Smile
Reply
#3
Can you open the graphics file in another program, such as XnView? If you can, load and save the file from that program, it could be possible that the file is a little corrupt.
Reply
#4
good day you both - very glad to hear from you.







 Well i will try to reproduce the steps so that we can have the file again.

see here : 

       

btw: if you want to run the code - you can do it here: https://colab.research.google.com/drive/...sp=sharing

then you can see the outcome - and all that stuff  Smile

love to hear from you Smile
Reply
#5
(8 hours ago)saint_m Wrote: good day you both - very glad to hear from you.







 Well i will try to reproduce the steps so that we can have the file again.

see here : 



btw: if you want to run the code - you can do it here: https://colab.research.google.com/drive/...sp=sharing

then you can see the outcome - and all that stuff  Smile

love to hear from you Smile
access denied
Reply
#6
hello dear denzjos

many  many thanks for the quick reply - very glad to hear from you


i updated the google-colab-settings - now you have access and can run the plotly-code

https://colab.research.google.com/drive/...sp=sharing

and it should output it with a transparent background

greetings


btw.
the code is taken form here: 
https://plotly.com/python/line-and-scatter/



btw: does this help 


Code:
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')
fig.show()

siome changes were made: 

Code:
# Plotly-Bibliothek installieren (falls nicht vorhanden)
!pip install plotly

import plotly.express as px
import plotly.graph_objects as go # Für erweiterte Layout-Optionen

# Daten laden
df = px.data.iris()

# Figur erstellen
fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')

# --- Hier wird der Hintergrund transparent gemacht ---
fig.update_layout(
    template='plotly_white',   # Helle Basis, aber wir überschreiben alles
    paper_bgcolor='rgba(0,0,0,0)',  # Transparenter Papier-Hintergrund
    plot_bgcolor='rgba(0,0,0,0)',   # Transparenter Plot-Bereich
    margin=dict(l=40, r=40, t=40, b=40) # Etwas Rand für die Achsenbeschriftung
)

# Achsenlinien und Gitterlinien optional anpassen (für bessere Sichtbarkeit auf transparentem Grund)
fig.update_xaxes(
    showgrid=True,          # Gitterlinien anzeigen (optional)
    gridcolor='lightgrey',  # Farbe der Gitterlinien
    zeroline=True,
    zerolinecolor='lightgrey'
)
fig.update_yaxes(
    showgrid=True,
    gridcolor='lightgrey',
    zeroline=True,
    zerolinecolor='lightgrey'
)

# Figur an
zeigen

fig.show()


and some additonal Changes were made: 


paper_bgcolor='rgba(0,0,0,0)': Makes the entire paper background (the area surrounding the plot) transparent. rgba(0,0,0,0) stands for red, green, and blue with an alpha value (transparency) of 0.

plot_bgcolor='rgba(0,0,0,0)': Makes the background of the coordinate system itself transparent.

template='plotly_white': This sets a light base style, though it is overridden by the transparency settings. It ensures that axis labels and legends remain clearly legible.

Adjusting grid lines: To ensure data points remain visible against a transparent background, the grid lines are kept a subtle gray ('lightgrey'). You can set showgrid=False if you wish to remove them entirely.
you should be able to run it here:  https://colab.research.google.com/drive/...sp=sharing




some findings:  Even if the  screenshot turns black when clicked. Didn't know exporting a png could be so complicated and messy. 

Guess that for Matplotlib, export directly with alpha from the figure, not by screenshotting or copying the preview.

what about the usage of this pattern:

Code:
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(10, 5), dpi=150)

# Plot your data
sc = ax.scatter(x, y, c=petal_length, cmap="plasma", s=20)

# Make BOTH figure canvas and axes rectangle transparent
fig.patch.set_alpha(0)
ax.patch.set_alpha(0)

# Optional: if you have a colorbar
cbar = fig.colorbar(sc, ax=ax)
cbar.ax.patch.set_alpha(0)

# Save directly to PNG with alpha
fig.savefig(
    "plot_transparent.png",
    format="png",
    dpi=300,
    transparent=True,
    facecolor="none",
    edgecolor="none",
    bbox_inches="tight",
    pad_inches=0.05
)
Important details
Do not do this
plt.savefig("plot.png")
if your figure or axes already have a dark/white facecolor.

Also avoid:

fig.savefig("plot.png", facecolor=fig.get_facecolor())
That can bake the current background into the PNG.

Do this instead
fig.savefig(
    "plot.png",
    transparent=True,
    facecolor="none",
    edgecolor="none"
)
If you use a dark plotting style
For example:

plt.style.use("dark_background")
it may set the axis background to dark. Clear it before saving:

fig.patch.set_facecolor("none")
ax.set_facecolor("none")

fig.savefig(
    "plot.png",
    transparent=True,
    facecolor="none",
    edgecolor="none",
    dpi=300,
    bbox_inches="tight"
)
If you want transparent background but visible grid/text
You still need to set the plot element colors manually:

ax.tick_params(colors="white")
ax.xaxis.label.set_color("white")
ax.yaxis.label.set_color("white")
ax.title.set_color("white")

for spine in ax.spines.values():
    spine.set_color("white")

ax.grid(color="white", alpha=0.45)
That makes the background transparent, while the grid, axes, labels, and points remain visible.

Verification
After export:

from PIL import Image
import numpy as np

img = Image.open("plot_transparent.png").convert("RGBA")
alpha = np.array(img)[:, :, 3]

print(alpha.min(), alpha.max())
Correct transparent PNG:

0 255
Flattened/non-transparent PNG:

255 255
Main rule: save from Matplotlib with transparent=True, facecolor="none", and transparent fig/ax patches. Do not export through viewer/editor screensh

look forward to hear from you  Wink
Reply
#7
Some help here : https://plotly.com/python/static-image-export/
Reply
#8
(5 hours ago)denzjos Wrote: Some help here : https://plotly.com/python/static-image-export/


hello dear denzjos - many many thanks  -this is incredible - and so helpful. 

you are a true GIMP-Hero  Smile

i am very glad. 

have a great day. 
#greetings  Smile
Reply


Forum Jump: