【问题标题】:How to convert a .dm3 file (with annotation and scale bar) to .jpg/jpeg image?如何将 .dm3 文件(带注释和比例尺)转换为 .jpg/jpeg 图像?
【发布时间】:2021-10-24 15:45:05
【问题描述】:

我想知道如何将 dm3 文件转换为 .jpg/jpeg 图像?图像上有测试注释和比例尺。我设置了一个脚本,但它总是显示“格式不能包含要保存的数据”。这可以通过文件/批量转换功能来完成。那么如何在脚本中实现同样的功能呢?谢谢

image test:=IntegerImage("test",2,1,100,100)
test.ShowImage()
image frontimage:=GetFrontImage()
string  filename=getname(frontimage)
imagedisplay disp = frontImage.ImageGetImageDisplay(0)
disp.applydatabar()
ImageDocument frontDoc = GetFrontImageDocument() 
string directoryname, pathname
number length
if(!SaveAsDialog("","Do Not Change Me",directoryname)) exit(0)
length=len(directoryname)-16 
directoryname=mid(directoryname,0,length) 
pathname=directoryname+filename
frontDoc.ImageDocumentSaveToFile( "JPG Format", pathname ) 

【问题讨论】:

  • mile7 下面的回答是正确的。本质上,您使用了错误的文件类型字符串。没有“JPG 格式”,只有“JPEG/JFIF 格式”。错误消息确实说明了这一点。如果你完整引用它,它会说:“无法识别的文件格式,或者该格式不能包含要保存的数据。”

标签: export saving-data dm-script


【解决方案1】:

要转换为 jpg,您必须使用 "JPEG/JFIF Format" 作为 handler(=格式)。

它必须是 ImageDocument.ImageDocumentSaveToFile() 函数中的这个字符串。帮助中提到了其他格式(F1 > 脚本 > 对象 > 文档对象模型 > ImageDocument 对象 > ImageDocumentSaveToFile() 函数)。这些是(例如):

  • '加坦格式'
  • 'Gatan 3 格式'
  • 'GIF 格式'
  • 'BMP 格式'
  • 'JPEG/JFIF 格式'
  • '增强的元文件格式'

在您的代码中,您使用SaveAsDialog() 来获取目录。这不是必需的。您可以使用GetDirectoryDialog() 获取目录。这样可以为您节省 directoryname 的名称操作,并避免用户更改您的文件名时出现问题。

对于连接路径,我更喜欢使用PathConcatenate()。一方面,这使您的代码更具可读性,因为它的名称说明了您在做什么。另一方面,这也处理了是否以\ 结尾的目录以及其他与路径相关的事情。


以下代码是我认为你需要的:

Image test := IntegerImage("test", 2, 1, 100, 100);
test.ShowImage();

Image frontimage := GetFrontImage();

ImageDisplay disp = frontImage.ImageGetImageDisplay(0);
disp.applydatabar();

ImageDocument frontDoc = GetFrontImageDocument();

string directoryname;
if(!GetDirectoryDialog("Select directory", "C:\\\\", directoryname)){
    //                                        ↑
    // You can of course use something else as the start point for selection here
    exit(0);
}

string filename = GetName(frontimage);
string pathname = directoryname.PathConcatenate(filename);

frontDoc.ImageDocumentSaveToFile("JPEG/JFIF Format", pathname);

【讨论】:

  • 非常感谢。这正是我所需要的,并且对我的项目很有帮助。我真的犯了一个愚蠢的错误。感谢您的详细指导。
  • @user11078158 请单击问题左上角投票图标下方的勾号,将其中一个答案标记为“正确答案”。这样系统就知道这个问题得到了回答。其他人在发现您的问题时也知道这一点。
【解决方案2】:

这个answer 是正确的,应该被接受。您的问题是错误的文件类型字符串。您想使用“JPEG/JFIF 格式”


有关在 DigitalMicrograph 中保存图像文件的更多一般信息。

  • 不保存 images,但总是保存 imageDocuments,其中可以包含一个、多个甚至零个 image 对象。像 SaveAsGatan() 这样保存 图像 的脚本命令实际上只是调用类似:ImageGetOrCreateImageDocument().ImageDocumentSaveToFile()

    对于简单的文档中单图像类型的图像,差异并不重要,但当文档中有多个图像或单个图像同时显示多次时,它会产生差异(可以做到。)所以知道“真正”发生了什么总是好的。

  • ImageDocuments 包含一些与保存相关的属性:

    • 保存格式(“Gatan 格式”、“TIFF 格式”……)
      • 默认值:打开时使用什么格式,或者创建时最后使用的保存格式
      • 脚本命令
        ImageDocumentGetCurrentFileSaveFormat()
        ImageDocumentSetCurrentFileSaveFormat()
    • 当前的文件路径
      • 默认值:打开的地方,或为空
      • 脚本命令
        ImageDocumentGetCurrentFile()
        ImageDocumentSetCurrentFile()
    • 脏状态
      • 默认值:打开时干净,创建时脏
      • 脚本命令
        ImageDocumentIsDirty()
        ImageDocumentClean()
    • 链接到文件状态
      • 默认值:打开时为真,创建时为假
      • 脚本命令
        ImageDocumentIsLinkedToFile()
  • 保存imageDocument两种方法

    • 当前文档本身保存到光盘:

      void ImageDocumentSave( ImageDocument imgDoc, Number save_style )

      这利用 imageDocument 的当前属性将其以当前格式保存到当前路径,在此过程中将其标记为干净。
      save_style 参数决定程序如何处理丢失的信息:

      • 0 = 从不询问路径
      • 1 = 询问是否未链接(或空路径)
      • 2 = 总是问
    • 当前文档的副本保存到光盘:

      void ImageDocumentSaveToFile( ImageDocument imgDoc, String handler, String fileName )

      这会复制并以提供的格式将文件保存在提供的路径下。内存中的 imageDocument 不会改变它的属性。最值得注意的是:它确实没有变干净,并且它没有链接到光盘上提供的文件。
      filename 参数指定了保存位置包括文件名。如果提供了文件扩展名,它必须与文件格式匹配,但可以省略。
      handler 参数指定文件格式,可以是 GMS 当前支持的任何格式,如:

      • Gatan Format
      • Gatan 3 Format
      • GIF Format
      • BMP Format
      • JPEG/JFIF Format
      • Enhanced Metafile Format

简而言之:

要将当前打开的 imageDocument 保存为不同的格式,您需要这样做:

imageDocument doc = GetFrontImageDocument()
doc.ImageDocumentSetCurrentFileSaveFormat("TIFF Format")
doc.ImageDocumentSave(0)

虽然只是保存您将使用的当前状态的副本:

imageDocument doc = GetFrontImageDocument()
string path = doc.ImageDocumentGetCurrentFile() // full path including extension!
path = PathExtractDirectory(path,0) + PathExtractBaseName(path,0) // path without file extension
doc.ImageDocumentSaveToFile("TIFF Format", path )

【讨论】:

  • 非常感谢您的详细说明。这些信息帮助我解决了很多我一直有的难题。我不熟悉这些函数的参数。再次,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-05
  • 2012-12-21
  • 1970-01-01
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多