text_html_output_1.png
from PIL import Image, ImageDraw, ImageEnhance #for read the image

from tqdm import tqdm_notebook

import cv2 #for read the image

import os

import pandas as pd #dataframe manipulation

import seaborn as sns #for visualization

train = pd.read_csv('/kaggle/input/machinehack-watermark-challenge/train.csv')
test = pd.read_csv('/kaggle/input/machinehack-watermark-challenge/test.csv')

_=sns.countplot(x=train['Label'],order=train['Label'].value_counts().index);
_=plt.title("Target Label Distribution",fontsize=18)

def img_read(path, im, new_size=False):
    img = Image.open(f'{os.path.join(path, im)}')
    if new_size:
        img = img.resize((224, 224))
        return img
    else:
        return img

path="/kaggle/input/machinehack-watermark-challenge/train"
fig=plt.figure(figsize=(8, 15))
for i,label in enumerate(['Watermark','No Watermark']):    
    _=plt.subplot(1,2,i+1)
    img=img_read(path,train[train['Label']==label]['Image'].head(1).values[0])
    plt.imshow(img)
    plt.axis('off')
    plt.title(f"{'With Watermark' if label=='Watermark' else 'No Watermark'}")

def rgb_dist_plot(img,ax):
    start=0
    end=256
    for _,color in enumerate(['Red','Green','Blue']):
        _=sns.kdeplot(img.histogram()[start:end],label=color,color=color)
        _=plt.legend();
        start+=256
        end+=256

for label in ['Watermark','No Watermark']:
    fig, axs = plt.subplots(1, 2 ,figsize=(15,5))
    img_id=train[train['Label']==label].head(1)['Image'].values[0]
    img_file =Image.open(f"/kaggle/input/machinehack-watermark-challenge/train/{img_id}")
    axs[0].imshow(img_file)
    axs[0].axis('off')
    axs[0].set_title(img_id,fontsize=18)
    _=rgb_dist_plot(img_file,ax=axs[1])
    axs[1].set_title("RGB Color Distribution For "+img_id,fontsize=18)

def basic_image_info(df, path):
    image_name = []
    img_mode = []
    img_height = []
    img_width = []
    img_contrast = []
    for file in tqdm_notebook(df['Image']):
        image_name.append(file)
        img = Image.open(f'{os.path.join(path, file)}')
        grey_img = cv2.imread(f'{os.path.join(path, file)}', cv2.COLOR_BGR2GRAY)
        img_mode.append(img.mode)
        img_width.append(img.width)
        img_height.append(img.height)
        img_contrast.append(grey_img.std())
    return pd.DataFrame({'image_name': image_name, 'img_mode': img_mode, 'img_contrast': img_contrast, 'img_width': img_width, 'img_height': img_height})

train_image_basic_info=basic_image_info(train,
                                        "/kaggle/input/machinehack-watermark-challenge/train")

path="/kaggle/input/machinehack-watermark-challenge/train"
fig=plt.figure(figsize=(8, 15))
for i,img in enumerate(train_image_basic_info[train_image_basic_info['img_contrast']<15]['image_name'].values):    
    _=plt.subplot(5,2,i+1)
    img_file=img_read(path,img)
    plt.imshow(img_file)
    plt.axis('off')
    plt.title(f"{'With Watermark' if train[train['Image']==img]['Label'].values=='Watermark' else 'No Watermark'}")

for img in train_image_basic_info[train_image_basic_info['img_contrast'] < 15]['image_name'][:10].values:
    fig, axs = plt.subplots(1, 2, figsize=(15, 5))
    img_file = Image.open(f'/kaggle/input/machinehack-watermark-challenge/train/{img}')
    axs[0].imshow(img_file)
    axs[0].axis('off')
    axs[0].set_title(img_id, fontsize=18)
    _ = rgb_dist_plot(img_file, ax=axs[1])
    axs[1].set_title('RGB Color Distribution For ' + img_id, fontsize=18)