5.8 Miscellaneous

This folder contains examples related to folder management, color management in Origin with originpro package.

Export Graphs as Images from Sample Projects

'''
This sample generate an image from a graph from a sample project. 
Please note that a file named my_py_test.png will be replaced by running this sample.
'''
import originpro as op

#from Learning Center folder
op.open(op.path('c')+ r'\Graphing\Multi-Panel Graphs - Trellis Box Plot.opju')

gg=op.find_graph(0)

# export graph to an image
f=''
if gg:
    f=gg.save_fig(op.path()+'my_py_test.png',width=500)

import os
if len(f):
    os.startfile(f)

Extract Image Colors

'''
This sample picks all colors from an image. 
The RGB values for each color is output to a worksheet. 
This example requires Python package extcolors to be preinstalled.
'''
import extcolors
import numpy as np
import originpro as op

# choose an image to extract colors
file_path = op.file_dialog('*.png;*.jpg;*.bmp','Select an Image')
if len(file_path) ==0:
    raise ValueError('user cancel')
    
colors = extcolors.extract_from_path(file_path)

# rgb = colors[:,0]
rgb = map(list, zip(*colors))

# print(rgb)
r = []
g = []
b = []
for row in rgb:
    r.append(row[0])
    g.append(row[1])
    b.append(row[2])

# output colors
wks = op.new_sheet()
wks.from_list(0, r)
wks.from_list(1, g)
wks.from_list(2, b)

Loop through worksheets

'''
This sample loops through all the worksheets in the project,
print out the number of rows of each sheet.
'''
import originpro as op

for wb in op.pages('w'):
    for wks in wb:
        print(f'Worksheet {wks.lt_range()} has {wks.rows} rows')

Manage Project Explorer

'''
This sample shows the management of folders under Project Explorer. It sets up same
folder structure as the /Sample/Python/ and create a workbook under each sub folder. 
'''
import os
import originpro as op

# Get the path string of a folder
path = os.path.join(op.path('e'), 'Samples', 'Python')

# Start a new project and go to the root folder in Project Explorer
op.new() 
op.pe.cd('/UNTITLED')

# Loop over subfolders under /Samples/Python/ and create subfolders with same name.
for f in os.listdir(path):
    fd = f'{os.path.splitext(f)[0]}'
    op.pe.mkdir(fd)
    op.pe.cd(f'"{fd}"')
    op.new_sheet('w',fd)
    op.pe.cd('/UNTITLED')

Multi Threading

import threading
from datetime import datetime
from time import sleep
import originpro as op

#critical section to global data
def write(tt, name):
    global v1, v2
    v1.append(name)
    v2.append(tt)
    
#must not do print or anything related to Origin inside the thread function
def thread_task(lock, name):
    for _ in range(5):
        sleep(0.5)
        now = datetime.now()
        tt = now.strftime("%H:%M:%S.%f")[:-3]
        lock.acquire()
        write(tt, name)
        lock.release()

#global variables to be used inside thread task
v1=[]
v2=[]
#we need to have criticlal section for any access to shared data
lock = threading.Lock()

t1 = threading.Thread(target=thread_task, args=(lock,'a'))
t2 = threading.Thread(target=thread_task, args=(lock,'b'))
t1.start()
t2.start()
t1.join()
t2.join()

#threads done, we can put data to a worksheet
wks=op.new_sheet()
#need to set col(2) as time format before putting data into it
wks.cols = 2
wks.as_time(1, 10)

wks.from_list(0, v1, 'Name')
wks.from_list(1, v2, 'Time')
#has to use LabTalk for setting column width
wks.lt_exec("wks.col2.width=9")