pycharmers.utils.pil_utils module

pycharmers.utils.pil_utils.pilread(img=None, path=None)[source]

Opens and identifies the given image file.

Parameters
  • img (PIL.Image) – PIL Image object

  • path (str) – Path or URL to image file.

Returns

img (PIL.Image) : PIL Image object

Examples

>>> from pycharmers.utils import pilread
>>> img = pilread(img=None, path="https://iwasakishuto.github.io/Python-Charmers/_static/favicon.png")
>>> img == pilread(img=img, path=None)
True
pycharmers.utils.pil_utils.roughen_img(img=None, path=None, rrate=5)[source]

Roughen the Image.

Parameters
  • img (PIL.Image) – image file.

  • path (str) – Path or URL to image file.

  • rrate (float) – Reduction rate.

Returns

img (PIL.Image) : Roughened PIL Image object

Examples

>>> from pycharmers.utils import roughen_img, pilread
>>> img = pilread(path="https://iwasakishuto.github.io/Python-Charmers/_static/favicon.png")
>>> roughened_img = roughen_img(img=img, rrate=5)
>>> img.size == roughened_img.size
True
pycharmers.utils.pil_utils.draw_text_in_pil(text: str, img: Optional[PIL.Image.Image] = None, ttfontname: Optional[str] = None, img_size: Tuple[int, int] = 250, 250, text_width: Optional[int] = None, fontsize: int = 16, fontwidth: Optional[int] = None, fontheight: Optional[int] = None, margin: int = 10, line_height: Optional[int] = None, drop_whitespace: bool = False, bgRGB: Union[str, Tuple] = 255, 255, 255, textRGB: Union[str, Tuple] = 0, 0, 0, mode: str = 'RGB', ret_position: str = 'line', **kwargs)[source]

Draw text in PIL.Image object.

Parameters
  • text (str) – Text to be drawn to img.

  • img (PIL.Image) – The image to draw in. If this argment is None, img will be created using img_size and bgRGB arguments.

  • ttfontname (str) – A filename or file-like object containing a TrueType font. If the file is not found in this filename, the loader may also search in other directories, such as the fonts/ directory on Windows or /Library/Fonts/ , /System/Library/Fonts/ and ~/Library/Fonts/ on macOS.

  • img_size (tuple) – The image size.

  • text_width (int) – The length of characters in one line.

  • fontsize (int) – The requested size, in points.

  • fontwidth (int) – The font width. (If not given, automatically calculated.)

  • fontheight (int) – The font height. (If not given, automatically calculated.)

  • margin (int) – The margin size.

  • line_height (int) – The line height. If not specify, use font.getsize(string.ascii_letters)

  • drop_whitespace (bool) – If True, whitespace at the beginning and ending of every line. Defaults to False.

  • bgRGB (tuple) – The color of background image. (RGB)

  • textRGB (tuple) – The color of text. (RGB)

  • mode (str) – Optional mode to use for color values.

  • ret_position (str) – Type of the position of next text to be returned. Please choose from ["line", "word"]. Defaults to "line".

  • **kwargs (dict) – Specify margin_top , margin_right , margin_bottom , margin_left .

Returns

img, Position of next text ( x , y ).

Return type

tuple (PIL.Image, pos)

Example

>>> from pycharmers.utils import draw_text_in_pil
>>> img, y = draw_text_in_pil("Hello World!!")
>>> img.save("sample.png")
pycharmers.utils.pil_utils.draw_cross(img, size, width=5, fill_color=255, 0, 0, 255, outline=None, color_mode='RGBA', margin=0, **kwargs)[source]

Draw Cross Mark.

Parameters
  • img (PIL.Image) – Pillow Image.

  • size (int/tuple) – Cross mark size. (width,Height)

  • width (int) – The width of the cross mark.

  • fill_color (tuple) – The color in the line.

  • outline (tuple) – The color of the edge of the line.

  • color_mode (str) – Color Mode (ex. "RGBA" , "P" )

  • margin (int/list) – Specify the position.

  • **kwargs (dict) – Specify the individual margin ( margin_top , margin_right )

Examples

>>> from PIL import Image
>>> from pycharmers.opencv import SAMPLE_LENA_IMG
>>> from pycharmers.utils import draw_cross
>>> img = Image.open(SAMPLE_LENA_IMG)
>>> draw_cross(img=img, size=200, width=10)
>>> draw_cross(img=img, size=(100,200), width=10, outline=(0,255,0))
pycharmers.utils.pil_utils.draw_frame(img, width=10, border_color=255, 255, 255, is_radius=True)[source]

Draw Frame with Image.

Parameters
  • img (PIL.Image) – Pillow Image.

  • width (int) – The width of the border.

  • border_color (tuple) – The color in the border.

  • is_radius (bool) – Whether to round the corners.

Examples

>>> from PIL import Image
>>> from pycharmers.opencv import SAMPLE_LENA_IMG
>>> from pycharmers.utils import draw_frame
>>> img = Image.open(SAMPLE_LENA_IMG)
>>> draw_frame(img=img, width=10)