Jump to content

Export images to a specific folder via Ironpython


Yacine El Gharbi

Recommended Posts

Hi everyone! I'm using the following code to export image from spotfire to a folder, which will be selected. the code shows me no errors but the image is not saved in the folder!! It may be a path problem. Your help will be very appreciated.

That's the code:

 

import System

import clr

clr.AddReference("System.Windows.Forms")

from System import *

from System.IO import *

from System.Drawing import Bitmap, Rectangle

from System.Drawing import Graphics, Image

from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread

from System.IO import Path

from System.Windows.Forms import FolderBrowserDialog,MessageBox

 

#Choose Folder to save visualization

filename=""

folder=FolderBrowserDialog()

result=folder.ShowDialog()

if result==result.OK:

filename=folder.SelectedPath

else:

MessageBox.Show("Please select a folder and try again")

 

app = Application.GetService(ApplicationThread)

image = Bitmap(800, 600)

def f(visual=visual, document=Document, app=app, image=image, Graphics=Graphics, Rectangle=Rectangle):

try:

vis = visual

gfx = Graphics.FromImage(image)

rect = Rectangle(0,0,image.Width,image.Height)

vis.Render(gfx, Rectangle(0,0,image.Width,image.Height))

imageFilename = filename + "image.png"

except:

return

# DO SOMETHING WITH THE IMAGE HERE

image.Save(imageFilename)

app.InvokeAsynchronously(f)

Link to comment
Share on other sites

Hi,

I do run the script from the script editor, then you will get the errors created by the script.

 

I pasted your script in adefault report of Analyst, and then executed it from the Script editor, and just corrected the few errors I got.

Below is the script I got working, saving to my desktop, and adding the script parameter 'visual' pointing to one of the visualizations.

 

BR, Per K

 

import System

import clr

clr.AddReference("System.Windows.Forms")

from System import *

from System.IO import *

from System.Drawing import Bitmap, Rectangle

from System.Drawing import Graphics, Image

from Spotfire.Dxp.Framework.ApplicationModel import ApplicationThread

from System.IO import Path

from System.Windows.Forms import FolderBrowserDialog,MessageBox

#Choose Folder to save visualization

filename=""

folder=FolderBrowserDialog()

result=folder.ShowDialog()

if result==result.OK:

filename=folder.SelectedPath

else:

MessageBox.Show("Please select a folder and try again")

app = Application.GetService(ApplicationThread)

image = Bitmap(800, 600)

def f(visual, document, app, image, Graphics, Rectangle):

try:

vis = visual

gfx = Graphics.FromImage(image)

rect = Rectangle(0,0,image.Width,image.Height)

vis.Render(gfx, Rectangle(0,0,image.Width,image.Height))

imageFilename = filename + "image.png"

except:

return

# DO SOMETHING WITH THE IMAGE HERE

image.Save(imageFilename)

app.InvokeAsynchronously(f(visual, Document, app, image, Graphics, Rectangle))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...