1. Ethan Sherbondy
  2. albums

Commits

Ethan Sherbondy  committed 2ca75ba Merge

Added notifier of which image is being generated.

  • Participants
  • Parent commits 8504418, 2bad9d6
  • Branches default

Comments (0)

Files changed (2)

File TextDraw.py

View file
  • Ignore whitespace
 import Image, ImageDraw, math
 
+def calcRotation(x, y, width, height, angle):
+    """Recalculate the top-left corner's coordinate for a rotated box"""
+    angle %= 360
+    if 0 <= angle <= 90:
+        y += -width * math.sin(math.radians(angle))
+    elif 90 <= angle < 180:
+        y += -width * math.sin(math.radians(angle)) + height * math.cos(math.radians(angle))
+        x += width * math.cos(math.radians(angle))
+    elif 180 <= angle < 270:
+        x += height * math.sin(math.radians(angle)) + width * math.cos(math.radians(angle))
+        y += height * math.cos(math.radians(angle))
+    else:
+        x += height * math.sin(math.radians(angle))
+    return (x, y)
+
 class Drawer:
     def __init__(self, im):
         self.image = im
         draw = ImageDraw.Draw(self.image)
 
         # Recalculate (x, y), the top-left corner of the rotated box
-        angle %= 360
-        if 0 <= angle <= 90:
-            y += -width * math.sin(math.radians(angle))
-        elif 90 <= angle < 180:
-            y += -width * math.sin(math.radians(angle)) + height * math.cos(math.radians(angle))
-            x += width * math.cos(math.radians(angle))
-        elif 180 <= angle < 270:
-            x += height * math.sin(math.radians(angle)) + width * math.cos(math.radians(angle))
-            y += height * math.cos(math.radians(angle))
-        else:
-            x += height * math.sin(math.radians(angle))
+        x, y = calcRotation(x, y, width, height, angle)
         draw.bitmap((x,y),rotated_box,fill=fill) # put the rotated text on the image

File albums.py

View file
  • Ignore whitespace
-import Image, ImageFont, TextDraw, requests, os, sys, urllib
+import Image, ImageFont, ImageStat, os, sys, urllib
+import TextDraw, requests
 from random import randint
 
 success = 0;
 fonts = []
 
 class Orientation:
-    def __init__(self, xywh1, xywh2, angle1=0, angle2=0, just='L'):
+    def __init__(self, xywh1, xywh2, angle1=0, angle2=0, justify='L'):
         self.xywh1 = xywh1
         self.xywh2 = xywh2
         self.angle1 = angle1
         self.angle2 = angle2
-        self.just = just
+        self.justify = justify
     def getBandNamePos(self):
         return (self.xywh1, self.angle1)
     def getTitlePos(self):
     if len(fonts) == 0:
         return None
     name = fonts[randint(0, len(fonts)-1)]
-    return ImageFont.truetype(name, size1), ImageFont.truetype(name, size2)
+    return ImageFont.truetype(name, size1, encoding='unic'), ImageFont.truetype(name, size2, encoding='unic')
 
-
-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 calculateColor(im, xywh, angle):
+    """Return a calculated color according to pixel levels in the specified box"""
+    x, y, w, h = xywh
+    tempbox = Image.new('1', (w,h)).rotate(angle)
+    x, y = TextDraw.calcRotation(x, y, w, h, angle)
+    w, h = tempbox.size
+    box = im.crop((int(x),int(y),int(x)+w,int(y)+h))
+    if im.mode != 'RGB':
+        return (255, 255, 255)
+    m = ImageStat.Stat(box).median
+    color = [255, 255, 255]
+    for i in range(0,3):
+        if m[i] >= 180:
+            color[i] = 0
+        elif m[i] >= 120: # and < 180
+            color[i] = 200
+    return tuple(color)
 
 
 def createAlbum(arg):
 
     drawer = TextDraw.Drawer(im)
     orient = randOrientation()
-    font1, font2 = randFonts(44,26)
     xywh1, angle1 = orient.getBandNamePos()
     xywh2, angle2 = orient.getTitlePos()
 
-    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]))
+    fill1 = calculateColor(im, xywh1, angle1)
+    fill2 = calculateColor(im, xywh2, angle2)
+    font1, font2 = randFonts(44,26)
+    drawer.draw(xywh1, wiki, angle1, font1, fill1, orient.justify)
+    drawer.draw(xywh2, quote, angle2, font2, fill2, orient.justify)
 
-    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():
     if not prepare():
         return
     while True:
-        prompt = raw_input("Enter number of albums to generate, or 'q' to quit...")
+        prompt = raw_input("Enter number of albums to generate, or 'q' to quit... ")
         if (prompt == 'q'):
             break
         if not prompt.isdigit():