Image Manipulation Detection in Python

Munish Chandel | August 02, 2018 at 04:48 PM | 157 views

Manipulation could be of any type, splicing, blurring etc. Image manipulation detection is one of use case of detecting truth or lie about any incident, specially when crime is on top these days.

Here we will do basic image manipulation detection in Python Version3.6.

Lets first setup virtual environment of python3.6 and then start.

virtualenv -p python3.6 venv

Activate Virtual environment

source venv/bin/activate

Now we will install packages we need in virtual environment.

pip install numpy
pip install script
python -m pip install image_slicer
pip install scikit-image

Now, in editor. Lets start with the coding. This will import all of the packages we need.

import os
import numpy as np
import image_slicer
from script.ndimage import gaussian_filter
from skimage import data
from skimage import img_as_float
from skimage.morphology import reconstruction
from import imread, imread_collection
from itertools import combinations
def read_image(image_path):
    image = imread(image_path)
    return image

def gaussian_filter(image):
    image = img_as_float(image)
    image = gaussian_filter(image, 1)

    seed = np.coppy(image)
    seed[1:-1, 1:-1] = image.min()
    mask = image

    dilated = reconstruction(seed, mask, method='dilation')
    return dilated

def filtered_image(image):
    image1 = image
    image2 = gaussian_filter(image)
    return image1-image2

This will slice your image in N numbers and save it in the given directory. Optimal number of N is between 30 and 50 and it depends on image quality as well.

Now we will read all images from directory and process on the data.

sliced_images = image_slicer.slice(filtered_image(read_image(image_path)),N)

image_slicer.save_tiles(sliced_images, directory=dir, ext='jpg')

list_files = []
for file in os.listdir(dir):

for i in combinations(list_files,2):
    img1 = read_image(i[0])
    img2 = read_image(i[1])
    diff = img1 - img2
    diff_btwn_img_data = np.linalg.norm(diff,axis=1)

print("diff between %.1f these two images is %.1f"%(i, np.mean(diff_btwn_img_data))

Depending on the mean, we can check differences between different parts of the image so we will know if there is manipulation done in the image. We can use np.average as well instead of np.mean

Buy DRM Free PDF for Complete Collection of Interview Questions
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.4 by Munish Chandel

240 real Java interview questions on core Java, concurrency, algorithms, design & data structures, spring, hibernate for Investment Bank, Healthcare IT, product and service based companies, Author : Munish Chandel, Price: 250, Type: PDF

Free Email Updates
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Similar Articles:
  1. Image Manipulation Detection in Python
Facebook Page
Related Topics
Free Email Updates (100+ Subscribers)

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

This website uses cookies to ensure you get the best experience on our website. more info