wed.utils.image_utils module¶
This submodule contains a set of functions for editing the following image:
- wed.utils.image_utils.arr2pil(frame: numpy.ndarray[Any, numpy.dtype[numpy.uint8]]) → PIL.Image.Image[source]¶
Convert from
frame
(BGRnpt.NDArray
) toimage
(RGBImage.Image
)- Parameters
frame (npt.NDArray[np.uint8]) – A BGR
npt.NDArray
.- Returns
A RGB
Image.Image
- Return type
Image.Image
- wed.utils.image_utils.pil2arr(image: PIL.Image.Image) → numpy.ndarray[Any, numpy.dtype[numpy.uint8]][source]¶
Convert from
image
(RGBImage.Image
) toframe
(BGRnpt.NDArray
).- Parameters
image (Image.Image) – A RGB
Image.Image
- Returns
A BGR
npt.NDArray
.- Return type
npt.NDArray[np.uint8]
- wed.utils.image_utils.cv2plot(frame: numpy.ndarray[Any, numpy.dtype[numpy.uint8]], ax: Optional[matplotlib.axes._axes.Axes] = None, isBGR: bool = True) → matplotlib.axes._axes.Axes[source]¶
Plot a image.
- Parameters
frame (npt.NDArray[np.uint8]) – Input image.
ax ([type], optional) – An
Axes
instance. Defaults toNone
.isBGR (bool, optional) – Whether
frame
is BGR (OpenCV format) or not. Defaults toTrue
.
- Returns
- Return type
Axes
- wed.utils.image_utils.draw_text_in_pil(text: str, ttfontname: str = '/Users/iwasakishuto/Github/portfolio/Wed-Downtown-OP/data/fonts/LeagueGothic-1.601/static/TTF/LeagueGothic-Regular.ttf', img: Optional[PIL.Image.Image] = None, xy: Tuple = (0, 0), img_size: Tuple = (640, 360), img_mode: str = 'RGB', bgRGB: Union[str, Tuple] = 'white', textRGB: Union[str, Tuple] = 'black', fontsize: int = 16, fontwidth: Optional[int] = None, fontheight: Optional[int] = None, line_height: Optional[int] = None, anchor: Optional[str] = None, spacing: int = 4, align: str = 'left', direction: str = 'ltr', features: Optional[str] = None, language: Optional[str] = None, stroke_width: int = 0, stroke_fill: Optional[Union[str, Tuple]] = None, embedded_color: bool = False, text_width: Optional[int] = None, wrap_text: bool = False, drop_whitespace: bool = False, ret_position: str = 'word') → PIL.Image.Image[source]¶
Draw text in pillow image (
Image.Image
).- Parameters
text (str) – Text to be drawn to
img
.ttfontname (str, optional) – A filename or file-like object containing a TrueType font. Defaults to
FONT_LEAGUEGOTHIC_PATH
.img (Optional[Image.Image], optional) – The image to draw in. If this argment is
None
, img will be created usingimg_size
andbgRGB
arguments. Defaults toNone
.xy (Tuple, optional) – Where to write the
text
. This value means the coordinates of (x
,y
). Defaults to(0, 0)
.img_size (Tuple, optional) – The image size. Defaults to
(640, 360)
.img_mode (str, optional) – Optional mode to use for color values. Defaults to
"RGB"
.bgRGB (Union[str, Tuple], optional) – The color of background image. Defaults to
"white"
.textRGB (Union[str, Tuple], optional) – The color of text. Defaults to
"black"
.fontsize (int, optional) – The font size. Defaults to
16
.fontwidth (Optional[int], optional) – The font width. (If not given, automatically calculated.) Defaults to
None
.fontheight (Optional[int], optional) – The font height. (If not given, automatically calculated.) Defaults to
None
.line_height (Optional[int], optional) – The line height. (If not given, automatically calculated.) Defaults to
None
.anchor (Optional[str], optional) – The text anchor alignment. Defaults to
None
.spacing (int, optional) – The number of pixels between lines. Defaults to
4
.align (str, optional) – Determines the relative alignment of lines. Defaults to
"left"
.direction (str, optional) – Direction of the text. Defaults to
"ltr"
.features (Optional[str], optional) – A list of OpenType font features to be used during text layout. Defaults to
None
.language (Optional[str], optional) – Language of the text. Defaults to
None
.stroke_width (int, optional) – The width of the text stroke. Defaults to
0
.stroke_fill (Optional[Union[str, Tuple]], optional) – Color to use for the text stroke. If not given, will default to the
textRGB
parameter. Defaults toNone
.embedded_color (bool, optional) – Whether to use font embedded color glyphs (COLR, CBDT, SBIX). Defaults to
False
.text_width (Optional[int], optional) – The length of characters in one line. Defaults to
None
.wrap_text (bool, optional) – Whether to wrap
text
for multilines or not. Defaults toFalse
.drop_whitespace (bool, optional) – If
True
, whitespace at the beginning and ending of every line. Defaults toFalse
.ret_position (str, optional) – Type of the position of next text to be returned. Please choose from
["line", "word"]
. Defaults to"word"
.
- Returns
img
withtext
drawn.- Return type
Image.Image
- wed.utils.image_utils.image_conversion(frame: numpy.ndarray[Any, numpy.dtype[numpy.uint8]], method: str) → numpy.ndarray[Any, numpy.dtype[numpy.uint8]][source]¶
Convert image by
method
method- Parameters
frame (npt.NDArray[np.uint8]) – Input image (BGR).
method (str) – How to convert an image.
- Returns
Converted image.
- Return type
npt.NDArray[np.uint8]
>>> import cv2 >>> import matplotlib.pyplot as plt >>> from wed.utils import ( ... ROTATING_SQUARE_IMAGE_PATH, ... SUPPORTED_CONVERSION_METHODS, ... cv2plot, ... image_conversion, >>> ) >>> frame = cv2.imread(ROTATING_SQUARE_IMAGE_PATH) >>> num_methods = len(SUPPORTED_CONVERSION_METHODS) >>> fig, axes = plt.subplots(ncols=num_methods, nrows=1, figsize=(6 * num_methods, 4)) >>> for ax, method in zip(axes, SUPPORTED_CONVERSION_METHODS): ... ax = cv2plot(image_conversion(frame, method=method), ax=ax) ... ax.set_title(method) >>> fig.show()
- wed.utils.image_utils.nega_conversion(frame: numpy.ndarray[Any, numpy.dtype[numpy.uint8]]) → numpy.ndarray[Any, numpy.dtype[numpy.uint8]][source]¶
Convert images to negative and positive.
- Parameters
frame (npt.NDArray[np.uint8]) – Input image (BGR).
- Returns
[description]
- Return type
npt.NDArray[np.uint8]
- wed.utils.image_utils.alpha_composite(bg: PIL.Image.Image, paste: PIL.Image.Image, box: Tuple[int, int] = (0, 0)) → PIL.Image.Image[source]¶
Paste the
paste
image tobg
with considering alpha channel.- Parameters
bg (Image.Image) – Background Image.
paste (Image.Image) – Image to paste.
box (Tuple[int,int], optional) – Where to paste the
paste
image onbg
image. Defaults to(0,0)
.
- Returns
The alpha-composited image.
- Return type
Image.Image