Last active
April 27, 2024 16:34
-
-
Save bgeron/9b6247cc97a6a63feb62161a8be83cc5 to your computer and use it in GitHub Desktop.
Converts an image to a scatter plot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
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
turn this data into a scatterplot graph