79559340

Date: 2025-04-07 07:54:44
Score: 2
Natty:
Report link

import cv2

import numpy as np

from PIL import Image

# Încarcă imaginea originală

img = cv2.imread("/mnt/data/file-49ASQcghDJJzfcGtVqsUGF")

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

height, width = img.shape[:2]

# Initializează masca pentru GrabCut

mask = np.zeros(img.shape[:2], np.uint8)

bgdModel = np.zeros((1, 65), np.float64)

fgdModel = np.zeros((1, 65), np.float64)

# Definește un dreptunghi care acoperă aproximativ subiectul

rect = (10, 10, width - 20, height - 20)

cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

# Creează masca finală: 0 pentru fundal, 1 pentru subiect

mask2 = np.where((mask==2) | (mask==0), 0, 1).astype('uint8')

img_subject = img_rgb * mask2[:, :, np.newaxis]

# Convertim imaginea subiectului la format PIL

subject = Image.fromarray(img_subject)

# Crearea unui fundal de tip "Cer cu apus"

background = Image.new("RGB", (width, height))

for y in range(height):

\# Calculul culorilor pentru un gradient specific apusului

\# De la portocaliu aprins în partea de sus la roșu în partea de jos

r = int(255 - (y / height) \* 100)

g = int(100 - (y / height) \* 100)

b = int((y / height) \* 150)

for x in range(width):

    background.putpixel((x, y), (r, g, b))

# Combină subiectul cu noul fundal, folosind masca

mask_pil = Image.fromarray((mask2 * 255).astype(np.uint8))

background.paste(subject, (0, 0), mask_pil)

# Salvează rezultatul final

background.save("/mnt/

data/edited_image.png")

background.show()

Reasons:
  • Blacklisted phrase (2): Crear
  • Long answer (-1):
  • Has code block (-0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Darius