This is an archive of the old MediaWiki-based ImageJ wiki. The current website can be found at imagej.net.

Find Dimension of Raw Image

Purpose

This script serves two purposes:

  • to demonstrate that an AWT Listener can be written in Jython, and
  • to find the width of an image you know is uncompressed, but do not know the dimensions.

To use it, open the raw image with File  › Import  › Raw... choosing a width and height that should roughly be the correct one. Then start this script, which will open a dialog box with a slider, with which you can interactively test new widths -- the pixels in the image window will be updated accordingly.

The script is maintained by Johannes Schindelin.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# This script serves two purposes:
#
# - to demonstrate that an AWT Listener can be written in Jython, and
#
# - to find the width of an image you know is uncompressed, but do not know
#   the dimensions.
#
# To use it, open the raw image with {{bc | File | Import | Raw...}} choosing a width and
# height that should roughly be the correct one.  Then start this script,
# which will open a dialog box with a slider, with which you can interactively
# test new widths -- the pixels in the image window will be updated accordingly.
 
from ij.gui import GenericDialog
 
from java.awt.event import AdjustmentListener
 
from java.lang import Math, System
 
image = WindowManager.getCurrentImage()
ip = image.getProcessor()
pixelsCopy = ip.getPixelsCopy()
pixels = ip.getPixels()
width = ip.getWidth()
height = ip.getHeight()
 
minWidth = int(Math.sqrt(len(pixels) / 16))
maxWidth = minWidth * 16
 
class Listener(AdjustmentListener):
    def adjustmentValueChanged(self, event):
        value = event.getSource().getValue()
        rowstride = min(width, value)
        for j in range(0, min(height, int(width * height / value))):
            System.arraycopy(pixelsCopy, j * value,
                pixels, j * width, rowstride)
        image.updateAndDraw()
 
gd = GenericDialog("Width")
gd.addSlider("width", minWidth, maxWidth, ip.getHeight())
gd.getSliders().get(0).addAdjustmentListener(Listener())
gd.showDialog()
if gd.wasCanceled():
    pixels[0:width * height] = pixelsCopy
    image.updateAndDraw()

See also