Posted by
Volker Baecker on
May 19, 2010; 4:57pm
URL: http://imagej.273.s1.nabble.com/Every-time-an-image-is-opened-tp3688256p3688259.html
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I wrote one as well. It allows to configure a macro and the event.
Here is the source. I'll put it for download on our website tomorrow.
Best regards,
Volker Baecker
/**
* This plugin is in the public domain.
* Author: Volker Baecker (volker.baecker at mri.cnrs.fr)
*/
import java.util.HashSet;
import java.util.Set;
import ij.IJ;
import ij.ImageListener;
import ij.ImagePlus;
import ij.gui.GenericDialog;
import ij.plugin.PlugIn;
/**
* Run a macro-file from ImageJ's macros folder, each time a selected
image-event
* is sent.
*
* @author Volker Baecker
*
*/
public class RunMacroOnEvent_ implements ImageListener, PlugIn {
private GenericDialog dialog;
private String macroFile;
private String event;
static public Set<RunMacroOnEvent_> instances = new
HashSet<RunMacroOnEvent_>();
@Override
public void imageClosed(ImagePlus imp) {
if (event.equals("closed")) IJ.runMacroFile(macroFile);
}
@Override
public void imageOpened(ImagePlus imp) {
if (event.equals("opened")) IJ.runMacroFile(macroFile);
}
@Override
public void imageUpdated(ImagePlus imp) {
if (event.equals("updated")) IJ.runMacroFile(macroFile);
}
@Override
public void run(String arg) {
for (RunMacroOnEvent_ instance : instances)
ImagePlus.removeImageListener(instance);
instances.clear();
this.showDialog();
macroFile = dialog.getNextString();
event = dialog.getNextChoice();
if (event.equals("none")) return;
ImagePlus.addImageListener(this);
instances.add(this);
}
private void showDialog() {
dialog = new GenericDialog("Run Macro On Event");
dialog.addStringField("macro", "ShowImageInfo.txt");
String[] events = {"opened", "closed", "updated", "none"};
dialog.addChoice("event", events, events[0]);
dialog.showDialog();
}
}
On 19/05/10 18:37, Michael Schmid wrote:
> Hi Scott,
>
> Here is a very simple plugin that registers as an ImageListener. You can
> call it in your StartupMacros.txt.
>
>
> import ij.*;import ij.plugin.*;
>
> public class Always_Open_16bit_Fullscale implements PlugIn, ImageListener {
> public void run(String arg) {
> ImagePlus.addImageListener(this);
> }
>
> public void imageOpened(ImagePlus imp) {
> //if it does not work, add this: IJ.wait(100);
> if (imp.getType()==ImagePlus.GRAY16) imp.setDisplayRange(0, 65535);
> imp.updateAndDraw();
> }
> public void imageClosed(ImagePlus imp){}
> public void imageUpdated(ImagePlus imp){}
> }
>
> You should call it only once; otherwise you should add some 'singleton'
> code.
>
>
> Concerning drawing: if you only want to draw points, you could also
> create your own drawing macro tool that uses the setPixel(x, y, value)
> command to draw with a fixed value rather than a color that depends on
> the display range. Drawing 1-pixel-wide lines would be also possible,
> but slightly more complex code.
>
> Michael
> ________________________________________________________________
>
> On 19 May 2010, at 17:06, SHW wrote:
>
>> General:
>> Is there way to do an action/Macro/etc. every time an image is opened,
>> automatically?
>>
>> I would please like to know the actual answer to my question, as it
>> applies
>> to many other situations also. Other workarounds for this specific
>> situation may be helpful, and would be appreciated, but would not address
>> the issue in general. Therefore, my specific problem is listed below.
>>
>> I appreciate any and all help.
>> - Scott Williams, PhD -
>>
>>
>> Specific:
>> I have 2048x2048 16-bit grayscale scale images. ImageJ automatically
>> sets
>> Min and Max display values to the smallest and largest value in the file,
>> respectively, which are often NOT 0 and 65535. I can manually set these
>> values through Balance and Contrast (B&C)>[Set], or use setMinAndMax()
>> in a
>> macro to do the same. But any time I go to B&C, the [Reset] button goes
>> back to these values. I do not want this "option".
>> Additionally, this default value range actually PROHIBITS the manual
>> drawing
>> of any value higher than the largest in the file. Even if I set the
>> Pencil
>> to 255,255,255, it does not draw an actual white of 65565.
>>
>> My current workaround is to draw a single Black pixel in the upper
>> left, and
>> a single White pixel in the lower right, through a macro with:
>> setPixel (0,0,0)
>> setPixel (2047,2047,65535)
>>
>> This will ensure that B&C [Reset] never shows lower than the full 16-bit
>> range. Also, it will ensure that that I can manually draw true White.
>>
>> I would like a way for this simple macro to apply, automatically, each
>> and
>> every time I open an image.
>>
>>
>> Long-term, I might suggest:
>> - adding a [Full] button to the B&C pop-up menu
>> - relabeling the [Reset] button as something more appropriate, perhaps
>> [Range], and
>> - making the "auto-[Range]-on-Open" a Option somewhere which could be
>> enabled/disabled.
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla -
http://enigmail.mozdev.org/iEYEARECAAYFAkv0GFwACgkQ0gXPLVKexCcixACfW6RXzk37eMXaJumnCTcQLqfk
cIQAn2bF2BatPtz3jNGxl6YKZYG+6U7a
=0yHR
-----END PGP SIGNATURE-----
--
passerelle antivirus du campus CNRS de Montpellier
--