可以直接将.png文件加载到DigitalMicrograph中吗?

huangapple go评论106阅读模式
英文:

Is it possible to load .png files into DigitalMicrograph directly

问题

有人成功将.png文件直接加载到DM中吗?我使用了菜单项“导入数据...”,知道了像素大小并尝试了所有组合,但没有成功。任何指针都将非常受欢迎。

英文:

Has anyone been successful in loading .png files directly into DM? I used the menu item 'Import Data..." and know the image size in pixels and tried all combinations but did not succeed. Any pointer will be very welcome.

答案1

得分: 2

PNG 不是GMS支持的文件格式,因此不存在用于读取它的脚本命令。

由于PNG的格式规范,直接的二进制导入将失败。它不仅仅是一个值数组的二进制表示,而是以更复杂的方式编码。

然而,PNG文件格式已经得到官方文档支持,原则上可以使用DM脚本语言编写一个导入类,利用Stream对象中的原始文件读取功能,并根据文件格式定义解释值。

另一种方法是依赖Python功能和适当的库,然后在GMS中调用Python脚本,可以作为独立的Python脚本或作为DM脚本内的Python调用。


为了完整起见,应该提到GMS支持GIF、JPG和BMP格式,其中任何一个都可以由DM脚本直接读取:

NewImageDocumentFromFile("C:/temp/sample.bmp").ImageDocumentShow()
NewImageDocumentFromFile("C:/temp/sample.gif").ImageDocumentShow()
NewImageDocumentFromFile("C:/temp/sample.jpg").ImageDocumentShow()
英文:

PNG is not a supported file format by GMS and hence no script
command exists to read it neither.

A direct raw binary import will fail because of the Format specifications of PNG. It is not just a binary representation of a value array but encoded in a more complex manner.

However, the PNG file format is officially documented and one could in principle write an import class in the DM Scripting language, utilizing the raw-file reading in the Stream object and interpreting the values as per the file format definitions.

An alternative approach would be to rely on Python functionality and a suitable library, and then call the Python script within GMS either as a stand-along Python script or as a Python-call within a DM-script.


For completeness sake it should be mentioned that GMS supports GIF, JPG and BMP formats, and that either of them can be directly read by DM script as well:

NewImageDocumentFromFile("C:/temp/sample.bmp").ImageDocumentShow()
NewImageDocumentFromFile("C:/temp/sample.gif").ImageDocumentShow()
NewImageDocumentFromFile("C:/temp/sample.jpg").ImageDocumentShow()

答案2

得分: 2

以下是关于Python解决方法的描述,用于导入灰度值PNG图像。
(彩色PNG可能需要一些适应性调整。)

步骤1 - 准备Python

我假设Phyton for GMS已经通过默认设置的安装程序安装。
为了将PNG图像导入Python,需要一个合适的库。我正在使用Pillow

要在GMS中安装要在其中使用的Python库,请按照F1帮助文档中详细的说明进行操作:

  • 以管理员权限打开Anaconda提示符
  • 使用以下命令切换到GMS_VENV_PYTHON虚拟环境:activate GMS_VENV_PYTHON
  • 使用以下命令安装Pillow:pip install Pillow

可以直接将.png文件加载到DigitalMicrograph中吗?

步骤2 - 通过Python导入

启动GMS后,下面的Python脚本应该可以工作,前提是在指定的位置有一张灰度PNG图像:

import numpy
from PIL import Image
img = Image.open('C:/temp/sample2.png')
DM.CreateImage(numpy.array(img).copy()).ShowImage()

步骤3 - 通过Python调用DM-Script导入

如果上述步骤有效,还可以使用调用Python代码导入PNG图像的DM-Script:

void ReadGreyPNG(string path)
{
	string PythonHelp
	PythonHelp += "import numpy"+"\n"
	PythonHelp += "from PIL import Image"+"\n"
	PythonHelp += "img = Image.open('"
	pythonHelp += path
	PythonHelp += "')"+ "\n"
	PythonHelp += "DM.CreateImage(numpy.array(img).copy()).ShowImage()"
	ExecutePythonScriptString(PythonHelp)	
}
ReadGreyPNG("C:/temp/sample2.png")
英文:

Here is a description for the Python workaround, importing a grey value PNG.
(Color PNGs would need a bit of adaptation.)

STEP 1 - Prepare Python

I am assuming that Phyton for GMS has been installed by the installer with default settings.
In order to import PNG images into Python, an appropriate library is needed. I'm using Pillow

To install the Python library to be used in GMS, please follow the instructions found in detail in the F1 help documentation:

  • Open Anaconda prompt with administrative privileges
  • Switch to the GMS_VENV_PYTHON virtual environment using <br>activate GMS_VENV_PYTHON
  • Install Pillow using<br>pip install Pillow

可以直接将.png文件加载到DigitalMicrograph中吗?

STEP 2 - Import via Python

After launching GMS, the following Python script should work, provided there is a grey-scale PNG image at given location:

import numpy
from PIL import Image
img = Image.open(&#39;C:/temp/sample2.png&#39;)
DM.CreateImage(numpy.array(img).copy()).ShowImage()

STEP 3 - Import via DM-Script with Python call

Provided the above works, one can also use a DM-Script that calls Python code to import the PNG image:

void ReadGreyPNG(string path)
{
	string PythonHelp
	PythonHelp += &quot;import numpy&quot;+&quot;\n&quot;
	PythonHelp += &quot;from PIL import Image&quot;+&quot;\n&quot;
	PythonHelp += &quot;img = Image.open(&#39;&quot;
	pythonHelp += path
	PythonHelp += &quot;&#39;)&quot;+&quot;\n&quot;
	PythonHelp += &quot;DM.CreateImage(numpy.array(img).copy()).ShowImage()&quot;
	ExecutePythonScriptString(PythonHelp)	
}
ReadGreyPNG(&quot;C:/temp/sample2.png&quot;)

huangapple
  • 本文由 发表于 2023年5月29日 09:30:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定