backend.components.utils.imageUtils

 1import numpy as np
 2import cv2
 3
 4
 5def parse_image(file_bytes):
 6    '''
 7    Decodes image from bytes to ndarray
 8
 9    **Parameters**:
10    - `file` (bytes): Raw image bytes.
11
12    **Returns**:
13    - `ndarray`: Image decoded into ndarray.
14    '''
15    file_bytes = np.frombuffer(file_bytes, np.uint8)
16    image_bgr = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
17    if image_bgr is None:
18        raise ValueError("Nie udało się przetworzyć pliku jako obrazu.")
19    img = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
20    return img
21
22
23def encode_image(img_array, encode_format=".png"):
24    '''
25    Encodes image into bytes from ndarray
26
27    **Parameters**:
28    - `img_array` (ndarray): Image decoded into ndarray
29    - `format` (str): Optional. Encoding format as file extension. Default .png
30
31    **Returns**:
32    - `bytes`: Raw image bytes
33
34    **Raises**:
35    - `ValueError` - CV2 failed to encode the image to the format.
36    '''
37    try:
38        # cv2.imencode rzuca błąd dla pustych macierzy
39        success, buffer = cv2.imencode(encode_format, img_array)
40    except cv2.error:
41        success = False
42    if not success:
43        raise ValueError(f"Nie udało się zenkodować obrazu do formatu {encode_format}")
44    image_bytes = buffer.tobytes()
45    return image_bytes
def parse_image(file_bytes):
 6def parse_image(file_bytes):
 7    '''
 8    Decodes image from bytes to ndarray
 9
10    **Parameters**:
11    - `file` (bytes): Raw image bytes.
12
13    **Returns**:
14    - `ndarray`: Image decoded into ndarray.
15    '''
16    file_bytes = np.frombuffer(file_bytes, np.uint8)
17    image_bgr = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
18    if image_bgr is None:
19        raise ValueError("Nie udało się przetworzyć pliku jako obrazu.")
20    img = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
21    return img

Decodes image from bytes to ndarray

Parameters:

  • file (bytes): Raw image bytes.

Returns:

  • ndarray: Image decoded into ndarray.
def encode_image(img_array, encode_format='.png'):
24def encode_image(img_array, encode_format=".png"):
25    '''
26    Encodes image into bytes from ndarray
27
28    **Parameters**:
29    - `img_array` (ndarray): Image decoded into ndarray
30    - `format` (str): Optional. Encoding format as file extension. Default .png
31
32    **Returns**:
33    - `bytes`: Raw image bytes
34
35    **Raises**:
36    - `ValueError` - CV2 failed to encode the image to the format.
37    '''
38    try:
39        # cv2.imencode rzuca błąd dla pustych macierzy
40        success, buffer = cv2.imencode(encode_format, img_array)
41    except cv2.error:
42        success = False
43    if not success:
44        raise ValueError(f"Nie udało się zenkodować obrazu do formatu {encode_format}")
45    image_bytes = buffer.tobytes()
46    return image_bytes

Encodes image into bytes from ndarray

Parameters:

  • img_array (ndarray): Image decoded into ndarray
  • format (str): Optional. Encoding format as file extension. Default .png

Returns:

  • bytes: Raw image bytes

Raises:

  • ValueError - CV2 failed to encode the image to the format.