Commits

Ethan Sherbondy committed 8504418

Now smartly decides if text should be black or white based on average colors and the w3c contrast comparison.

  • Participants
  • Parent commits 88f6c2d

Comments (0)

Files changed (2)

 
 *.pyc
 .DS_Store
-images/*
+./images/*
+./raw/*
+./output/*
 
 def randOrientation():
     """Return a random Orientaion instance"""
-    xywh1 = [(20,20,460,120), (150,380,330,100), (20,480,460,120)]
-    xywh2 = [(20,140,200,200), (300,230,180,150), (140,480,200,200)]
+    xywh1 = [(20,20,460,120), (100,360,380,120), (20,480,460,120)]
+    xywh2 = [(20,140,200,200), (300,210,180,150), (140,480,200,200)]
     angle1 = [0, 0, 90]
     angle2 = [0, 0, 90]
     just = ['L', 'R', 'L']
     name = fonts[randint(0, len(fonts)-1)]
     return ImageFont.truetype(name, size1), ImageFont.truetype(name, size2)
 
+
+def getBrightness(color):
+    return (color[0]*299 + color[1]*587 + color[2]*114)/1000
+
+
+def colorsPass(text, bg):
+    """ Try to ensure good color contrast using this formula:
+        http://www.w3.org/TR/AERT#color-contrast. Defaults to white
+        text. If contrast test fails, switch to black text. """
+     # establish brightness difference. If less than 125, poor contrast
+    bright_diff = abs(getBrightness(text) - getBrightness(bg))
+    color_diff = sum([max(text[i],bg[i]) - min(text[i],bg[i]) for i in range(0,3)])
+    if color_diff > 500 and bright_diff > 125:
+        return text
+    else:
+        return tuple([abs(text[i]-255) for i in range(0,3)])
+
+
 def createAlbum(arg):
     """Use the given arg=(imurl, wiki, quote) to generate an album cover"""
     imurl, wiki, quote = arg
     urllib.urlretrieve(imurl, './raw/'+str(success+1)+'.jpg')
-    im = cropImage(Image.open('./raw/'+str(success+1)+'.jpg'))
+    image = Image.open('./raw/'+str(success+1)+'.jpg')
+    im = cropImage(image)
+
     drawer = TextDraw.Drawer(im)
     orient = randOrientation()
     font1, font2 = randFonts(44,26)
     xywh1, angle1 = orient.getBandNamePos()
     xywh2, angle2 = orient.getTitlePos()
-    drawer.draw(xywh1, wiki, angle1, font1, justify=orient.just)
-    drawer.draw(xywh2, quote, angle2, font2, justify=orient.just)
+
+    text_bg1 =  im.crop((xywh1[0], xywh1[1], xywh1[0]+xywh1[2], xywh1[1]+xywh1[3]))
+    text_bg2 =  im.crop((xywh2[0], xywh2[1], xywh2[0]+xywh2[2], xywh2[1]+xywh2[3]))
+
+    white = (255,255,255)    
+
+    color1 = text_bg1.resize((1,1), Image.ANTIALIAS).getpixel((0,0))
+    color2 = text_bg2.resize((1,1), Image.ANTIALIAS).getpixel((0,0))
+    text_colors = [colorsPass(white, color1), colorsPass(white,color2)]
+
+    print text_colors
+
+    drawer.draw(xywh1, wiki, angle1, font1, justify=orient.just, fill=text_colors[0])
+    drawer.draw(xywh2, quote, angle2, font2, justify=orient.just, fill=text_colors[1])
     im.save('./output/'+str(success+1)+'.jpg')
 
 def main():
         n = int(prompt)
 
         for i in range(success, success + n):
+            print "Generating image {0}".format(success+1) 
             if len(results) == 0:
                 results = requests.getResults()
             createAlbum(results.pop())