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
48 changes: 27 additions & 21 deletions omero/export_scripts/Batch_Image_Export.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
import zipfile
from datetime import datetime

try:
from PIL import Image # see ticket:2597
except ImportError:
import Image

# keep track of log strings.
logStrings = []

Expand All @@ -71,7 +76,7 @@ def compress(target, base):
finally:
zip_file.close()

def savePlane(image, format, cName, zRange, projectZ, t=0, channel=None, greyscale=False, imgWidth=None, folder_name=None):
def savePlane(image, format, cName, zRange, projectZ, t=0, channel=None, greyscale=False, zoomPercent=None, folder_name=None):
"""
Renders and saves an image to disk.

Expand All @@ -81,7 +86,7 @@ def savePlane(image, format, cName, zRange, projectZ, t=0, channel=None, greysca
@param t: T index
@param channel: Active channel index. If None, use current rendering settings
@param greyscale: If true, all visible channels will be greyscale
@param imgWidth: Resize image to this width if specified.
@param zoomPercent: Resize image by this percent if specified.
"""

originalName = image.getName()
Expand All @@ -94,7 +99,7 @@ def savePlane(image, format, cName, zRange, projectZ, t=0, channel=None, greysca
log("t: %s" % t)
#log("channel %s" % channel)
#log("greyscale %s" % greyscale)
#log("imgWidth %s" % imgWidth)
#log("zoomPercent %s" % zoomPercent)

# if channel == None: use current rendering settings
if channel != None:
Expand All @@ -108,10 +113,10 @@ def savePlane(image, format, cName, zRange, projectZ, t=0, channel=None, greysca

# All Z and T indices in this script are 1-based, but this method uses 0-based.
plane = image.renderImage(zRange[0]-1, t-1)
if imgWidth:
if zoomPercent:
w, h = plane.size
newH = (float(imgWidth) / w ) * h
plane = plane.resize((imgWidth, int(newH)))
fraction = (float(zoomPercent) / 100 )
plane = plane.resize((w * fraction, h * fraction), Image.ANTIALIAS)

if format == "PNG":
imgName = makeImageName(originalName, cName, zRange, t, "png", folder_name)
Expand Down Expand Up @@ -177,7 +182,7 @@ def saveAsOmeTiff(conn, image, folder_name=None):


def savePlanesForImage(conn, image, sizeC, splitCs, mergedCs, channelNames=None,
zRange=None, tRange=None, greyscale=False, imgWidth=None, projectZ=False, format="PNG", folder_name=None):
zRange=None, tRange=None, greyscale=False, zoomPercent=None, projectZ=False, format="PNG", folder_name=None):
"""
Saves all the required planes for a single image, either as individual planes or projection.

Expand All @@ -187,7 +192,7 @@ def savePlanesForImage(conn, image, sizeC, splitCs, mergedCs, channelNames=None,
@param zRange: Tuple: (zStart, zStop). If None, use default Zindex
@param tRange: Tuple: (tStart, tStop). If None, use default Tindex
@param greyscale: If true, all visible channels will be greyscale
@param imgWidth: Resize image to this width if specified.
@param zoomPercent: Resize image by this percent if specified.
@param projectZ: If true, project over Z range.
"""

Expand Down Expand Up @@ -230,15 +235,15 @@ def savePlanesForImage(conn, image, sizeC, splitCs, mergedCs, channelNames=None,
for t in tIndexes:
if zRange == None:
defaultZ = image.getDefaultZ()+1
savePlane(image, format, cName, (defaultZ,), projectZ, t, c, gScale, imgWidth, folder_name)
savePlane(image, format, cName, (defaultZ,), projectZ, t, c, gScale, zoomPercent, folder_name)
elif projectZ:
savePlane(image, format, cName, zRange, projectZ, t, c, gScale, imgWidth, folder_name)
savePlane(image, format, cName, zRange, projectZ, t, c, gScale, zoomPercent, folder_name)
else:
if len(zRange) > 1:
for z in range(zRange[0], zRange[1]):
savePlane(image, format, cName, (z,), projectZ, t, c, gScale, imgWidth, folder_name)
savePlane(image, format, cName, (z,), projectZ, t, c, gScale, zoomPercent, folder_name)
else:
savePlane(image, format, cName, zRange, projectZ, t, c, gScale, imgWidth, folder_name)
savePlane(image, format, cName, zRange, projectZ, t, c, gScale, zoomPercent, folder_name)


def batchImageExport(conn, scriptParams):
Expand All @@ -261,9 +266,9 @@ def batchImageExport(conn, scriptParams):
channelNames = []
if "Channel_Names" in scriptParams:
channelNames = scriptParams["Channel_Names"]
imgWidth = None
if "Image_Width" in scriptParams:
imgWidth = scriptParams["Image_Width"]
zoomPercent = None
if "Zoom" in scriptParams and scriptParams["Zoom"] != "100%":
zoomPercent = int(scriptParams["Zoom"][:-1])


# functions used below for each imaage.
Expand Down Expand Up @@ -373,15 +378,15 @@ def getTrange(sizeT, scriptParams):
elif len(tRange) == 1: log(" T-index: %d" % tRange[0])
else: log(" T-range: %s-%s" % ( tRange[0],tRange[1]-1) )
log(" Format: %s" % format)
if imgWidth is None: log(" Image Width: no resize")
else: log(" Image Width: %s" % imgWidth)
if zoomPercent is None: log(" Image Zoom: 100%")
else: log(" Image Zoom: %s" % zoomPercent)
log(" Greyscale: %s" % greyscale)
log("Channel Rendering Settings:")
for ch in img.getChannels():
log(" %s: %d-%d" % (ch.getLabel(), ch.getWindowStart(), ch.getWindowEnd()) )

savePlanesForImage(conn, img, sizeC, splitCs, mergedCs, channelNames,
zRange, tRange, greyscale, imgWidth, projectZ=projectZ, format=format, folder_name=folder_name)
zRange, tRange, greyscale, zoomPercent, projectZ=projectZ, format=format, folder_name=folder_name)

# write log for exported images (not needed for ome-tiff)
logFile = open(os.path.join(exp_dir, 'Batch_Image_Export.txt'), 'w')
Expand Down Expand Up @@ -434,6 +439,7 @@ def runScript():
tChoices = [rstring(defaultToption),
rstring('ALL T planes'),
rstring('Other (see below)')]
zoomPercents = omero.rtypes.wrap(["25%", "50%", "100%", "200%", "300%", "400%"])

client = scripts.client('Batch_Image_Export.py', """Save multiple images as jpegs or pngs in a zip
file available for download as a batch export.
Expand Down Expand Up @@ -480,9 +486,9 @@ def runScript():

scripts.Int("...specify_T_end", grouping="6.3",
description="Choose a specific T-index to export", min=1),
scripts.Int("Image_Width", grouping="7",
description="The max width of each image panel. Default is actual size", min=1),

scripts.String("Zoom", grouping="7", values=zoomPercents,
description="Zoom (jpeg, png or tiff) before saving with ANTIALIAS interpolation", default="100%"),

scripts.String("Format", grouping="8",
description="Format to save image", values=formats, default='JPEG'),
Expand Down
4 changes: 2 additions & 2 deletions omero/figure_scripts/ROI_Split_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def getROIsplitView (re, pixels, zStart, zEnd, splitIndexes, channelNames, me
roiImage.load() # hoping that when we zoom, don't zoom fullImage
if roiZoom is not 1:
newSize = (int(roiWidth*roiZoom), int(roiHeight*roiZoom))
roiImage = roiImage.resize(newSize)
roiImage = roiImage.resize(newSize, Image.ANTIALIAS)
renderedImages.append(roiImage)
panelWidth = roiImage.size[0]
re.setActive(index, False) # turn the channel off again!
Expand Down Expand Up @@ -202,7 +202,7 @@ def getROIsplitView (re, pixels, zStart, zEnd, splitIndexes, channelNames, me
roiMergedImage.load() # make sure this is not just a lazy copy of the full image
if roiZoom is not 1:
newSize = (int(roiWidth*roiZoom), int(roiHeight*roiZoom))
roiMergedImage = roiMergedImage.resize(newSize)
roiMergedImage = roiMergedImage.resize(newSize, Image.ANTIALIAS)

if channelMismatch:
log(" WARNING channel mismatch: The current image has fewer channels than the primary image.")
Expand Down