Python Remove Parts Of An Image

Do you like this?

Summary:
This snippet helps you remove parts of an image.


Content:

All you need to do is to blur the sections of the image many times as belows:

from PIL import Image, ImageFilter
import os

positions = (36, 9, 117, 37)

image = Image.open (os.path.join(os.path.abspath('.') ,"image.jpg")) #load image.jpg from the current directory

# blur parts of the image
image_crop_part = image.crop(positions)
for i in range(10):  # You can blur many times
	image_crop_part = image_crop_part.filter(ImageFilter.BLUR)
image.paste(image_crop_part, positions)

image.save(os.path.join(".", "image_new.jpg")) #save the processed image as image_new.jpg

Following is an real life example in which this snippet gets all the images in the current directory, remove parts of them and then save all into a sub folder "images"
from PIL import Image, ImageFilter
import os

# constants
image_extensions = ['JPG']
positions = (36, 9, 117, 37)

resize_flag = True
resize_method = Image.ANTIALIAS
    #Image.NEAREST)  # use nearest neighbour
    #Image.BILINEAR) # linear interpolation in a 2x2 environment
    #Image.BICUBIC) # cubic spline interpolation in a 4x4 environment
    #Image.ANTIALIAS) # best down-sizing filter
resize_ratio= 0.9 # real_size * resize_ratio

image_folder= "images"

# end - constants

def resize(image):
    width, height = image.size
    width, height = int(width * resize_ratio) , int(height * resize_ratio)
    image = image.resize((width, height), resize_method) 
    return image

def filterImageFiles(filenames):
    result = []
    for filename in filenames: # loop through all the files and folders
        if os.path.isfile(os.path.join(os.path.abspath("."), filename)): # check whether the current object is a file or not
            filename_text, filename_ext= os.path.splitext(filename)
            filename_ext= filename_ext[1:]
            filename_ext = filename_ext.upper()
            if filename_ext in image_extensions: # check whether the current object is a JPG file
                result.append(filename)                    
    result.sort()
    return result

def process_image_files(image_files):
    for index, image_file in enumerate(image_files):
        # open the original image
        print "%s - %s - %s" % (index,image_file, image_file[:-4])
        image = Image.open (os.path.join(os.path.abspath('.') ,image_file))

        # blur parts of the image
        image_crop_part = image.crop(positions)
        for i in range(10):  # You can blur many times
            image_crop_part = image_crop_part.filter(ImageFilter.BLUR)
        image.paste(image_crop_part, positions)

        if resize_flag:
            # resizing the cropped images
            image = resize(image)
        
        # saving the cropped images
        image.save(os.path.join(".",image_folder,image_file))

def empty_folder(folder):
    for the_file in os.listdir(folder):
        file_path = os.path.join(folder, the_file)
        try:
            os.unlink(file_path)
        except Exception, e:
            print e


def rebuild_image_folder():
    image_folder_path= os.path.join(".",image_folder)
    if os.path.exists(image_folder_path):
        print "Clearing %s" % image_folder_path
        empty_folder(image_folder_path)
    else:
        print "Creating %s" % image_folder_path
        os.mkdir(image_folder_path)

if __name__ == "__main__":
    rebuild_image_folder()
    image_files = filterImageFiles(os.listdir('.'))
    process_image_files(image_files)

 
comments powered by Disqus