Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions omero/export_scripts/Batch_Image_Export.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ def savePlane(image, format, cName, zRange, projectZ, t=0, channel=None, greysca
imgName = makeImageName(originalName, cName, zRange, t, "png", folder_name)
log("Saving image: %s" % imgName)
plane.save(imgName, "PNG")
elif format == 'TIFF':
imgName = makeImageName(originalName, cName, zRange, t, "tiff", folder_name)
log("Saving image: %s" % imgName)
plane.save(imgName, 'TIFF')
else:
imgName = makeImageName(originalName, cName, zRange, t, "jpg", folder_name)
log("Saving image: %s" % imgName)
Expand Down Expand Up @@ -383,16 +387,22 @@ def getTrange(sizeT, scriptParams):

# zip everything up (unless we've only got a single ome-tiff)
if format == 'OME-TIFF' and len(os.listdir(exp_dir)) == 1:
ometiffIds = [t.id for t in parent.listAnnotations(ns=omero.constants.namespaces.NSOMETIFF)]
print "Deleting OLD ome-tiffs: %s" % ometiffIds
conn.deleteObjects("Annotation", ometiffIds)
export_file = os.path.join(folder_name, os.listdir(exp_dir)[0])
namespace = omero.constants.namespaces.NSOMETIFF
outputDisplayName = "OME-TIFF"
mimetype = 'image/tiff'
else:
export_file = "%s.zip" % folder_name
compress(export_file, folder_name)
mimetype='application/zip'
outputDisplayName = "Batch export zip"
namespace = omero.constants.namespaces.NSCREATED+"/omero/export_scripts/Batch_Image_Export"

namespace = omero.constants.namespaces.NSCREATED+"/omero/export_scripts/Batch_Image_Export"
fileAnnotation, annMessage = script_utils.createLinkFileAnnotation(conn, export_file, parent,
output="Batch export zip", ns=namespace, mimetype=mimetype)
output=outputDisplayName, ns=namespace, mimetype=mimetype)
message += annMessage
return fileAnnotation, message

Expand All @@ -404,6 +414,7 @@ def runScript():
dataTypes = [rstring('Dataset'),rstring('Image')]
formats = [rstring('JPEG'),
rstring('PNG'),
rstring('TIFF'),
rstring('OME-TIFF')]
defaultZoption = 'Default-Z (last-viewed)'
zChoices = [rstring(defaultZoption),
Expand Down Expand Up @@ -465,7 +476,7 @@ def runScript():
description="The max width of each image panel. Default is actual size", min=1),

scripts.String("Format", grouping="8",
description="Format to save image", values=formats, default='PNG'),
description="Format to save image", values=formats, default='JPEG'),

scripts.String("Folder_Name", grouping="9",
description="Name of folder (and zip file) to store images", default='Batch_Image_Export'),
Expand Down
145 changes: 78 additions & 67 deletions omero/figure_scripts/Movie_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import StringIO
from omero_sys_ParametersI import ParametersI
from datetime import date
import math

try:
from PIL import Image, ImageDraw # see ticket:2597
Expand All @@ -58,22 +59,19 @@
COLOURS = scriptUtil.COLOURS # name:(rgba) map
OVERLAY_COLOURS = dict(COLOURS, **scriptUtil.EXTRA_COLOURS)

JPEG = "image/jpeg"
PNG = "image/png"

logLines = [] # make a log / legend of the figure
def log(text):
print text
logLines.append(text)


def getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer,
algorithm, stepping, scalebar, overlayColour, timeUnits):

def createMovieFigure(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer,
algorithm, stepping, scalebar, overlayColour, timeUnits, imageLabels, maxColCount):
"""
Makes a canvas showing an image per row with multiple columns showing
Makes the complete Movie figure: A canvas showing an image per row with multiple columns showing
frames from each image/movie. Labels obove each frame to show the time-stamp of that frame in the
specified units.
specified units and labels on the left name each image.

@param session The OMERO session
@param pixelIds A list of the Pixel IDs for the images in the figure
Expand All @@ -88,6 +86,7 @@ def getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer
@param scalebar A number of microns for scale-bar
@param overlayColour Colour of the scale-bar as tuple (255,255,255)
@param timeUnits A string such as "SECS"
@param imageLabels A list of lists, corresponding to pixelIds, for labelling each image with one or more strings.
"""

mode = "RGB"
Expand Down Expand Up @@ -179,17 +178,20 @@ def getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer


# make a canvas for the row of splitview images...(will add time labels above each row)
colCount = min(maxColCount, len(renderedImages))
rowCount = math.ceil(float(len(renderedImages)) / colCount)
font = imgUtil.getFont(width/12)
fontHeight = font.getsize("Textq")[1]
canvasWidth = ((width + spacer) * len(renderedImages)) + spacer
canvasHeight = spacer/2 + fontHeight + spacer + height
canvasWidth = ((width + spacer) * colCount) + spacer
canvasHeight = rowCount * (spacer/2 + fontHeight + spacer + height)
size = (canvasWidth, canvasHeight)
canvas = Image.new(mode, size, white) # create a canvas of appropriate width, height

# add text labels
queryService = conn.getQueryService()
textX = spacer
textY = spacer/4
colIndex = 0
timeLabels = figUtil.getTimeLabels(queryService, pixelsId, tIndexes, sizeT, timeUnits)
for t, tIndex in enumerate(tIndexes):
if tIndex >= sizeT:
Expand All @@ -200,6 +202,11 @@ def getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer
textdraw = ImageDraw.Draw(canvas)
textdraw.text((textX+inset, textY), time, font=font, fill=(0,0,0))
textX += width + spacer
colIndex += 1
if colIndex >= maxColCount:
colIndex = 0
textX = spacer
textY += (spacer/2 + fontHeight + spacer + height)

# add scale bar to last frame...
if scalebar:
Expand All @@ -214,13 +221,23 @@ def getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer

px = spacer
py = spacer + fontHeight
colIndex = 0
# paste the images in
for img in renderedImages:
for i, img in enumerate(renderedImages):
imgUtil.pasteImage(img, canvas, px, py)
px = px + width + spacer

totalWidth = max(totalWidth, canvasWidth) # most should be same width anyway
totalHeight = totalHeight + canvasHeight # add together the heights of each row
colIndex += 1
if colIndex >= maxColCount:
colIndex = 0
px = spacer
py += (spacer/2 + fontHeight + spacer + height)

# Add labels to the left of the panel
canvas = addLeftLabels(canvas, imageLabels, row, width, spacer)

totalWidth = max(totalWidth, canvas.size[0]) # most should be same width anyway
totalHeight = totalHeight + canvas.size[1] # add together the heights of each row

rowPanels.append(canvas)

# make a figure to combine all split-view rows
Expand All @@ -236,70 +253,58 @@ def getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer
return figureCanvas


def createMovieFigure(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer,
algorithm, stepping, scalebar, overlayColour, timeUnits, imageLabels):
def addLeftLabels(panelCanvas, imageLabels, rowIndex, width, spacer):
"""
Makes the complete Movie figure: A canvas showing an image per row with multiple columns showing
frames from each image/movie. Labels obove each frame to show the time-stamp of that frame in the
specified units and labels on the left name each image.

@param session The OMERO session
@param pixelIds A list of the Pixel IDs for the images in the figure
@param tIndexes A list of tIndexes to display frames from
@param zStart Projection Z-start
@param zEnd Projection Z-end
@param width Maximum width of panels
@param height Max height of panels
@param spacer Space between panels
@param algorithm Projection algorithm e.g. "MAXIMUMINTENSITY"
@param stepping Projecttion z-step
@param scalebar A number of microns for scale-bar
@param overlayColour Colour of the scale-bar as tuple (255,255,255)
@param timeUnits A string such as "SECS"
@param imageLabels A list of lists, corresponding to pixelIds, for labelling each image with one or more strings.
Takes a canvas of panels and adds one or more labels to the left,
with the text aligned vertically.
NB: We are passed the set of labels for ALL image panels (as well as the
index of the current image panel) so that we know what is the max label count
and can give all panels the same margin on the left.

@param panelCanvas: PIL image - add labels to the left of this
@param imageLabels: A series of label lists, one per image. We only add labels from one list
@param rowIndex: The index of the label list we're going to use from imageLabels
@param width: Simply used for finding a suitable font size
@param spacer: Space between panels
"""

