cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
lala
Level VIII

How to make the background of images made by JSL transparent using python?

I didn't succeed like this.

Thanks!

dt = Open("$SAMPLE_DATA/Big Class.jmp");

p1 = dt << Graph Builder(
    Show Control Panel(0),
    Variables(X(:height), Y(:weight), Overlay(:sex)),
    Elements(Points(X, Y, Legend(1)), Smoother(X, Y, Legend(2)))
);


img = p1 << Get Picture;


img_binary = img << Get Blob("PNG");


base64_img = Blob To Base64(img_binary);
Python Send("base64_img", base64_img);


Python Submit("
import base64
from PIL import Image
import numpy as np
import io

def remove_white_background(img_data_bytes):
    img = Image.open(io.BytesIO(img_data_bytes))
    if img.mode != 'RGBA':
        img = img.convert('RGBA')
    data = np.array(img)
    white_pixels = (data[:, :, 0] >= 240) & (data[:, :, 1] >= 240) & (data[:, :, 2] >= 240)
    data[white_pixels, 3] = 0
    new_img = Image.fromarray(data)
    output = io.BytesIO()
    new_img.save(output, format='PNG')
    return output.getvalue()

img_data_bytes = base64.b64decode(base64_img)
transparent_img_binary = remove_white_background(img_data_bytes)
");


transparent_img_binary = Python Get("transparent_img_binary");


transparent_img = New Image(transparent_img_binary);

2025-04-29_13-43-39.png

9 REPLIES 9
lala
Level VIII

回复: How to make the background of images made by JSL transparent using python?

Python Send(img);
wrong.

 

Note that the pictures should not be saved during the processing.

Thanks!

回复: How to make the background of images made by JSL transparent using python?

JMP 18 Python Send() does not know how to handle a JMP Image. Data table types had priority in 18.  JMP 19 has minimal support for Image so that Images can be placed into the data table from Python.  At present there are no accessors to the underlying image data from Python in JMP 19 EA.

Re: How to make the background of images made by JSL transparent using python?

The key issue you are having is getting the JMP image data into a format you can work with in Python.  Taking your above code, This will give you an RGBA image in Python that you can then work with,

 

dt = Open( "$SAMPLE_DATA/Big Class.jmp" );

p1 = dt << Graph Builder(
	Show Control Panel( 0 ),
	Variables( X( :height ), Y( :weight ), Overlay( :sex ) ),
	Elements( Points( X, Y, Legend( 1 ) ), Smoother( X, Y, Legend( 2 ) ) )
);

img = p1 << Get Picture;

m = img << Get Pixels( "rgba" );  // See scripting index for docs on get pixel
python send(m);                   // Sends list of vectors { [r],[g],[b],[a] } 

python submit("\[
from PIL import Image
import numpy as np

print(m.__class__)
# JMP image values are 0..1 so covert and make numpy array
rgb_uint8 = (np.dstack((m[0],m[1],m[2],m[3])) * 255.999) .astype(np.uint8)

pyimg = Image.fromarray(rgb_uint8,"RGBA"). #you now have a PIL.Image in Python.
pyimg.save(jmp.HOME+'pyimg.png'). # here I saved just to prove image made it through

]\");

This is where limitations of AI come in, to do this efficiently you need to understand what capabilities that JSL provides and how to get that data across to Python in a manner that can be consumed buy the Python APIs.  The JMP scripting index is your friend, I use it as a go to reference.

lala
Level VIII

Re: How to make the background of images made by JSL transparent using python?

It seems that python is more convenient for processing and saving images.

 

Thanks Experts!

2025-04-30_10-27-27.png

lala
Level VIII

Re: How to make the background of images made by JSL transparent using python?

It seems that python is more convenient for processing and saving images.I want to make such a picture.

It's a pity that JSL doesn't have the function of directly making the graphic background transparent.

2025-04-29_08-32-52.png

Re: How to make the background of images made by JSL transparent using python?

See scripting index: Image

img << Transparency( 0. );      // sets entire image's transparency level 0..1   0 => transparent, 1 => opaque

 

If you want just the background transparent, << get pixels( "rgba" ) then edit the arrays to find the white pixels and setting their transparency to 0, then use << set pixels( "rgba", { r, g, b, a} ) set the updates back to the image.

lala
Level VIII

Re: How to make the background of images made by JSL transparent using python?

Thanks Experts!

 

I tried to write like this and also asked the AI, but still failed.

I knew before that this could make the picture lighter, but it's not transparent.

Craige specifically put forward a suggestion for achieving background transparency (in JMP 17, but JMP18 did not improve).

 

dt = Open("$SAMPLE_DATA/Big Class.jmp");
p1 = dt << Graph Builder(
	Show Control Panel(0), Show Legend(0), Show Title(0), Show Footer(0), Show X Axis(0), Show Y Axis(0), Show X Axis Title(0), Show Y Axis Title(0),
	Variables(X(:height), Y(:weight), Overlay(:sex)),
	Elements(Points(X, Y, Legend(1)), Smoother(X, Y, Legend(2)))
);
img = p1 << Get Picture;
m = img << Get Pixels("rgba");
size_info = img << Get Size;
img_width = If(N Items(size_info) >= 1, size_info[1], 0);
img_height = If(N Items(size_info) >= 2, size_info[2], 0);
n = m;
white_pixels = Loc(m[0, 0, 1] == 255 & m[0, 0, 2] == 255 & m[0, 0, 3] == 255);
for(k = 1, k <= N Rows(white_pixels), k++,
	i = white_pixels[k, 1];
	j = white_pixels[k, 2];
	n[i, j, 4] = 0
);
img << Set Pixels("rgba", n);
win2 = NewWindow("Transformed - White Transparent", img);
lala
Level VIII

Re: How to make the background of images made by JSL transparent using python?

2025-04-18_18-22-01.png

lala
Level VIII

Re: How to make the background of images made by JSL transparent using python?

Recommended Articles