Scale Bar Python Script - Zen Lite 3.7

Share your programming ideas for various measurement and analysis tasks here
Post Reply
Marcos Paulo Pereira Dias
Posts: 1
Joined: Sat Jun 10, 2023 6:00 pm

Scale Bar Python Script - Zen Lite 3.7

Post by Marcos Paulo Pereira Dias »

Scale Bar via Python Script

Hello!

At my workplace, we use a script to process images of petrographic samples. The script used to work perfectly fine, but after updating from ZEN version 3.7, the script stopped working. It successfully inserts the scale bar in the correct position on the image, but when the .JPG file is generated, the scale bar is placed in the default position (bottom right corner), and we need it to be in the top left corner. Please help us!

This is code:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<Script>
  <Context>Zen26</Context>
  <Version>1.0.0</Version>
  <Language>Python</Language>
  <Text>

import os
import time

win1=Zen.Windows.CreateWindow("Special batch marco:")
win1.AddFolderBrowser("infolder","Please Select Batch Folder")
win1.AddFolderBrowser("outfolder","Please Select Output Folder")
win1.AddCheckbox("addscale","Add standard scalebar?: ",True)
win1.AddTextBox("scalesize","Scalebar size / um: ","2000")
win1.AddCheckbox("save2jpg","Save to .jpg?: ",True)
win1.AddTextBox("downs_f","Downsample factor: ","2")
window=win1.Show()

foldertodo=window.GetValue("infolder")
outfolder=window.GetValue("outfolder")
save2jpg=window.GetValue("save2jpg")
addscale=window.GetValue("addscale")
scalesize=int(window.GetValue("scalesize"))
downs_f=int(window.GetValue("downs_f"))

print(scalesize)

os.chdir(foldertodo)
todolist=os.listdir(foldertodo)
todolist2=[]
for item in todolist:
    if item.endswith(".czi"):
        todolist2.append(item)
        print(item)
        

for item in todolist2:
    foldertodo=window.GetValue("infolder")
    outfolder=window.GetValue("outfolder")
    save2jpg=window.GetValue("save2jpg")
    addscale=window.GetValue("addscale")
    scalesize=int(window.GetValue("scalesize"))
    downs_f=int(window.GetValue("downs_f"))
    
    print(item)
    image1 = Zen.Application.LoadImage(os.path.join(foldertodo,item), False)
    Zen.Application.Documents.Add(image1)
    
    if save2jpg:
    
        image2 = Zen.Processing.Transformation.Geometric.SampleDown(image1, ZenThirdProcessingDimension.None, downs_f, downs_f, 1, False)
        Zen.Application.Documents.Add(image2)
        
    else:
        image2=image1
        Zen.Application.Documents.Add(image2)
    if addscale:
        scalebar1 = image2.InsertDefaultScaleBarGraphic()
        if save2jpg:
            scalebar1.Thickness = 25.
            scalebar1.FontSize= 120.
        else:
            scalebar1.Thickness = 5.
            scalebar1.FontSize= 24.
        xsize=int(image2.Metadata.SizePixels.split("x")[0])
        ysize=int(image2.Metadata.SizePixels.split("x")[1])
        
        pixsize=image2.Metadata.ScalingMicron.X
        #xsize=xsize*pixsize
        #ysize=ysize*pixsize
        
        imbounds=image2.Bounds
        
        if save2jpg:
            xbarpos = xsize - (xsize / 33) - scalesize
            ybarpos=ysize/10
            ybarsize=int(ysize/20)
            scalesize=scalesize/2
        else:
            xbarpos=xsize/10
            ybarpos=ysize/10
        
        scalebar1.SetBounds(xbarpos,ybarpos,scalesize,0)
        #scalebar1.Set
        
        
        #scalebar1.SetBounds(xbarpos,ybarpos,scalesize,ybarsize)
        #print(scalebar1.GetBounds())
        
        
    
    fnameb=os.path.splitext(item)[0]
    
    if save2jpg:
        
        image3=image2.BurnInGraphics()
        Zen.Application.Documents.Add(image3)
        Zen.Analyzing.ResetAnalysisResult(image3)
        processingsetting1 = Zen.Processing.Utilities.Settings.SingleFileExportSetting(ZenExportFileType.JPEG)
        processingsetting1.Quality = 95
        processingsetting1.ShiftPixel = True
        processingsetting1.DestinationFolder = outfolder
        processingsetting1.SubsetString = ''
        processingsetting1.MergedChannelsImage = False
        processingsetting1.IndividualChannelsImages = True
        processingsetting1.OriginalData = False
        processingsetting1.ApplyDisplayCurveAndChannelColor = True
        
        processingsetting1.BurnInGraphics = True
        processingsetting1.UseChannelNames = False
        processingsetting1.Resize = 1
        processingsetting1.GenerateXMLFile = False
        processingsetting1.CreateFolder = True
        processingsetting1.Tiles = ZenTileMode.Retile
        processingsetting1.Columns = 1
        processingsetting1.Rows = 1
        processingsetting1.Overlap = 0
        processingsetting1.GenerateZipFile = False
        processingsetting1.Prefix = fnameb
        Zen.Processing.Utilities.ExportSingleFile(image3, processingsetting1)
    
    else:
        image3=image2
        Zen.Application.Documents.Add(image3)
        time.sleep(1)
        Zen.Application.Save(image3,image1.Metadata.FileName,False)
    
    Zen.Application.Documents.Remove(image1,False)
    Zen.Application.Documents.Remove(image2,False)
    Zen.Application.Documents.Remove(image3,False)
    
    time.sleep(1)
    
    #Zen.Application.Documents.RemoveAll(False)
    
    
#for file in directorytorun:
    
</Text>
  <Author></Author>
  <Description></Description>
  <Keywords></Keywords>
  <Row>59</Row>
  <Column>26</Column>
</Script>
Thanks for Supporting!
Rose J Lever
Posts: 23
Joined: Tue Jun 20, 2023 10:20 pm

Re: Scale Bar Python Script - Zen Lite 3.7

Post by Rose J Lever »

To create a scale bar using Python and Zen Lite 3.7 software, you would typically need to access the Zen Lite software's API (Application Programming Interface) or SDK (Software Development Kit). Unfortunately, as an AI text-based model, I don't have access to the specific functions and capabilities of the Zen Lite software. If anyone want to know about Garage door spring repair shortpump
Elinor Pintar
Posts: 1
Joined: Thu Apr 04, 2024 6:31 am
Contact:

Re: Scale Bar Python Script - Zen Lite 3.7

Post by Elinor Pintar »

Replace the following lines in your code:

Code: Select all

xbarpos=xsize/10
ybarpos=ysize/10
With:

Code: Select all

xbarpos = xsize / 20  # Adjust as needed
ybarpos = ysize / 20  # Adjust as needed
These adjustments will position the scale bar closer to the top left corner. You may need to fine-tune the xbarpos and ybarpos values based on your specific requirements.
Post Reply