| import numpy as np | |
| import io | |
| from PIL import Image, ImageFilter, ImageChops | |
| from torchvision import transforms | |
| def genELA(img_pil, scale=77, alpha=0.66): | |
| # Error Level Analysis for basic image forensics | |
| original = img_pil.copy() # open up the input image | |
| temp_path = 'temp.jpg' # temporary image name to save the ELA to | |
| original.save(temp_path, quality=95) # re-save the image with a quality of 95% | |
| temporary = Image.open(temp_path) # open up the re-saved image | |
| diff = ImageChops.difference(original, temporary) # load in the images to look at pixel by pixel differences | |
| d = diff.load() # load the image into a variable | |
| WIDTH, HEIGHT = diff.size # set the size into a tuple | |
| for x in range(WIDTH): # row by row | |
| for y in range(HEIGHT): # column by column | |
| d[x, y] = tuple(k * scale for k in d[x, y]) # set the pixels to their x,y & color based on error | |
| new_img = ImageChops.blend(temporary, diff, alpha) # blend the original w/ the ELA @ a set alpha/transparency | |
| return new_img |