Image Manipulation Detection in Python

Upasana | May 05, 2019 | 2 min read | 845 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 scipy numpy image_slicer 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 scipy.ndimage import gaussian_filter
from skimage import io
from skimage import img_as_float
from skimage.morphology import reconstruction
from skimage.io import imread
from itertools import combinations
def read_image(image_path):
    image = imread(image_path)
    return image


def gaussian_filter1(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_filter1(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, save=False)

image_slicer.save_tiles(sliced_images, directory=dir, prefix='slice')

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

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 " + str(i) + " two images is " + str(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

Now, the whole code looks something like below:

import os
import numpy as np
import image_slicer
from scipy.ndimage import gaussian_filter
from skimage import io
from skimage import img_as_float
from skimage.morphology import reconstruction
from skimage.io import imread
from itertools import combinations

image_path = "sample-image.png"
N = 12 # number of slices
dir = "./data"

def read_image(image_path):
    image = imread(image_path)
    return image

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

    seed = np.copy(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_filter1(image)
    img3 =  image1-image2
    io.imsave("out.png", img3)
    return "out.png"

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

image_slicer.save_tiles(sliced_images, directory=dir, prefix='slice')

list_files = []
for file in os.listdir(dir):
    list_files.append(file)
for i in combinations(list_files,2):
    img1 = read_image(dir + '/' + i[0])
    img2 = read_image(dir + '/' + i[1])
    diff = img1 - img2

    diff_btwn_img_data = np.linalg.norm(diff,axis=1)
    print("diff between " + str(i) + " two images is " + str(np.mean(diff_btwn_img_data)))

Top articles in this category:
  1. Flask Interview Questions
  2. Python coding challenges for interviews
  3. Top 100 interview questions on Data Science & Machine Learning
  4. Introduction to Python 3.6 & Jupyter Notebook
  5. Deploying Keras Model in Production using Flask
  6. Sequence of Differences in Python
  7. Find extra long factorials in python

Recommended books for interview preparation:

Find more on this topic: