1

I'm trying to make a script that does something similar to what is presented in this post:

http://spotfire.tibco.com/tips/2014/02/25/dynamically-displaying-images-in-a-text-area/

The only difference is that I'm trying to use images that I have saved on a network drive or on my local machine. I've done a ton of research and tried different approaches but nothing seems to work. I think I just need a way to read an image from file without all of the "webRequest" related activities. The part about the image processing and the use of the blob seem fine.

Any help is much appreciated. Thanks!

cholmes
  • 11
  • 1
  • 2

1 Answers1

3

I found your question really interesting and ran around the internet all over looking for a way to do this and I think I have come up with a good method that blends the link you provided and local/network file usage.

In the code below I used a picture of my cat and a picture of a bell to go back and forth between. The extra escaping backslashes are necessary for python to read your file path. I have included 2 paths in my example of a local and a network path and both work. Also note that this code does not care if my Bell image was a .gif rather than a .png.

With more logical coding and document properties you could more specifically tell it what file you want to open. (if property = "Dog" then grab the dog path, elif property = "Kid" then grab kid path, etc.)

The key element that makes this work for you is the Image.FromFile(stringPath) function. The rest follows the source material and saves it as a blob.

from System.IO import MemoryStream, SeekOrigin
from System.Drawing import Image
from System.Drawing.Imaging import ImageFormat
from Spotfire.Dxp.Data import BinaryLargeObject

if Document.Properties["CurImg"] != "Cat":
    imgSrc = "C:\\Users\\USERNAME\\Pictures\\2013-07-14 19.20.18.png"
    Document.Properties["CurImg"] = "Cat"
else:
    imgSrc = "\\\\COMPANY\\DEPARTMENT\\USERNAME\\Private\\Bell.gif"
    Document.Properties["CurImg"] = "Bell"

img = Image.FromFile(imgSrc)
stream = MemoryStream()
img.Save(stream, ImageFormat.Png)
stream.Seek(0, SeekOrigin.Begin)
blob = BinaryLargeObject.Create(stream)
Document.Properties["PngImage"] = blob

Sources:
http://www.grasshopper3d.com/forum/topics/read-images-in-python -- Reading Image Files in Python
http://spotfire.tibco.com/tips/2014/02/25/dynamically-displaying-images-in-a-text-area/ -- Source material for taking image into blob

clesiemo3
  • 1,099
  • 6
  • 17
  • Wonderful answer this worked for me, can't thank you enough. For those confused where to include this script, create the document property "CurImg" manually with "string" type and "PngImage" with "binary" type. CurImg is where you have the strings that will select which image you want and that's where you'll include this script. In turn this will update PngImage where you can insert that as a Property Control > Label into a text area to display the image – Alex Braksator Oct 04 '22 at 21:45