Skip to content

Instantly share code, notes, and snippets.

@agness
Created July 7, 2023 06:34
Show Gist options
  • Save agness/d85cf2655567c0c331ff90c573dcbdf2 to your computer and use it in GitHub Desktop.
Save agness/d85cf2655567c0c331ff90c573dcbdf2 to your computer and use it in GitHub Desktop.

Revisions

  1. agness created this gist Jul 7, 2023.
    31 changes: 31 additions & 0 deletions union_bitmaps.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #!/usr/bin/env python3

    # Combines an array of image files into one bitmap with union (logical OR).

    # Requirements
    # pip3 install opencv-python

    import cv2
    import numpy as np

    infiles = [
    'colour-7.png',
    'colour-8.png',
    'colour-9.png'
    ]
    outfile = 'colour-789.png'
    o = None

    for i in infiles:
    im = cv2.imread(i,cv2.IMREAD_GRAYSCALE) # load image
    p = np.array(im) >= 255 # binarize
    print(f'Opening file {i}:')
    print(p)
    if (o is None):
    o = p
    else:
    o = p + o # Logical OR

    # Save to outfile.
    cv2.imwrite(outfile, o * 255)
    print(f'Wrote file {outfile}.')