cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
RSantana
Level II

Adding Images to A Data Table From a Directory

Friends, I am running JMP12 and I would like to add a series of PNG images to my data table.  I would like to do this so that I can use them as labels on a scatterplot of statistical mean-matching flags and invoke the PNG (an image of a control limit chart) by hovering the mouse over the value of each flag.  I found an SAS video by Gail Massari importing state bird pictures from a wikipedia web URL; different than him, my images are in a folder, on a local hard drive. 

 

I have tried the instruction:

{"",{ newimage(open( "drive:\file path\filename.png",png))}" as a entry in each respective row but all I get in return is an "Empty()" output.

 

I am new to JSL scripting and I have run out of ideas.

2 ACCEPTED SOLUTIONS

Accepted Solutions
gzmorgan0
Super User (Alumni)

Re: Adding Images to A Data Table From a Directory

RSantana, Gail's video was demonstarting how to get pics from URLs, not embedded pictures.

 

Try the script that startw with Names default to Here(1). It was created in JMP 12.

 

Note, if all of your pictures are of the same type then you can simplify lines 11 and 12. Suppose all your pictures are png, then you do not need line 11: _typ = word() and line 12 you can remove the Eval( EvalExpr() ) functions. I needed it for this example because the sample images have different types and in JMP 12, the open would not evaluate the variable _ext; it required that the expression be substituted then evaluated.  

This would be the simplified line 12    tp = Open("$Sample_images/"||_fid[i], "png" );

 

Copy and paste this into a JMP script window, run it then hover over the points. One final comment this is embedding each picture's "code" into your table column.

 

 

Names Default to Here(1);

_fid = Files in Directory("$sample_images");
nr = nitems(_fid);
//Create a table
dt = New Table("demo",add rows(nr),
New Column("X", <<set Values(1::nr)), New Column("Y", <<set Values(1::nr)),
  New Column("pics", Expression)  );
 
for(i=1, i<=nr, i++,
  _typ = word(2,_fid[i],".");
  Eval(Eval Expr(tp = Open("$Sample_images/"||_fid[i], expr(_typ) )));	
  dt:pics[i] = new image(tp)	
);
dt:pics << label(1);

biv = dt<< Bivariate( Y(:Y), X(:X)); 

View solution in original post

gzmorgan0
Super User (Alumni)

Re: Adding Images to A Data Table From a Directory

Is the full path in a separate column, or do you have a column for path and a column for file name?

Suppose it is a column named :Path. Since they are all png files then 

 tp = Open( :Path[i], "png" );

 

If you have two columns :Dir and :Name use something like this, if there is no trailing slash for the dir name

 tp = Open( :Dir[i] || "\" || :Name[i], "png" );

Older versions of JMP required an eval()

 

 tp = Open( eval( :Dir[i] || "\" || :Name[i] )  , "png" );

 

Try it out before running your script , run just one line replacing i with a number to make sure you have the file pathname correct.

 

View solution in original post

8 REPLIES 8
gzmorgan0
Super User (Alumni)

Re: Adding Images to A Data Table From a Directory

RSantana, Gail's video was demonstarting how to get pics from URLs, not embedded pictures.

 

Try the script that startw with Names default to Here(1). It was created in JMP 12.

 

Note, if all of your pictures are of the same type then you can simplify lines 11 and 12. Suppose all your pictures are png, then you do not need line 11: _typ = word() and line 12 you can remove the Eval( EvalExpr() ) functions. I needed it for this example because the sample images have different types and in JMP 12, the open would not evaluate the variable _ext; it required that the expression be substituted then evaluated.  

This would be the simplified line 12    tp = Open("$Sample_images/"||_fid[i], "png" );

 

Copy and paste this into a JMP script window, run it then hover over the points. One final comment this is embedding each picture's "code" into your table column.

 

 

Names Default to Here(1);

_fid = Files in Directory("$sample_images");
nr = nitems(_fid);
//Create a table
dt = New Table("demo",add rows(nr),
New Column("X", <<set Values(1::nr)), New Column("Y", <<set Values(1::nr)),
  New Column("pics", Expression)  );
 
for(i=1, i<=nr, i++,
  _typ = word(2,_fid[i],".");
  Eval(Eval Expr(tp = Open("$Sample_images/"||_fid[i], expr(_typ) )));	
  dt:pics[i] = new image(tp)	
);
dt:pics << label(1);

biv = dt<< Bivariate( Y(:Y), X(:X)); 
RSantana
Level II

Re: Adding Images to A Data Table From a Directory

This is exactly what I need and the solution is simply elegant; I am very appreciative of your time and of your help.

 

Now the challenge...

I have my PNG files in 46 individual folders and the PNG files share the same name; for example I have "graph30.png" repeated 20-30 times but each file is saved in a different folder because it is an image of a different control limit chart.  I already have the path\filename for each as a discrete column in my data table. 

 

What would be the best way look for each graphXX.png based on the specific path\filename already available in a contiguous column?

gzmorgan0
Super User (Alumni)

Re: Adding Images to A Data Table From a Directory

Is the full path in a separate column, or do you have a column for path and a column for file name?

Suppose it is a column named :Path. Since they are all png files then 

 tp = Open( :Path[i], "png" );

 

If you have two columns :Dir and :Name use something like this, if there is no trailing slash for the dir name

 tp = Open( :Dir[i] || "\" || :Name[i], "png" );

Older versions of JMP required an eval()

 

 tp = Open( eval( :Dir[i] || "\" || :Name[i] )  , "png" );

 

Try it out before running your script , run just one line replacing i with a number to make sure you have the file pathname correct.

 

RSantana
Level II

Re: Adding Images to A Data Table From a Directory

gzmorgan, this was awesome and worked like a charm. Big thank you!!

A final question,
Are journals in JMP.12 unable to display labels as you select or hover over data points in graphs?
gzmorgan0
Super User (Alumni)

Re: Adding Images to A Data Table From a Directory

Journals are snapshots of your display.  One option is to save as a .JRP with embedded tables. Then when another opens it the underlying table and report is active.

 

In JMP 13 and 14 Save as Interactive HTM saves the display and the labels are embedded. 

RSantana
Level II

Re: Adding Images to A Data Table From a Directory

Again, thank you for your time.

Interacting in this community has been a delightful learning experience.


RSantana
Level II

Re: Adding Images to A Data Table From a Directory

Building up on this topic of using pictures (i.e. *.png) as labels on charts while using the following loop:

 

For( i = 1, i <= nr, i++,
tp = Open( :PNG_URL[i], "png" );
Latest_Report:PNGs[i] = New Image( tp )
);

Latest_Report:PNGs << label (1);

 

I learned that every time the OPEN command fails to find a one of the PNG files, the script stops and from that point forward, any other rows showing the folder addresses are ignored, with JMP delivering as a result "(Empty)" on each remaining cell of the table.  This means I end up with valuable information not being displayed because one row triggers a failure in the loop.

 

What is the best way to override this?

Running the loop again only adds another column (in this case :PNG_URL 2) and delivers (as expected) the same result.

gzmorgan0
Super User (Alumni)

Re: Adding Images to A Data Table From a Directory

Before the open, use the function If (File Exits(), Open(); etc.; );

For( i = 1, i <= nr, i++,
 If ( File Exists( :PNG_URL[i]|| ".png" ),
   // do this only of the file is found
	tp = Open( :PNG_URL[i], "png" );
	Latest_Report:PNGs[i] = New Image( tp );
 ) // end if	 
);

Latest_Report:PNGs << label( 1 );