Skip to content

Instantly share code, notes, and snippets.

@jwinterm
Created December 11, 2017 03:30
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 jwinterm/0cdd7ebf2b1dbf1b688492c65c88e657 to your computer and use it in GitHub Desktop.
Save jwinterm/0cdd7ebf2b1dbf1b688492c65c88e657 to your computer and use it in GitHub Desktop.
Image gen for GRS ticker
# -*- coding: utf-8 -*-
from PIL import Image, ImageChops, ImageFont, ImageDraw
import cmc
import os
def trim(im):
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
diff = ImageChops.add(diff, diff, 2.0, -100)
bbox = diff.getbbox()
print(bbox)
if bbox:
# return im.crop(bbox)
return im.crop((bbox[0], 0, bbox[2], 36))
def imggen():
cmcdata = cmc.getcmc()
font = ImageFont.truetype("Ubuntu-B.ttf",16)
tickerimg = Image.new("RGBA", (400,36), (0,0,0,0))
draw = ImageDraw.Draw(tickerimg)
draw.text((0,0), "Rank", (0,0,0), font=font)
draw.text((50,0), "Marketcap", (0,0,0), font=font)
draw.text((140,0), "Price(USD)", (0,0,0), font=font)
draw.text((230,0), "Price(BTC)", (0,0,0), font=font)
draw.text((0,18), "#"+cmcdata[0]['rank'], (75,75,75), font=font)
draw.text((50,18), "${:.2f}M".format(float(cmcdata[0]['market_cap_usd'])/1e6), (75,75,75), font=font)
draw.text((140,18), "${:.2f}".format(float(cmcdata[0]['price_usd'])), (75,75,75), font=font)
draw.text((230,18), "{:.8f}".format(float(cmcdata[0]['price_btc'])), (75,75,75), font=font)
tickerimg = trim(tickerimg)
tickerimg.save("img/temp/ticker.png")
print("Ticker image size = {}".format(t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment