def save_image(self, grabResult): #Method for saving images #Set saved image title format: Time-Date.jpeg now = datetime.datetime.now() nowstr = now.strftime("%H%M%f-%m_%d_%y") print("Saving image...") ipo = pylon.ImagePersistenceOptions() ipo.SetQuality(100) filename = nowstr + ".jpeg" #Save image to current directory try: image = self.converter.Convert(grabResult) img = image.GetArray() cv2.imwrite(filename, img) grabResult.Release( ) #Release the grab result so that a new image can be grabbed print("Image successfully saved as", filename) except Exception as error: print("Error saving image:", error) return False return True
tlf = pylon.TlFactory.GetInstance() cam = pylon.InstantCamera(tlf.CreateFirstDevice()) cam.Open() cam.StartGrabbing() for i in range(num_img_to_save): with cam.RetrieveResult(2000) as result: # Calling AttachGrabResultBuffer creates another reference to the # grab result buffer. This prevents the buffer's reuse for grabbing. img.AttachGrabResultBuffer(result) if platform.system() == 'Windows': # The JPEG format that is used here supports adjusting the image # quality (100 -> best quality, 0 -> poor quality). ipo = pylon.ImagePersistenceOptions() quality = 90 - i * 10 ipo.SetQuality(quality) filename = "saved_pypylon_img_%d.jpeg" % quality img.Save(pylon.ImageFileFormat_Jpeg, filename, ipo) else: filename = "saved_pypylon_img_%d.png" % i img.Save(pylon.ImageFileFormat_Png, filename) # In order to make it possible to reuse the grab result for grabbing # again, we have to release the image (effectively emptying the # image object). img.Release() cam.StopGrabbing()