Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Photo effects
from PIL import Image
import numpy as np
import matplotlib.pyplot as pyplot
import pandas as pd
from datetime import datetime
import seaborn as sns
ini = datetime.now()
fle = "Cosmonaut" # File name
img = Image.open("C:\\Users\\Rober\\Desktop\\"+fle+".png") # Works only with png files
def conv(arr) : # convertion functions - treats the image array to be used in Seaborn scatteplot func.
mtz = pd.DataFrame()
XL = []
YL = []
SL = []
for y in range(0,len(arr)) :
for x in range(0,len(arr[0])) :
YL.append(y)
XL.append(x)
SL.append(arr[y][x])
mtz["Y"] = XL
mtz["X"] = YL
mtz["S"] = SL
return mtz
h = img.size[0]
v = img.size[1]
R,G,B = img.split()
s = 3 # size of the block(resolution) - smaller number equals to longer time processing
rsizeArr_R = np.asarray(R.resize((int(h/s),int(v/s)))) # Red channel
rsizeArr_G = np.asarray(G.resize((int(h/s),int(v/s)))) # Green channel
rsizeArr_B = np.asarray(B.resize((int(h/s),int(v/s)))) # Blue channel
r = 100 # proportion, [higher/lower] number equals to [smaller/larger] image
bkg = "black" # background color
pyplot.rcParams['figure.facecolor'] = bkg
pyplot.rcParams['savefig.facecolor'] = bkg
fig, g = pyplot.subplots(figsize=(h/r,v/r))
pyplot.subplots_adjust(top = 1.00, left = 0.00, right = 1.00, bottom = 0.00)
g.axis("off")
#Calling the conversion function
Arr_r = conv(rsizeArr_R)
Arr_b = conv(rsizeArr_B)
Arr_g = conv(rsizeArr_G)
# 3 color plotting block
sz = (1,9) # minimum and maximum size of the marker
sns.scatterplot(x="Y", y="X",alpha = 0.99,
hue="S", size="S",legend = False,
palette="Oranges_r", sizes=sz,
data=Arr_r, marker = "8", edgecolor=None)
sz = (1,6)
sns.scatterplot(x="Y", y="X",alpha = 0.60,
hue="S", size="S",legend = False,
palette="Blues_r", sizes=sz,
data=Arr_b, marker = "8", edgecolor=None)
sz = (0.5,4)
sns.scatterplot(x="Y", y="X",alpha = 0.15,
hue="S", size="S",legend = False,
palette="Greens_r", sizes=sz,
data=Arr_g, marker = "8", edgecolor=None)
# This block was set to reproduce more or less the original image, but it is a trial and error process.
# The first color (red channel) has a stronger size marker range and alpha (transparency), the others :
# the color after is plot on top of the previous one, so must be compensated.
#
# You can try other backgoung color, block size, palletes, sizes, markers, or even how many channels to
# plot - the imagination is the limit.
#
# For reasosn that I do not know, the marker 's' (square) "leakes" over the plot so I avoid it.
#
# Put a time counting that might be useful when you test different resolutions (block sizes).
pyplot.grid(False)
pyplot.ylim(int(v/s),-1)
pyplot.xlim(-1,int(h/s))
pyplot.show()
fim = datetime.now()
print(fim-ini)
@Especuloide

This comment has been minimized.

Copy link
Owner Author

commented Oct 31, 2018

Original image :
cosmonaut

Result :
figure_1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.