# Generated by pandoc-plot 1.9.1
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(2019)

import numpy as np
import matplotlib.pyplot as plt
from skued import diffread
from pathlib import Path
from math import floor

def center_of_mass_masked(im, mask):
    rr, cc = np.indices(im.shape)
    weights = im * mask.astype(im.dtype)

    r = np.average(rr, weights=weights)
    c = np.average(cc, weights=weights)
    return int(r), int(c)

im1 = diffread(Path("images") / "autocenter" / "graphite.tif")
mask1 = diffread(Path("images") / "autocenter" / "graphite_mask.tif").astype(bool)

im2 = diffread(Path("images") / "mnxc" / "Cr_1.tif")
mask2 = np.ones_like(im2, dtype=bool)
mask2[0:1250, 950:1250] = False

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6,3))

for ax, im, mask in zip([ax1, ax2], [im1, im2], [mask1, mask2]):

    placeholder = np.full_like(im, fill_value=im.max())

    r, c = center_of_mass_masked(im, mask)
    side_length = floor(min([r, abs(r - im.shape[0]), c, abs(c - im.shape[1])]))
    rs = slice(r - side_length, r + side_length)
    cs = slice(c - side_length, c + side_length)
    placeholder[rs, cs] = im[rs, cs]

    ax.imshow(placeholder, vmin=0, vmax=200, cmap='inferno')
    ax.axis('off')
Click here to see how this plot was generated.