image_commons.py 2.61 KB
Newer Older
tihmels's avatar
tihmels committed
1 2 3
# ***************************************************
# * Copyright (C) 2010-2011 Tjado Ihmels <tjado.ihmels@uni-oldenburg.de>
# *
tihmels's avatar
tihmels committed
4
# * This file is part of project mood.
tihmels's avatar
tihmels committed
5
# *
tihmels's avatar
tihmels committed
6
# * project mood can not be copied and/or distributed without the express
tihmels's avatar
tihmels committed
7 8 9 10 11 12
# * permission of Tjado Ihmels
# *
# * Parts of this program are legally copied and adapted from
# * van Gent, P. (2016). Emotion Recognition With Python, OpenCV and a Face Dataset. A tech blog about fun things with Python and embedded electronics. Retrieved from:
# * http://www.paulvangent.com/2016/04/01/emotion-recognition-with-python-opencv-and-a-face-dataset/
# ***************************************************/
13 14 15 16
import cv2
import numpy as np
from PIL import Image

tihmels's avatar
tihmels committed
17
# Utility Class for working with images in OpenCV
18 19 20

def image_as_nparray(image):
    """
tihmels's avatar
tihmels committed
21
    Convert PIL Image to Numpy array
22
    :param image: PIL's Image object.
tihmels's avatar
tihmels committed
23
    :return: Numpy Array
24 25 26 27 28 29
    """
    return np.asarray(image)


def nparray_as_image(nparray, mode='RGB'):
    """
tihmels's avatar
tihmels committed
30
    Convert numpy array to PIL image
31
    :param nparray: Numpy's array of image.
tihmels's avatar
tihmels committed
32 33
    :param mode: transformation mode
    :return: PIL image
34 35 36 37 38 39
    """
    return Image.fromarray(np.asarray(np.clip(nparray, 0, 255), dtype='uint8'), mode)


def load_image(source_path):
    """
tihmels's avatar
tihmels committed
40 41 42
    Load the RGB image and convert to grayscale
    :param source_path: source path to image
    :return: converted image
43 44 45 46 47
    """
    source_image = cv2.imread(source_path)
    return cv2.cvtColor(source_image, cv2.COLOR_BGR2GRAY)


tihmels's avatar
tihmels committed
48
def draw_alpha_3dim(source_image, image_to_draw, coordinates):
49
    """
tihmels's avatar
tihmels committed
50
    Draws a partially transparent, three dimensional image on another image
tihmels's avatar
tihmels committed
51 52 53
    :param source_image: Background image
    :param image_to_draw: Overdraw image
    :param coordinates: : Coordinates to which the image should be drawn
54 55 56 57 58
    """
    x, y, w, h = coordinates
    image_to_draw = image_to_draw.resize((h, w), Image.ANTIALIAS)
    image_array = image_as_nparray(image_to_draw)
    for c in range(0, 3):
tihmels's avatar
tihmels committed
59
        source_image[y:y + h, x:x + w, c] = image_array[:, :, c] * (image_array[:, :, 3] / 255.0) + source_image[y:y + h, x:x + w, c] * (1.0 - image_array[:, :, 3] / 255.0)
60

tihmels's avatar
tihmels committed
61
def draw_2dim(source_image, image_to_draw, coordinates):
62
    """
tihmels's avatar
tihmels committed
63
    Draws an two dimensional image to another image
tihmels's avatar
tihmels committed
64 65 66
    :param source_image: Background image
    :param image_to_draw: Overdraw image
    :param coordinates: : Coordinates to which the image should be drawn
67 68 69 70 71 72
    """
    x, y, w, h = coordinates
    image_to_draw = image_to_draw.resize((h, w), Image.ANTIALIAS)
    image_array = image_as_nparray(image_to_draw)
    for c in range(0, 3):
        source_image[y:y + h, x:x + w, c] = image_array[:, :]