I wonder how to convert a dm3 file into .jpg/jpeg images? there is test annotation and scale bar on the image. I setup a script but it always show that "the format cannot contain the data to be saved". This can be done via file/batch convert function. So how to realize the same function in script? Thanks
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 )
To convert to jpg you have to use "JPEG/JFIF Format" as the handler (=format).
It has to be exactly this string in the ImageDocument.ImageDocumentSaveToFile() function. Other formats are mentioned in the help (F1 > Scripting > Objects > Document Object Model > ImageDocument Object > ImageDocumentSaveToFile() function). Those are (for example):
'Gatan Format'
'Gatan 3 Format'
'GIF Format'
'BMP Format'
'JPEG/JFIF Format'
'Enhanced Metafile Format'
In your code you are using the SaveAsDialog() to get a directory. This is not necessary. You can use GetDirectoryDialog() to get a directory. This saves you the name operation for the directoryname and avoids problems when users do change your filename.
Also for concatinating paths I prefer using PathConcatenate(). On the first hand this makes your code a lot more readable since its name tells what you are doing. On the other hand this also takes care of the directory ending with \ or not and other path related things.
The following code is what I think you need:
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);
This answer is correct and should be accepted. Your problem is the wrong file-type string. You want to use "JPEG/JFIF Format"
A bit more general information on image file saving in DigitalMicrograph.
One doesn't save images but always imageDocuments that can contain one, more, or even zero image objects in them. Script-commands that save an image like SaveAsGatan() really just call things like: ImageGetOrCreateImageDocument().ImageDocumentSaveToFile()
The difference doesn't really matter for simple one-image-in-document type images, but it can make a difference when there are multiple images in a document, or when a single image is displayed multiple times simultaneously (which can be done.) So it is always good to know what "really" goes on.
ImageDocuments contain some properties relating to saving:
A save format (“Gatan Format”, “TIFF Format”, …)
Default value: What it was opened with, or last used save-format in case of creation
Script commands: ImageDocumentGetCurrentFileSaveFormat() ImageDocumentSetCurrentFileSaveFormat()
A current file path:
Default value: What it was opened from, or empty
Script commands: ImageDocumentGetCurrentFile() ImageDocumentSetCurrentFile()
A dirty-state:
Default value: clean when opened, dirty when created
Script commands: ImageDocumentIsDirty() ImageDocumentClean()
A linked-to-file state:
Default value: true when opened, false when created
Script commands: ImageDocumentIsLinkedToFile()
There are two ways of saving an imageDocument:
Saving the current document itself to disc:
void ImageDocumentSave( ImageDocument imgDoc, Number save_style ) This utilizes the current properties of the imageDocument to save it to current path in current format, marking it clean in the process. The save_style parameter determines how the program deals with missing info:
0 = never ask for path
1 = ask if not linked (or empty path)
2 = always ask
Saving a copy of the current document to disc:
void ImageDocumentSaveToFile( ImageDocument imgDoc, String handler, String fileName ) This makes a copy and save the file under provided path in the provided format. The imageDocument in memory does not change its properties. Most noticeable: It does not become clean, and it is not linked to the provided file on disc. The filename parameter specifies the saving location including the filename. If a file extension is provided, it has to match the file-format, but it can be left out. The handler parameter specified the file-format and can be anything GMS currently supports, such as:
Gatan Format
Gatan 3 Format
GIF Format
BMP Format
JPEG/JFIF Format
Enhanced Metafile Format
In short:
To save the currently opened imageDocument with a different format, you would want to do:
imageDocument doc = GetFrontImageDocument()
doc.ImageDocumentSetCurrentFileSaveFormat("TIFF Format")
doc.ImageDocumentSave(0)
While to just save a copy of the current state you would use:
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 )
enter image description here
I was trying to put a list of strings ("eggs, alfalfa sprouts, bean sprouts, cabbage, tomatoes, etc) into an array. I did this however, it put each string onto a line by itself. I essentially need to correct this so it outputs "bean sprouts" and "alfalfa sprouts" on the same line not "alfalfa" then "sprouts" on the line below it. Attached is my code as well as the output that it is giving me.
I am trying to extract some results i've got into a csv file, from a text one.
The results.txt has this form and I wanted to extract it in the following form as csv:
Benchmark, Pass/Fail, ops/m
compiler.compiler, PASSED, 18.37
compress, PASSED, 10.87
crypto.aes, PASSED, 3.91
etc...
So I want to keep only the iteration 1 results, in that form. What would you suggest me to do?
Thank you!
The following Python 2.x script should help to get you started (as was originally tagged). The results.txt file can be passed for lines containing iteration 1 as follows:
import csv
from itertools import ifilter
with open('results.txt', 'rb') as f_input, open('output.csv', 'wb') as f_output:
csv_input = csv.reader(ifilter(lambda x: "iteration 1" in x, f_input), delimiter=' ', skipinitialspace=True)
csv_output = csv.writer(f_output)
csv_output.writerow(["Benchmark", "Pass/Fail", "ops/m"])
for row in csv_input:
csv_output.writerow([row[0], 'PASSED', row[6]])
Creating an output CSV file as follows:
Benchmark,Pass/Fail,ops/m
compiler.compiler,PASSED,18.37
compress,PASSED,10.87
crypto.aes,PASSED,3.91
crypto.rsa,PASSED,8.79
crypto.signverify,PASSED,15.10
derby,PASSED,9.40
mpegaudio,PASSED,7.81
scimark.fft.large,PASSED,4.27
scimark.lu.large,PASSED,0.85
scimark.sor.large,PASSED,2.38
scimark.sparse.large,PASSED,1.46
scimark.monte_carlo,PASSED,5.65
scimark.fft.small,PASSED,8.94
scimark.lu.small,PASSED,8.24
scimark.sor.small,PASSED,12.90
scimark.sparse.small,PASSED,5.61
serial,PASSED,4.53
startup.helloworld,PASSED,41.24
startup.compiler.compiler,PASSED,2.05
startup.compress,PASSED,3.62
startup.crypto.aes,PASSED,0.92
startup.crypto.rsa,PASSED,1.87
startup.crypto.signverify,PASSED,2.76
startup.mpegaudio,PASSED,1.82
startup.scimark.fft,PASSED,4.49
startup.scimark.lu,PASSED,2.44
startup.scimark.monte_carlo,PASSED,1.27
startup.scimark.sor,PASSED,3.14
startup.scimark.sparse,PASSED,1.54
startup.serial,PASSED,1.73
startup.sunflow,PASSED,3.55
startup.xml.transform,PASSED,0.27
startup.xml.validation,PASSED,4.28
sunflow,PASSED,3.69
xml.transform,PASSED,10.41
xml.validation,PASSED,15.37
However, it simply assumes all entries in the file are considered PASSED as there is no example showing what a failed entry would look like in the sample file.
I was trying to write to a file from textbox input with date:
Here is a part of the code:
DateTime dt=System::DateTime::Now;
System::IO::StreamWriter^ history = gcnew StreamWriter("history.txt");
history->WriteLine(textBox1->Text);
history->WriteLine(dt);
history->Close();
But the output is like this: text
09/02/2015 23:26:07
But I want it to be like:
text 09/02/2015 23:26:07
And also has to append next input to next line of the file.
It's something like a log file.
you use two writeLine and so that write it in 2 Line concat them to write it in one line
history->WriteLine(textBox1->Text);
history->WriteLine(dt);
change this part to
history->write(textBox1->Text);
history->writeLine(dt);
history->WriteLine(textBox1->Text);
Using WriteLine() causes the line break of course. You'd have to use Write() instead. And fret a bit about how you get the extra space between the text and the date, never hesitate to use composite formatting in .NET:
history->WriteLine("{0} {1}", textBox1->Text, System::DateTime::Now);
Camel 2.13.0
I am attempting to consume a json string containing multiple records and produce an output file for each record.
public void configure() {
from("file:data/input")
// my bean splits the input string and returns a json string via Gson
.bean(new com.camel.Tokenizer(), "getSentences")
.split(new JsonPathExpression("$[*]"))
.convertBodyTo(String.class)
.to("file:data/output");
}
}
Sample input:
"This is string #1. This is string #2."
If I run the code above as-is I get a single output file containing "This is string #2.". If I remove the .split() call I get a single output file containing json as expected:
[
{
"token": "This is string #1."
},
{
"token": "This is string #2."
}
]
How can I achieve two output files representing both lines of data?
It occurred to me that perhaps the split was working correctly and the second output file was overwriting the first. According to the documentation, the default behavior if CamelFileName is not set is to create a unique generated ID but I do not experience this. In my case the output file name always matches the input file name.
How can I get unique file name within each folder?
Thanks!
Finally stumbled upon the proper search terms and came across the following helpful post: Camel: Splitting a collection and writing to files
The trick is to use a bit of logic in the .to() to achieve unique output file names:
.to("file:data/sentence_q?fileName=${header.CamelSplitIndex}.txt");
The JsonPathExpression works like a charm and no need for a Processor() or unmarshal(), as I'd tried previously.