Last active
August 29, 2015 14:02
-
-
Save CloudCray/dd618556fd0d73671140 to your computer and use it in GitHub Desktop.
Text Art
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
from PIL import Image, ImageDraw, ImageFont | |
from random import randint as rand | |
# Get the font we want from our OS | |
# create an image big enough to hold the text | |
# write on the image | |
img_font = ImageFont.truetype("courbd.ttf", 280) # Courier New, Bold; Windows 7 | |
im = Image.new("RGB", (2560, 640), "white") | |
img_draw = ImageDraw.Draw(im) | |
img_draw.text((0, 0), "Hello World", font=img_font, fill="black") | |
width, height = im.size | |
# We're going to go through each pixel to set the image boundaries | |
# if the pixel is not white (255, 255, 255), adjust the boundaries | |
max_x = 0 | |
max_y = 0 | |
min_x = width | |
min_y = height | |
pixels = im.load() | |
for x in range(width): | |
for y in range(height): | |
cpixel = pixels[x, y] | |
if cpixel != (255, 255, 255): | |
if x < min_x: | |
min_x = x | |
if x > max_x: | |
max_x = x | |
if y < min_y: | |
min_y = y | |
if y > max_y: | |
max_y = y | |
im_cropped = im.crop((min_x, min_y, max_x+1, max_y+1)) | |
# Create a function to draw circles on an image | |
def draw_ellipse(image, location, radius, c, line_width=1): | |
x, y = location | |
# image.ellipse((x-r, y-r, x+r, y+r), fill=c) # Uncomment for filled circles | |
for i in range(line_width): # There's no line width option, so draw concentric circles 1 pixel apart | |
r = radius + i - 1 | |
image.ellipse((x-r, y-r, x+r, y+r), outline=c) | |
colors = ( | |
(244, 11, 38), | |
(244, 51, 4), | |
(192, 2, 2), | |
(114, 0, 0), | |
(65, 0, 0), | |
) | |
# Pick your favorite color scheme | |
# https://kuler.adobe.com/Chrysanthemum-color-theme-3948781/edit/?copy=true | |
margin = 10 # Circles originating from pixels on the edge will hang outside | |
# Create a new blank Image and ImageDraw object to draw the circles | |
im_ellipses = Image.new("RGB", (max_x-min_x+margin*2, max_y-min_y+margin*2), "white") | |
im_draw_ellipse = ImageDraw.Draw(im_ellipses) | |
w, h = im_cropped.size | |
pixels = im_cropped.load() | |
for x in range(w): | |
for y in range(h): | |
cpixel = pixels[x, y] | |
denominator = 45 # 1 / denominator = likelihood of a circle forming. Higher number, less circles | |
if rand(1, denominator) == 1 and cpixel != (255, 255, 255): | |
draw_ellipse(im_draw_ellipse, | |
(x+margin, y+margin), | |
rand(1,3)*1.5, | |
colors[rand(0,4)], | |
rand(1,3.0)) | |
fn_ellipse = "text_test_circles.png" | |
im_ellipses.save(fn_ellipse, "PNG") | |
# http://i.imgur.com/MokbIHa.png | |
""" | |
# Display in IPython Notebooks | |
from IPython.display import display | |
from IPython.display import Image as IPyImage | |
display(IPyImage(filename=fn_ellipse)) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment