ImageJ Macro: Undefined Variable when saving an image to a directory - imagej

When I am changing open image window it forgets the directory, how can I retrieve it to save again
This is how I create a direcotry in Stack image folder
dir=getDirectory("image");
print(dir)
splitDir= dir + "OneChannel";
File.makeDirectory(splitDir);
print(splitDir);
This is where I change the active image window to Montage.tiff to save it to splitDir folder
title= getTitle()
saveAs("tiff",splitDir+title);
The error is undefined variable. Should I make global splitdir to keep it when I change the active image window
Error: Undefined variable in line 2:
saveAs ( "tiff" , <splitDir> + title ) ;
Thanks in advance

Variables in ImageJ macros are case-sensitive. In your code you have defined splitDir but have not defined splitdir, hence the error.
Change the line to saveAs("tiff",splitDir+title); and it should work.

Related

Is there a way to compose a batch of images with imagemagick?

I have a series of PNG images (ABC_a.png, ABC_b.png, XYZ_a.png, XYZ_b.png, BCA_a.png, BCA_b.png etc.) and would like to compose every image of the same code (i.e. the name of an image without _a or _b) within a folder.
Manually, the code would look like this:
magick composite ABC_b.png ABC_a.png ABC.png
magick composite XYZ_b.png XYZ_a.png XYZ.png
magick composite BCA_b.png BCA_a.BCA BCA.png
...
... where a partly transparent image _b would be placed "on top of" _a and the name of the output file losing its _a/_b suffix.
I looked around and tried several apporaches via mogrify or for loops (FOR %i IN (*.png) DO magick composite ...) but couldn't get it automated. Perhaps it would help to use two separate folders and working with the same image names (without the suffix), but I'm not sure...
I appreciate any tips. Please be aware that I'd need to work within the Windows CMD or PowerShell to make it happen.
There can be used the following batch file for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ImageMagick=magick.exe"
if exist "*!*_a.png" goto ExtendedVersion
setlocal EnableDelayedExpansion
for %%I in (*_a.png) do (
set "FileNameA=%%~nI"
set "FileName=!FileNameA:~0,-2!%%~xI"
if not exist "!FileName!" "!ImageMagick!" composite "!FileNameA:~0,-1!b%%~xI" "!FileNameA!%%~xI" "!FileName!"
)
endlocal
goto EndBatch
:ExtendedVersion
echo INFO: Extended version required because of a PNG file with exclamation marks.
for %%I in (*_a.png) do (
set "FileNameA=%%~nI"
setlocal EnableDelayedExpansion
set "FileName=!FileNameA:~0,-2!%%~xI"
if not exist "!FileName!" "!ImageMagick!" composite "!FileNameA:~0,-1!b%%~xI" "!FileNameA!%%~xI" "!FileName!"
endlocal
)
:EndBatch
endlocal
There is defined first the required execution environment with the first two command lines.
Next the environment variable ImageMagick is defined with the file name of this program. It would be best to add the full path because in this case cmd.exe would not need to search for magick.exe in current directory and next in all directories in string value of environment variable PATH using the file extensions in string value of environment variable PATHEXT before each execution of ImageMagick. The usage of the fully qualified file name of ImageMagick would avoid hundreds or even thousands of file system accesses on more than ten PNG files to process.
The IF condition in the fourth command line quickly checks if there is any PNG file with case-insensitive _a in the file name before the file extension .png containing one or more exclamation marks in the file name. The extended version of the processing loop is required if this condition is true.
The standard version enables first required delayed expansion. Then a FOR loop is used to process one PNG file after the other with case-insensitive _a in the file name before the file extension .png.
The current file name without file extension .png is assigned first to the environment variable FileNameA.
Next a string substitution is used to get from the string value of the environment variable FileNameA the file name without the last two characters _a concatenated with the file extension .png assigned to the environment variable FileName.
If there is not already a PNG file not ending with _a in the file name before the file extension, there is next executed ImageMagick with first argument being the corresponding _b.png file determined by using again a string substitution with using the file name string assigned to the environment variable FileName without last character a concatenated with b and the file extension .png and the_a.png file with the file extension as second argument and the file name without _a as third argument.
The command ENDLOCAL after the loop restores the previous environment before enabling delayed expansion and the command GOTO instructs the Windows Command Processor to continue processing the batch file with the command line below the label EndBatch which contains one more ENDLOCAL to restore the environment on starting the batch file processing.
The extended version is nearly the same as the standard version. The difference is that delayed variable expansion is not enabled on assigning the file name of the current _a.png file without the file extension to the environment variable FileNameA. That avoids interpreting the exclamation mark(s) in the file name as beginning/end of a delayed expanded variable reference resulting in a manipulation of the file name string before assigning it to the environment variable as it would happen with delayed expansion already enabled.
The extended version enables next delayed variable expansion inside the loop, does the same as the standard version and restores finally the previous environment before processing the next _a.png file.
The extended version is slower because of the environment variables list copy and the other operations made in background by every execution of SETLOCAL as explained in full details in this answer. The command ENDLOCAL in the loop is required to avoid a stack overflow on processing lots of PNG files.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
set /?
setlocal /?

Writing a file to a specific path in ruby taking the file name from excel

I am having some trouble writing a file to a specific path taking the file name from excel. Here is the code which I am using
out_file = File.new (#temp_path/ "#{obj_info[3].to_s}","w")
"#{obj_info[3].to_s}" = sample.txt
The value sample.txt comes from Excel during run time
#temp_path = "C:/Users/Somefolder/"
The error displayed is:
NoMethodError: undefined method `[]' for nil:NilClass
However, if the code is:
out_file = File.new ("#{obj_info[3].to_s}","w")
it successfully creates a file called sample.txt in the default directory. However, I want it to be stored in a specific directory and the file name needs to be passed from Excel.
Any help is much appreciated.
I believe your problem is because there a space between / and "
#temp_path/ "#{obj_info[3].to_s}
and I guess you want to build a path.
My advice is that you use File.join
f_path = File.join(#temp_path,obj_info[3].to_s)
out_file = File.new (f_path,"w")
Let me know if that solved the problem
You have 2 problems:
obj_info is nil so you make an error reading the value from excel, the error you get indicates it is on an array, in the code you published the only thing that's an array is what you read from excel.
Print the contents with p obj_info right before your code to check.
#temp_path and {obj_info[3].to_s} need to be concatenated to make a path.
You can do that with File.join like Mauricio suggests or like this
out_file = File.new ("#{#temp_path}/#{obj_info[3]}","w")
You can drop the to_s in this case.
It would be better if you publish the whole of your script that is meaningful.

Applescript: "File Already Open" when writing to new file

I'm creating a text file whose file name will consist of constant and variable strings. For whatever reason, I'm getting an error saying "[file name] is already open" when I'm actually just creating it. The file is created, but none of my content makes it into the file.
All of the fixes I've tried end in another error saying "network file permission."
Also, I should mention that my new file is going into the same container as another file that is used to create the new file, which is where the filePathAlias comes in.
Any ideas? Thanks in advance!
Here's the script:
-- get the file --
set filePathAlias to (choose file with prompt "** Choose File **")
set filePath to filePathAlias as string
tell application "Finder"
set fileName to name of filePathAlias
set containerPath to (container of filePathAlias) as string
end tell
set filePath to filePathAlias as string
-- get file container --
tell application "Finder"
set containerPath to (container of filePathAlias) as string
end tell
-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"
-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try
You didn't give an example path for filePathAlias or locationName. I was unable to reproduce the file already open error. I can reproduce the network file permission error...So:
set filepathalias to ((path to desktop folder as string) & "test" as string) as alias
--alias of folder on desktop called test... massaged well to be an alias that can later be converted to string when making containerPath
set locationName to "stuff you left out" --Just a name I assume...
-- get file container --
tell application "Finder"
set containerPath to ((container of filepathalias) as string)
end tell
-- combine file name variable with constant suffix --
set finalFile to locationName & "_RMP_2014.txt"
-- create the file (THIS IS WHERE THE ERROR COMES IN) --
set myFile to open for access (containerPath) & finalFile with write permission
set listTextFinal to "text goes here"
try
write listTextFinal to myFile as text
close access myFile
on error
close access myFile
end try
This works perfectly, if you were to be working to the desktop. The problem appears to be in the stage of getting the path correct.
Without all the massaging to the filepathalias I did in the first line we get the network file error. The file is trying to save in places you can not save to....
You will need to verify the filepathalias, containerPath, & finalFile all contain the information you'd expect.
Right below where the finalFile is set try this from the editor:
return {filepathalias as string, containerPath as string, finalFile as string}
my result from the above:
{"mac:Users:lithodora:Desktop:test:", "mac:Users:lithodora:Desktop:", "stuff you left out_RMP_2014.txt"}
That is similar to what you should expect.
-- Convert the file to a string
set desktop_path to POSIX path of (path to desktop)
set theFile to desktop_path & "subject_lines.csv" as POSIX file
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
write "Hello Kitty" to theOpenedFile
-- Close the file
close access theOpenedFile
this would cause the error "File file Mac:Users:jun:Desktop:subject_lines.csv is already open."
If delete "set theFile to theFile as string", and makes the following changes:
set theFile to desktop_path & "subject_lines.csv" as string
then the error "Network file permission error."

File access in corona project

My project stucture is :
assets
-- image
-- my_image.png
scene
-- start.lua
main.lua
from start.lua, I want to show my_image.png with the imagePath is
"../assets/image/my_image.png", but it's failed.
Of course, I can do that from the project root. In sub-folder, it's not.
Please advise.
The path is not relative to the source file. Try using the same path in start.lua than in main.lua, i.e. "assets/image/my_image.png".
You can simply have the image in any of your lua files like for instance in "main.lua" you can put:
local img = display.newImage("assets/image/my_image.png",x,y)
now if you want to put that in your start.lua there is no problem.

Unable to concatenate defined string and the text within the path to image file

I got a problem with setting a path to image within the resource file (.rc).
For some reasone it was not possible to concatenate defined string and the text.
e.g.
File1:
#define Path "Brand_1"
File2:
#include File1
Logo BITMAP Path "\Logo.bmp"
Borland resource compiler (5.4) throws error message: 39: Cannot open file: Brand_1
EDIT:
My question would be: Is is possible to combine the path for loading image using resource string variable and a string (file name).
Also, project I'm working on relates to a file (Logo.bmp) being present in two locations. I would like to have a switch (.bat file) to generate a different resouce file depending on requirements.
Thanks.
BRCC32 accepts -i as search path seperated by semicolon, so you could create a bat file like this
compile_res.bat
brcc32 -ic:\mypath1;c:\mypath2 resource_script
and you define your resource_script as normal, for ex:
resource_script.rc
myImg BITMAP Logo.bmp
myDOC RCDATA mydoc.doc
when you run the compile_res.bat, it will run the brcc32.exe with the search path, and having the bat file saves you from retyping the search path every time.
You're not concatenating anything. You're compiling to Logo BITMAP "Brand_1" "\Logo.bmp", and "Brand_1" isn't a valid path to a bitmap file.
#define in the resource compiler acts sort of like find/replace in a text processor - not exactly, but close enough in this case.
You might get by (untested) with removing the quotes and space between them, as long as there are no space characters in either the path or filename; otherwise, you're probably out of luck. (Not sure what you're trying to accomplish, anyway.)

Resources