Skip to content

Instantly share code, notes, and snippets.

@bgeron
Last active April 27, 2024 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bgeron/9b6247cc97a6a63feb62161a8be83cc5 to your computer and use it in GitHub Desktop.
Save bgeron/9b6247cc97a6a63feb62161a8be83cc5 to your computer and use it in GitHub Desktop.
Converts an image to a scatter plot.
# CC-BY 2.0 by bgeron
from PIL import Image; import matplotlib.pyplot as plt, pandas as pd
a = Image.open("source.png")
b = Image.blend(a, Image.new(a.mode, a.size, "white"), .92).convert(mode="1")
df = pd.DataFrame(((x,-y) for x in range(b.width) for y in range(b.height) if not b.getpixel((x,y))), columns=("x","y"))
# Plot
df.plot.scatter(x=0, y=1, s=5, alpha=.5, c=["#111133"], figsize=(16,16)).set_aspect("equal")
# Save to CSV
df.to_csv(open("out.csv", "w"), index=False)
@msms5678
Copy link

turn this data into a scatterplot graph

@bgeron
Copy link
Author

bgeron commented Feb 26, 2024

turn this data into a scatterplot graph

The script does something else: it converts an image to a table that will look like the image when scatter-plotted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment