Obaid Ur Rehman
Obaid Ur Rehman

Reputation: 323

Implement function defined as entry point

I created a python program which simply removes green background from images.

Now I want to implement my function remove_green_background() defined as an entry point. I have searched through many websites and also through stackoverflow but can't understand how entry points works.

So anybody can explain it to me using this code in detail that where to put those entry points?

from PIL import Image
import sys
import os


def rgb_to_hsv(r, g, b):
    maxc = max(r, g, b)
    minc = min(r, g, b)
    v = maxc
    if minc == maxc:
        return 0.0, 0.0, v
    s = (maxc-minc) / maxc
    rc = (maxc-r) / (maxc-minc)
    gc = (maxc-g) / (maxc-minc)
    bc = (maxc-b) / (maxc-minc)
    if r == maxc:
        h = bc-gc
    elif g == maxc:
        h = 2.0+rc-bc
    else:
        h = 4.0+gc-rc
    h = (h/6.0) % 1.0
    return h, s, v


GREEN_RANGE_MIN_HSV = (100, 80, 70)
GREEN_RANGE_MAX_HSV = (185, 255, 255)

def remove_green_background():
    # Load image and convert it to RGBA, so it contains alpha channel
    name, ext = os.path.splitext(Filepath)
    im = Image.open(Filepath)
    im = im.convert('RGBA')

    # Go through all pixels and turn each 'green' pixel to transparent
    pix = im.load()
    width, height = im.size
    for x in range(width):
        for y in range(height):
            r, g, b, a = pix[x, y]
            h_ratio, s_ratio, v_ratio = rgb_to_hsv(r / 255.0, g / 255.0, b / 255.0)
            h, s, v = (h_ratio * 360, s_ratio * 255, v_ratio * 255)

            min_h, min_s, min_v = GREEN_RANGE_MIN_HSV
            max_h, max_s, max_v = GREEN_RANGE_MAX_HSV
            if min_h <= h <= max_h and min_s <= s <= max_s and min_v <= v <= max_v:
                pix[x, y] = (0, 0, 0, 0)


    im.save(name + '.png')


if __name__ == '__main__':
    remove_green_background()

Upvotes: 2

Views: 104

Answers (3)

user14792020
user14792020

Reputation:

In most cases, entry point is defined as the function name. So simply change your function name to remove_green_background() and this would be your entry point.

Upvotes: 0

Ralf
Ralf

Reputation: 16505

This should work:

if __name__ == '__main__':
    remove_green_background()

Upvotes: 1

Eats Indigo
Eats Indigo

Reputation: 396

The other way round:

if __name__ == '__main__':
    remove_green_background()

Upvotes: 2

Related Questions