Skip to content

Instantly share code, notes, and snippets.

@dunossauro
Created September 15, 2021 18:32
Show Gist options
  • Save dunossauro/237ae08f611ec5a6dfc0fa2438e7bc9a to your computer and use it in GitHub Desktop.
Save dunossauro/237ae08f611ec5a6dfc0fa2438e7bc9a to your computer and use it in GitHub Desktop.
remove image background and enhance image
"""
requires:
pip install rembg pillow numpy
usage:
python this_file.py input_image.png output_image.png
"""
from argparse import ArgumentParser
from io import BytesIO
from numpy import fromfile
from PIL import Image, ImageEnhance
from rembg.bg import remove
parser = ArgumentParser()
parser.add_argument(dest='input', type=str, help='Input image path')
parser.add_argument(dest='output', type=str, help='Output image path')
args = parser.parse_args()
photo_input = args.input
photo_output = args.output
def image_filter(img):
contrast = ImageEnhance.Contrast(img)
contrasted = contrast.enhance(1.1)
brightness = ImageEnhance.Brightness(contrasted)
bright = brightness.enhance(1.1)
saturation = ImageEnhance.Color(bright)
saturated = saturation.enhance(1.3)
return saturated
image_array = fromfile(photo_input)
image_bg = remove(image_array)
image_pillow = Image.open(BytesIO(image_bg)).convert("RGBA")
image_edited = image_filter(image_pillow).resize((1280, 720))
image_edited.save(photo_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment