This repository has been archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Dungeon-MASSters/master
Image processing on python
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from rembg import remove | ||
from PIL import Image | ||
|
||
|
||
def delete_background(image_path): | ||
inp = Image.open(image_path) | ||
out = remove(inp) | ||
return out | ||
|
||
|
||
def resize_img_to_eq(obj_img, back_img): | ||
w_b, h_b = back_img.size | ||
w_o, h_o = obj_img.size | ||
if w_o > w_b or w_o < w_b: | ||
width_percent = (w_b / float(obj_img.size[0])) | ||
height_size = int((float(obj_img.size[1]) * float(width_percent))) | ||
obj_img = obj_img.resize((w_b, height_size)) | ||
if h_o > h_b or h_o < h_b: | ||
hight_percent = (h_b / float(obj_img.size[1])) | ||
width_size = int((float(obj_img.size[0]) * float(hight_percent))) | ||
obj_img = obj_img.resize((width_size, h_b)) | ||
return obj_img | ||
|
||
|
||
def combine_images(obj_img, background, pos_x = 0, pos_y = 0, scale = 1, rotation = 0): | ||
back_img = Image.open(background) | ||
obj_img = Image.open(obj_img) | ||
|
||
obj_img = resize_img_to_eq(obj_img, back_img) | ||
|
||
#restrictions | ||
if scale > 1: scale = 1 | ||
if scale < 0.2: scale = 0.2 | ||
|
||
obj_img = obj_img.resize((int(obj_img.width * scale), int(obj_img.height * scale))) | ||
|
||
obj_img = obj_img.rotate(angle = rotation) | ||
|
||
# centering | ||
back_img.paste( | ||
obj_img, | ||
(round(back_img.width / 2 - obj_img.width / 2 + pos_x), round(back_img.height / 2 - obj_img.height / 2 + pos_y)), | ||
mask=obj_img | ||
) | ||
|
||
return back_img |