panelCanvas = getImageFrames(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer,
algorithm, stepping, scalebar, overlayColour, timeUnits)


# add lables to row...
mode = "RGB"
white = (255,255,255)
font = imgUtil.getFont(width/12)
textHeight = font.getsize("Sampleq")[1]
textGap = spacer /2
rowSpacing = panelCanvas.size[1]/len(pixelIds)
#rowSpacing = panelCanvas.size[1]/len(pixelIds)

# find max number of labels
maxCount = 0
rowHeights = []
for row in imageLabels:
maxCount = max(maxCount, len(row))
leftTextWidth = (textHeight + textGap) * maxCount
size = (panelCanvas.size[1], leftTextWidth) # make the canvas as wide as the panels height
leftTextHeight = (textHeight + textGap) * maxCount
leftTextWidth = panelCanvas.size[1] # make the canvas as wide as the panels height
size = (leftTextWidth, leftTextHeight)
textCanvas = Image.new(mode, size, white)
textdraw = ImageDraw.Draw(textCanvas)
px = spacer
imageLabels.reverse()
for row in imageLabels:
py = leftTextWidth - textGap # start at bottom
for l, label in enumerate(row):
py = py - textHeight # find the top of this row
w = textdraw.textsize(label, font=font) [0]
inset = int((height - w) / 2)
textdraw.text((px+inset, py), label, font=font, fill=(0,0,0))
py = py - textGap # add space between rows
px = px + rowSpacing # 2 spacers between each row


labels = imageLabels[rowIndex]
py = leftTextHeight - textGap # start at bottom
for l, label in enumerate(labels):
py = py - textHeight # find the top of this row
w = textdraw.textsize(label, font=font) [0]
inset = int((leftTextWidth - w) / 2)
textdraw.text((inset, py), label, font=font, fill=(0,0,0))
py = py - textGap # add space between rows

# make a canvas big-enough to add text to the images.
canvasWidth = leftTextWidth + panelCanvas.size[0]
canvasWidth = leftTextHeight + panelCanvas.size[0] # TextHeight will be width once rotated
canvasHeight = panelCanvas.size[1]
size = (canvasWidth, canvasHeight)
canvas = Image.new(mode, size, white) # create a canvas of appropriate width, height

# add the panels to the canvas
pasteX = leftTextWidth
pasteX = leftTextHeight
pasteY = 0
imgUtil.pasteImage(panelCanvas, canvas, pasteX, pasteY)

