Base64 Process
This module uses pybase64 internally and provides both bytes and str interfaces (e.g. img_to_b64 vs img_to_b64str).
-
Common issue: string vs bytes?
In Python,
stris a sequence of Unicode characters, whilebytesis a sequence of raw bytes. Base64 operates on bytes, so encoding/decoding typically happens onbytes. String variants are provided for convenience.
img_to_b64
-
Description: Encodes a numpy image into Base64
bytes. -
Parameters
- img (
np.ndarray): Image array. - imgtyp (
str | int | IMGTYP): Image type. SupportsIMGTYP.JPEG/IMGTYP.PNG. Default isIMGTYP.JPEG.
- img (
-
Returns
- bytes | None: Base64 bytes; returns
Nonewhen encoding fails.
- bytes | None: Base64 bytes; returns
-
Notes
- For backward compatibility,
IMGTYP=...is also accepted (providing bothimgtypandIMGTYPraisesTypeError).
- For backward compatibility,
-
Example
from capybara.vision.improc import IMGTYP, img_to_b64, imread
img = imread('lena.png')
b64 = img_to_b64(img, imgtyp=IMGTYP.PNG)
npy_to_b64
-
Description: Encodes a numpy array into Base64
bytes. -
Parameters
- x (
np.ndarray): Numpy array. - dtype (
str): Cast dtype before encoding. Default is'float32'.
- x (
-
Returns
- bytes: Base64 bytes.
-
Example
import numpy as np
from capybara.vision.improc import npy_to_b64
x = np.random.rand(100, 100, 3)
b64 = npy_to_b64(x)
npy_to_b64str
npy_to_b64str(x: np.ndarray, dtype='float32', string_encode: str = 'utf-8') -> str
-
Description: Encodes a numpy array into a Base64 string.
-
Parameters
- x (
np.ndarray): Numpy array. - dtype (
str): Cast dtype before encoding. Default is'float32'. - string_encode (
str): String encoding. Default is'utf-8'.
- x (
-
Returns
- str: Base64 string.
-
Example
import numpy as np
from capybara.vision.improc import npy_to_b64str
x = np.random.rand(100, 100, 3)
b64str = npy_to_b64str(x)
img_to_b64str
-
Description: Encodes a numpy image into a Base64 string.
-
Parameters
- img (
np.ndarray): Image array. - imgtyp (
str | int | IMGTYP): Image type. SupportsIMGTYP.JPEG/IMGTYP.PNG. Default isIMGTYP.JPEG. - string_encode (
str): String encoding. Default is'utf-8'.
- img (
-
Returns
- str | None: Base64 string; returns
Nonewhen encoding fails.
- str | None: Base64 string; returns
-
Notes
- For backward compatibility,
IMGTYP=...is also accepted (providing bothimgtypandIMGTYPraisesTypeError).
- For backward compatibility,
-
Example
import capybara as cb
img = cb.imread('lena.png')
b64str = cb.img_to_b64str(img, imgtyp=cb.IMGTYP.PNG)
b64_to_img
-
Description: Decodes Base64 bytes into a numpy image.
-
Parameters
- b64 (
bytes): Base64 bytes.
- b64 (
-
Returns
- np.ndarray | None: Decoded image; returns
Noneon failure.
- np.ndarray | None: Decoded image; returns
-
Example
from capybara.vision.improc import b64_to_img, img_to_b64, imread
b64 = img_to_b64(imread('lena.png'))
img = b64_to_img(b64)
b64str_to_img
b64str_to_img(b64str: str | None, string_encode: str = 'utf-8') -> np.ndarray | None
-
Description: Decodes a Base64 string into a numpy image.
-
Parameters
- b64str (
str | None): Base64 string. - string_encode (
str): String encoding. Default is'utf-8'.
- b64str (
-
Returns
- np.ndarray | None: Decoded image; returns
Noneon failure.
- np.ndarray | None: Decoded image; returns
-
Example
from capybara.vision.improc import b64str_to_img, img_to_b64, imread
b64 = img_to_b64(imread('lena.png'))
b64str = b64.decode('utf-8')
img = b64str_to_img(b64str)
b64_to_npy
-
Description: Decodes Base64 bytes into a numpy array.
-
Parameters
- x (
bytes): Base64 bytes. - dtype (
str): Output dtype. Default is'float32'.
- x (
-
Returns
- np.ndarray: Decoded numpy array.
-
Example
import numpy as np
from capybara.vision.improc import b64_to_npy, npy_to_b64
b64 = npy_to_b64(np.random.rand(100, 100, 3))
x = b64_to_npy(b64)
b64str_to_npy
b64str_to_npy(x: str, dtype='float32', string_encode: str = 'utf-8') -> np.ndarray
-
Description: Decodes a Base64 string into a numpy array.
-
Parameters
- x (
str): Base64 string. - dtype (
str): Output dtype. Default is'float32'. - string_encode (
str): String encoding. Default is'utf-8'.
- x (
-
Returns
- np.ndarray: Decoded numpy array.
-
Example
import numpy as np
from capybara.vision.improc import b64str_to_npy, npy_to_b64
b64 = npy_to_b64(np.random.rand(100, 100, 3))
x = b64str_to_npy(b64.decode('utf-8'))