# Generated by pandoc-plot 1.8.0
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), (ax3, ax4)) = plt.subplots(2, 2, figsize=(6,6))

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

    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)
    im = im[rs, cs]

    ax.imshow(im, vmin=0, vmax=200, cmap='inferno')
    ax_r.imshow(im[::-1, ::-1], vmin=0, vmax=200, cmap='inferno')
    ax.axis('off')
    ax_r.axis('off')

plt.tight_layout()
Click here to see how this plot was generated.