Expand All @@ -310,8 +315,8 @@ def createMovieFigure(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spa
imgUtil.pasteImage(textV, canvas, spacer/2, 0)

return canvas


def movieFigure(conn, commandArgs):
"""
Makes the figure using the parameters in @commandArgs, attaches the figure to the
Expand All @@ -332,8 +337,9 @@ def movieFigure(conn, commandArgs):
"MINS_SECS": "mins:secs",
"HOURS_MINS": "hours:mins"}
timeUnits = "SECS"
if "timeUnits" in commandArgs:
timeUnits = commandArgs["timeUnits"]
if "Time_Units" in commandArgs:
timeUnits = commandArgs["Time_Units"]
timeUnits = timeUnits.replace(" ", "_") # convert from UI name to timeLabels key
if timeUnits not in timeLabels.keys():
timeUnits = "SECS"
log("Time units are in %s" % timeLabels[timeUnits])
Expand Down Expand Up @@ -455,30 +461,33 @@ def getTags(name, tagsList, pdList):
if "Scalebar_Colour" in commandArgs:
r,g,b,a = OVERLAY_COLOURS[commandArgs["Scalebar_Colour"]]
overlayColour = (r,g,b)

maxColCount = 10
if "Max_Columns" in commandArgs:
maxColCount = commandArgs["Max_Columns"]

figure = createMovieFigure(conn, pixelIds, tIndexes, zStart, zEnd, width, height, spacer,
algorithm, stepping, scalebar, overlayColour, timeUnits, imageLabels)

#figure.show()
algorithm, stepping, scalebar, overlayColour, timeUnits, imageLabels, maxColCount)

log("")
figLegend = "\n".join(logLines)

#print figLegend # bug fixing only

format = JPEG
if "Format" in commandArgs:
if commandArgs["Format"] == "PNG":
format = PNG
format = commandArgs["Format"]

output = "movieFigure"
if "Figure_Name" in commandArgs:
output = str(commandArgs["Figure_Name"])

if format == PNG:
if format == 'PNG':
output = output + ".png"
figure.save(output, "PNG")
mimetype = "image/png"
elif format == 'TIFF':
output = output + ".tiff"
figure.save(output, "TIFF")
mimetype = "image/tiff"
else:
output = output + ".jpg"
figure.save(output)
Expand All @@ -501,7 +510,7 @@ def runAsScript():
labels = [rstring('Image Name'), rstring('Datasets'), rstring('Tags')]
algorithums = [rstring('Maximum Intensity'),rstring('Mean Intensity')]
tunits = [rstring("SECS"), rstring("MINS"), rstring("HOURS"), rstring("MINS SECS"), rstring("HOURS MINS")]
formats = [rstring('JPEG'),rstring('PNG')]
formats = [rstring('JPEG'),rstring('PNG'),rstring('TIFF')]
ckeys = COLOURS.keys()
ckeys.sort()
cOptions = wrap(ckeys)
Expand Down Expand Up @@ -541,11 +550,13 @@ def runAsScript():
scripts.String("Scalebar_Colour", grouping="10.2",
description="The colour of the scalebar.",default='White',values=oColours),
scripts.String("Format", grouping="11",
description="Format to save image.", values=formats),
description="Format to save image.", values=formats, default='JPEG'),
scripts.String("Figure_Name", grouping="12",
description="File name of the figure to save."),
scripts.String("Time_Units", grouping="13",
description="The units to use for time display", values=tunits),
scripts.Int("Max_Columns", grouping="04.1", default=10,
description="The maximum number of columns in the figure, for movie frames.", min=1),

version = "4.3.0",
authors = ["William Moore", "OME Team"],
Expand Down
15 changes: 7 additions & 8 deletions omero/figure_scripts/Movie_ROI_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
except ImportError:
import Image, ImageDraw # see ticket:2597

JPEG = "image/jpeg"
PNG = "image/png"

COLOURS = scriptUtil.COLOURS
OVERLAY_COLOURS = dict(COLOURS, **scriptUtil.EXTRA_COLOURS)
Expand Down Expand Up @@ -628,19 +626,20 @@ def getTags(name, tagsList, pdList):

#print figLegend # bug fixing only

format = JPEG
if "Format" in commandArgs:
if commandArgs["Format"] == "PNG":
format = PNG
format = commandArgs["Format"]

output = "movieROIFigure"
if "Figure_Name" in commandArgs:
output = str(commandArgs["Figure_Name"])

if format == PNG:
if format == 'PNG':
output = output + ".png"
fig.save(output, "PNG")
mimetype = "image/png"
elif format == 'TIFF':
output = output + ".tiff"
fig.save(output, "TIFF")
mimetype = "image/tiff"
else:
output = output + ".jpg"
fig.save(output)
Expand All @@ -666,7 +665,7 @@ def runAsScript():
algorithums = [rstring('Maximum Intensity'),rstring('Mean Intensity')]
roiLabel = """Specify an ROI to pick by specifying it's shape label. 'FigureROI' by default,
(not case sensitive). If matching ROI not found, use any ROI."""
formats = [rstring('JPEG'),rstring('PNG')]
formats = [rstring('JPEG'),rstring('PNG'),rstring('TIFF')]
ckeys = COLOURS.keys()
ckeys.sort()
cOptions = wrap(ckeys)
Expand Down
Loading