Skip to content

Instantly share code, notes, and snippets.

@fgiobergia
Created September 15, 2022 06:34
Show Gist options
  • Save fgiobergia/ae90ed5580aeeba16f14f903b9576cdf to your computer and use it in GitHub Desktop.
Save fgiobergia/ae90ed5580aeeba16f14f903b9576cdf to your computer and use it in GitHub Desktop.
"""
Code snippet to generate a scatter plot from a given image (mask).
"""
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
im = Image.open("image.png")
mask = (np.array(im) > 0).any(axis=2) # masking condition
np.random.seed(44)
ys, xs = np.nonzero(mask)
ndx = np.random.choice(len(xs), size=1500, replace=False)
plt.figure(figsize=(max(xs)//100, max(ys)//100))
plt.scatter(xs[ndx], -ys[ndx], s=10, c='#163473')
plt.axis("off")
plt.savefig("logo.svg", bbox_inches="tight")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment