Files
JustTwirk/utils.py
2025-08-25 19:37:48 +02:00

34 lines
992 B
Python

import cv2
import numpy as np
def recvall(sock, n):
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def resize_with_padding(image, target_size=(640, 640)):
h, w = image.shape[:2]
target_w, target_h = target_size
# Oblicz współczynnik skalowania, zachowując proporcje
scale = min(target_w / w, target_h / h)
new_w, new_h = int(w * scale), int(h * scale)
# Resize obrazu
resized_image = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA)
# Stwórz tło (czarne) o wymiarach docelowych
output_image = np.zeros((target_h, target_w, 3), dtype=np.uint8)
# Oblicz offsety do wyśrodkowania obrazu
x_offset = (target_w - new_w) // 2
y_offset = (target_h - new_h) // 2
# Wklej resized image na tło
output_image[y_offset:y_offset+new_h, x_offset:x_offset+new_w] = resized_image
return output_image