Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from PIL import Image
"""
# if you don't have the image and you do have 'requests', uncomment temporarily
import requests
GRAPH_URL = 'http://www.spotifyartists.com/site/wp-content/uploads/2013/09/First-Chart.png'
open('First-Chart.png', 'wb').write(requests.get(GRAPH_URL).content)
"""
PHYSICAL_COLOR = (185, 185, 185)
DOWNLOAD_COLOR = (119, 119, 118)
GRAPH_START_X = 150 # graph starts around 150px from the left
GRAPH_START_Y = 90 # graph starts around 95px from the bottom
NUM_YEARS_DEPICTED = 15.0
INDUSTRY_PEAK = 28.0 # about $28B at peak of industry by my eye
image = Image.open("First-Chart.png")
pixels = image.load()
# raw pixel heights for each
physical_heights = []
download_heights = []
width, height = image.size
for col in range(GRAPH_START_X, width):
min_physical, min_download = height, height
max_physical, max_download = 0, 0
for row in range(height - GRAPH_START_Y):
color = pixels[col, row]
if color == PHYSICAL_COLOR:
min_physical = min(min_physical, row)
max_physical = max(max_physical, row)
elif color == DOWNLOAD_COLOR:
min_download = min(min_download, row)
max_download = max(max_download, row)
physical = max(max_physical - min_physical, 0)
download = max(max_download - min_download, 0)
physical_heights.append(physical)
download_heights.append(download)
both_heights = zip(physical_heights, download_heights)
both_heights = [hs for hs in both_heights if hs[0] != 0 or hs[1] != 0]
phys_only = [p for (p,d) in both_heights]
dl_only = [d for (p,d) in both_heights]
best_total = max(x+y for (x,y) in both_heights)
px_per_billion = best_total / INDUSTRY_PEAK
px_per_year = len(both_heights) / NUM_YEARS_DEPICTED
print "~%.2f vertical pixels per $billion" % px_per_billion
print "~%.2f horizontal pixels per year" % px_per_year
print
def trend(data, lookback):
# returns str representation of trend in billions per year
vpx_per_hpx = (data[-1] - data[-int(lookback-1)])/float(int(lookback))
b = vpx_per_hpx*px_per_year/px_per_billion
sign = '-' if b < 0 else '+'
return "%s$%.1fB/yr" % (sign, abs(b))
print "physical trends:"
for y in (1,3,5):
print trend(phys_only, y*px_per_year), "(%syear)" % y
print
print "download trends:"
for y in (1,3,5):
print trend(dl_only, y*px_per_year), "(%syear)" % y
print
print "last physical dollars per year: $%.2fB" % (phys_only[-1]/px_per_billion)
print "last download dollars per year: $%.2fB" % (dl_only[-1]/px_per_billion)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment