1

我正在尝试制作一个与本文中介绍的内容类似的脚本:

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

唯一的区别是我尝试使用保存在网络驱动器或本地计算机上的图像。我已经进行了大量研究并尝试了不同的方法,但似乎没有任何效果。我想我只需要一种从文件中读取图像的方法,而不需要所有与“webRequest”相关的活动。关于图像处理和 blob 使用的部分似乎很好。

任何帮助深表感谢。谢谢!

4

1 回答 1

2

我发现你的问题真的很有趣,并在互联网上到处寻找一种方法来做到这一点,我想我想出了一个很好的方法,将你提供的链接和本地/网络文件的使用结合起来。

在下面的代码中,我使用了一张我的猫的照片和一张铃铛的照片在它们之间来回切换。python 读取文件路径需要额外的转义反斜杠。我在本地路径和网络路径的示例中包含了 2 条路径,它们都可以工作。另请注意,此代码不关心我的 Bell 图像是 .gif 而不是 .png。

使用更多逻辑编码和文档属性,您可以更具体地告诉它您要打开什么文件。(如果 property = "Dog" 则抓取 dog 路径,elif property = "Kid" 然后抓取 child 路径等)

使这项工作为您工作的关键元素是 Image.FromFile( stringPath ) 函数。其余部分遵循源材料并将其保存为 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

来源:
http ://www.grasshopper3d.com/forum/topics/read-images-in-python——在 Python 中读取图像文件
http://spotfire.tibco.com/tips/2014/02/25/dynamically- display-images-in-a-text-area/ -- 将图像转换为 blob 的源材料

于 2015-04-22T17:04:43.080 回答