#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
###############################################################################
#
#  Name:         opencv_basic_operations_1
#  Purpose:      Basic operations on images with OpenCV
#  Author:       weigu.lu
#  Date:         2020-11-22
#
#  Copyright 2020 weigu <weigu@weigu.lu>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#  More infos on weigu.lu/other_projects
#
###############################################################################
"""

import cv2 as cv # to run code even if version changes
import numpy as np

IMG_NAME = 'test.jpg'

def show_image(pimg, show_flag_time):
    ''' Show image during x ms if flag is set. Parameter show_flag_time is a
        tuple e.g (1,2000) to show picture for 2s or (0,2000) to prevent the
        show. (1,0) waits on keypress '''
    if show_flag_time[0] == 1:
        cv.imshow('image', pimg)      # cv.imshow(window_name, image)
        cv.waitKey(show_flag_time[1]) # show picture for x ms (x=0 for keypress)
        cv.destroyAllWindows()

# flags and times in ms to show images
flag = {'short':(1, 500), 'medium':(1, 1000), 'long':(1, 3000)}

img = cv.imread(IMG_NAME)                        # read the image
show_image(img, flag['medium'])
print(img.shape, img.dtype)                      # shape returns height,width,channels
height, width = img.shape[:2]
print("height x width = ", height, 'x', width)

img_pixel = img[0:2, 0:2]
show_image(img_pixel, flag['medium'])
print(img_pixel)                                 # print 4 pixel (2x2)

RATIO = float(40/100)
r_img = cv.resize(img, (0, 0), fx=RATIO, fy=RATIO)
show_image(r_img, flag['medium'])
cv.imwrite(IMG_NAME[0:IMG_NAME.find('.')] + '_40p.jpg', r_img)   # write r_img
print("height x width = ", r_img.shape[0], 'x', r_img.shape[1])

imgc = r_img.copy()                              # copy of an image
show_image(imgc, flag['medium'])

img2 = img[100:1200, 900:1750]                   # new image (img2) crop from img
show_image(img2, flag['medium'])
img2[50:150, 50:150] = [0, 0, 255]               # BGR: set pixels to red
show_image(img2, flag['medium'])
img2[600:1000, 400:800] = img[200:600, 1200:1600]# copy an image part to another image
show_image(img2, flag['medium'])

img3_list = [[[0, 0, 255], [255, 0, 0]], [[0, 0, 0], [255, 255, 255]]]
img3 = np.asarray(img3_list, dtype=np.uint8)     # create image (np.array) from list
show_image(img3, flag['medium'])
img4 = cv.cvtColor(img3, cv.COLOR_BGR2RGB)       # OpenCV uses BGR
show_image(img4, flag['medium'])
img5 = cv.cvtColor(img3, cv.COLOR_BGR2GRAY)      # OpenCV uses BGR
show_image(img5, flag['medium'])
img6 = np.zeros([300, 300, 3], dtype=np.uint8)   # create 512*512 black image
img6.fill(255)                                   # change to white
show_image(img6, flag['medium'])

height, width = img6.shape[:2]
cv.line(img6, (10, 10), (290, 290), (255, 0, 0), 8)                 # draw line
show_image(img6, flag['medium'])
cv.rectangle(img6, (50, 50), (250, 250), (255, 0, 255), 4)          # draw rect.
show_image(img6, flag['medium'])
cv.circle(img6, (int(height/2), int(width/2)), 120, (0, 0, 255), 2) # circle
show_image(img6, flag['medium'])
