Created
June 17, 2025 10:56
-
-
Save AnthonyZJiang/b0b91db46ac3fcefd311d12139668fef to your computer and use it in GitHub Desktop.
spinnaker gain value retrieval
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PySpin | |
NUM_IMAGES = 5 | |
class GainNodeCallback(PySpin.NodeCallback): | |
def __init__(self): | |
super(GainNodeCallback, self).__init__() | |
def CallbackFunction(self, node): | |
node_gain = PySpin.CFloatPtr(node) | |
print('>>>>>>>From callback:Gain changed to %f...' % node_gain.GetValue()) | |
def register_gain_callback(nodemap): | |
node_gain = PySpin.CFloatPtr(nodemap.GetNode('Gain')) | |
callback_gain = GainNodeCallback() | |
PySpin.RegisterNodeCallback(node_gain.GetNode(), callback_gain) | |
print('Gain callback registered...') | |
return callback_gain | |
def deregister_gain_callback(callback_gain): | |
PySpin.DeregisterNodeCallback(callback_gain) | |
print('Callbacks deregistered...') | |
def acquire_images(cam): | |
try: | |
processor = PySpin.ImageProcessor() | |
processor.SetColorProcessing(PySpin.SPINNAKER_COLOR_PROCESSING_ALGORITHM_HQ_LINEAR) | |
for i in range(NUM_IMAGES): | |
try: | |
image_result = cam.GetNextImage(1000*1000*5) | |
if image_result.IsIncomplete(): | |
print('Image incomplete with image status %d...' % image_result.GetImageStatus()) | |
else: | |
print(f"Grabbed Image {i} with gain: {cam.Gain.GetValue()}") | |
image_converted = processor.Convert(image_result, PySpin.PixelFormat_Mono8) | |
image_converted.Save('Gain-%d.jpg' % (i)) | |
image_result.Release() | |
except PySpin.SpinnakerException as ex: | |
print('Error: %s' % ex) | |
except PySpin.SpinnakerException as ex: | |
print('Error: %s' % ex) | |
def main(): | |
system = PySpin.System.GetInstance() | |
version = system.GetLibraryVersion() | |
print('Library version: %d.%d.%d.%d' % (version.major, version.minor, version.type, version.build)) | |
cam_list = system.GetCameras() | |
cam = cam_list[0] | |
try: | |
cam.Init() | |
nodemap = cam.GetNodeMap() | |
cam.GainAuto.SetValue(PySpin.GainAuto_Off) | |
callback_gain = register_gain_callback(nodemap) | |
cam.Gain.SetValue(0.0) | |
print("Gain set to 0.0 manually") | |
cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous) | |
print("Acquisition mode set to continuous") | |
cam.BeginAcquisition() | |
print("Acquisition started") | |
cam.GainAuto.SetValue(PySpin.GainAuto_Continuous) | |
print("Gain auto set to continuous") | |
acquire_images(cam) | |
cam.EndAcquisition() | |
deregister_gain_callback(callback_gain) | |
cam.DeInit() | |
except PySpin.SpinnakerException as ex: | |
print('Error: %s' % ex) | |
finally: | |
del cam | |
cam_list.Clear() | |
system.ReleaseInstance() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment