Advertisement
shubsbhatt

Code for eding speech bubbles on an image @NewsGraffiti

Oct 18th, 2015
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import random
  4. from twitterbot import TwitterBot
  5. import PIL
  6. from PIL import Image, ImageDraw
  7. from sys import argv
  8. import os
  9.  
  10.  
  11.  
  12. debug = True
  13. classifier = "haarcascade_frontalface_default.xml"
  14. faceCascade = cv2.CascadeClassifier(classifier)
  15.  
  16.  
  17. def face_detect(image):
  18.     """
  19.     Return rectangles of identified face regions
  20.     """
  21.  
  22.     # numpy grayscale image for face detection
  23.     array = np.asarray(image)
  24.     gray_image = cv2.cvtColor(array, cv2.COLOR_BGR2GRAY)
  25.  
  26.     # tweak this for better results ..
  27.  
  28.     faces = faceCascade.detectMultiScale(
  29.         gray_image,
  30.         scaleFactor=1.1,
  31.         minNeighbors=5,
  32.         minSize=(25, 25),
  33.         flags=cv2.cv.CV_HAAR_SCALE_IMAGE
  34.     )
  35.  
  36.    
  37.     # convert boxes from from arrays to tuples
  38.     boxes = [(x, y, x + w, y + h) for (x, y, w, h) in faces]
  39.     return boxes
  40.        
  41.  
  42. def face_mark(image):
  43.     """
  44.     Mark faces with boxes
  45.     """
  46.  
  47.     # work on a copy
  48.     image = image.copy()
  49.    
  50.     # identify boxes
  51.     boxes = face_detect(image)
  52.  
  53.     # get drawing context for the current image
  54.     ctx = ImageDraw.Draw(image)
  55.  
  56.     # define colors
  57.     black = (0, 0, 0, 255)
  58.     white = (255,255,255,255)
  59.  
  60.     # draw boxes
  61.     for box in boxes:
  62.  
  63.         # draw a black box
  64.         ctx.rectangle(box, fill=None, outline=black)
  65.  
  66.         # draw a white box around it
  67.         x1, y1, x2, y2 = box
  68.         box = x1 - 1, y1 - 1, x2 + 1, y2 + 1
  69.         ctx.rectangle(box, fill=None, outline=white)
  70.  
  71.     return image, boxes
  72.  
  73. def comic_element(image):
  74.     """
  75.     Pasting Speech Bubbles on the Image
  76.     """
  77.  
  78.     #working on a copy of the image
  79.     image = image.copy()
  80.  
  81.    
  82.     # face detection
  83.     boxes=face_detect(image)
  84.  
  85.     ctx = ImageDraw.Draw(image)
  86.  
  87.     # grabing random files of img from the folder of speech bubbles
  88.     directory = os.path.join(os.path.dirname(__file__), "bubbles")
  89.     directory_list = os.listdir('./bubbles/')
  90.  
  91.     # For MAC user - this is important step as iOS adds a hidden file called .DS
  92.     directory_list.remove('.DS_Store')
  93.    
  94.  
  95.  
  96.  
  97.     #finding the location for speach bubble and pasting it
  98.     for box in boxes:
  99.  
  100.        
  101.  
  102.         #cordinate for face box
  103.  
  104.         x1, y1, x2, y2 = box
  105.  
  106.         # taking a random choice from the list created before
  107.         one_bubble = random.choice(directory_list)
  108.         # adding the name of the file on the rute so it can be opend later...
  109.         imgfile = os.path.join(directory, one_bubble)
  110.  
  111.         # open the one_bubble img on the boxes
  112.         sBubble= Image.open(imgfile)
  113.  
  114.         #resize the img of the bubble
  115.         sBubble = sBubble.resize((160,160), resample=Image.ANTIALIAS)
  116.         xS, yS = sBubble.size
  117.         # Debugging  purpose
  118.         #sBubble.show()
  119.  
  120.  
  121.         # Finding the location for pasting the speechbubble
  122.        
  123.         # Centre Point coordinates
  124.  
  125.         cx = int((x2-x1)*0.5)
  126.         # This value is passed to define the box for pasting image and it only takes integer value.
  127.         cy = int((y2-y1)*0.5)  
  128.  
  129.         # Selecting either right or left side of the face for the bubble placement
  130.  
  131.         x_pos = random.choice((x1,x2))
  132.  
  133.         if x_pos == x1:
  134.             x_pos = x_pos - xS
  135.             y_pos =yS*(-1)
  136.         else:
  137.             y_pos =yS
  138.  
  139.         # Bubble box defines the size and place for pasting speech bubble on face
  140.         bubblebox = (x_pos,cy,x_pos+xS,cy+yS)
  141.  
  142.         image.paste(sBubble, bubblebox, sBubble)
  143.         #image.show()
  144.  
  145.     return image, boxes
  146.  
  147.  
  148.  
  149.  
  150. if __name__ == '__main__':
  151.  
  152.     """
  153.     Loading Image
  154.     """
  155.     src = Image.open("face1.jpg")
  156. # face detection
  157.     boxes=face_detect(src)
  158.     img,box = comic_element(src)
  159.     img.show()
  160.     img.save("test_img.jpg")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement