Skip to content

Instantly share code, notes, and snippets.

@daguiam
Created March 21, 2025 16:14
Show Gist options
  • Save daguiam/6d355f316e55631dbaa9ad5fbada2154 to your computer and use it in GitHub Desktop.
Save daguiam/6d355f316e55631dbaa9ad5fbada2154 to your computer and use it in GitHub Desktop.
from PIL import Image
Image.MAX_IMAGE_PIXELS = 933120000
def join_masks_with_template(filename_mask, filename_template, filename_output, center=True):
from PIL import Image
img = Image.open(filename_mask)
background = Image.open(filename_template)
bg_size = background.size
Image.MAX_IMAGE_PIXELS = 10*bg_size[0]*bg_size[1]
mask_size = img.size
corner = (int((bg_size[0] - mask_size[0])/2),int( (bg_size[1] - mask_size[1])/2))
print("Template size is ",bg_size, ". Mask size is ",mask_size,".")
if center:
print("Centering the mask. Corner is ",corner)
else:
corner = (0,0)
background.paste(img, corner, img)
# Force conversion to grayscale
background = background.convert('L')
print("Saving to ", filename_output)
background.save(filename_output)
return background
filename_template = 'gs_bmp_fiducials_10x10mm_1um.png'
filename_mask = 'mask_data.png'
filename_output = "mask_with_fiducials_10x10_8bit.bmp"
image = join_masks_with_template(filename_mask, filename_template, filename_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment