mirror of
https://github.com/pythad/nider.git
synced 2021-10-12 02:31:02 +03:00
Improved in-code docs + patch changes
This commit is contained in:
10
Makefile
10
Makefile
@@ -67,9 +67,13 @@ build_docs: ## run sphinx build on docs/ directory
|
||||
sphinx-build -b html docs/ docs/_build
|
||||
|
||||
docs: ## generate Sphinx HTML documentation, including API docs
|
||||
rm -f docs/nider.rst
|
||||
rm -f docs/modules.rst
|
||||
sphinx-apidoc -o docs/ nider
|
||||
sphinx-apidoc -o docs/ -T nider
|
||||
$(MAKE) -C docs clean
|
||||
$(MAKE) -C docs html
|
||||
$(BROWSER) docs/_build/html/index.html
|
||||
|
||||
docs_force:
|
||||
sphinx-apidoc -o docs/ -T -f nider
|
||||
$(MAKE) -C docs clean
|
||||
$(MAKE) -C docs html
|
||||
$(BROWSER) docs/_build/html/index.html
|
||||
|
||||
@@ -8,9 +8,9 @@ Nider is an approach to make generation of text based images simple yet flexible
|
||||
|
||||
installation
|
||||
usage
|
||||
Full API reference <nider>
|
||||
contributing
|
||||
authors
|
||||
Full API reference <modules>
|
||||
history
|
||||
|
||||
Indices and tables
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
nider
|
||||
=====
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 4
|
||||
|
||||
nider
|
||||
@@ -1,54 +1,20 @@
|
||||
nider package
|
||||
=============
|
||||
|
||||
Subpackages
|
||||
-----------
|
||||
|
||||
.. toctree::
|
||||
|
||||
nider.colors
|
||||
nider.mixins
|
||||
|
||||
Submodules
|
||||
----------
|
||||
|
||||
nider\.core module
|
||||
------------------
|
||||
|
||||
.. automodule:: nider.core
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
:members: Font, Outline
|
||||
|
||||
nider\.models module
|
||||
--------------------
|
||||
|
||||
.. automodule:: nider.models
|
||||
:members: Header, Paragraph, Linkback, Content, Image, FacebookSquarePost, FacebookLandscapePost, TwitterPost, TwitterLargeCard, InstagramSquarePost, InstagramPortraitPost, InstagramLandscapePost
|
||||
|
||||
nider\.exceptions module
|
||||
------------------------
|
||||
|
||||
.. automodule:: nider.exceptions
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
nider\.models module
|
||||
--------------------
|
||||
|
||||
.. automodule:: nider.models
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
nider\.utils module
|
||||
-------------------
|
||||
|
||||
.. automodule:: nider.utils
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
|
||||
Module contents
|
||||
---------------
|
||||
|
||||
.. automodule:: nider
|
||||
:members:
|
||||
:undoc-members:
|
||||
:show-inheritance:
|
||||
|
||||
155
docs/usage.rst
155
docs/usage.rst
@@ -18,32 +18,14 @@ There are three main units each ``nider.Image`` can consist of:
|
||||
|
||||
Each of the units is represented by a class in ``nider.models``:
|
||||
|
||||
- ``nider.models.Header``
|
||||
- ``nider.models.Paragraph``
|
||||
- ``nider.models.Linkback``
|
||||
- :class:`nider.models.Header`
|
||||
- :class:`nider.models.Paragraph`
|
||||
- :class:`nider.models.Linkback`
|
||||
|
||||
``nider.models.Header``
|
||||
=========================
|
||||
|
||||
.. class:: Header(text, \
|
||||
font=Font(), \
|
||||
text_width=21, line_padding=6, \
|
||||
color=None, outline=None \
|
||||
align='center')
|
||||
|
||||
Base class for the header unit
|
||||
|
||||
:param str text: Text used in the header
|
||||
:param nider.core.Font font: :class:`nider.core.Font` object that represents text's font
|
||||
:param int text_width: Header's text width - number of characters in a line
|
||||
:param int line_padding: Header's line padding - padding (in pixels) between the lines
|
||||
:param str color: string that represents a color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_
|
||||
:param nider.core.Outline outline: :class:`nider.core.Outline` object that represents text's outline
|
||||
:param align: Side with respect to which the text will be aligned
|
||||
:type align: 'left' or 'center' or 'right'
|
||||
:raises nider.exceptions.InvalidAlignException: if ``align`` is not one of 'left' or 'center' or 'right'
|
||||
:raises nider.exceptions.DefaultFontWarning: if ``font.path`` is ``None``
|
||||
:raises nider.exceptions.FontNotFoundWarning: if ``font.path`` does not exist
|
||||
.. autoclass:: nider.models.Header
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -66,27 +48,9 @@ Example
|
||||
``nider.models.Paragraph``
|
||||
============================
|
||||
|
||||
The class has the same attribures and behaviour as ``nider.models.Header``.
|
||||
This class has the same attribures and behaviour as :class:`nider.models.Header`.
|
||||
|
||||
.. class:: Paragraph(text, \
|
||||
font=Font(), \
|
||||
text_width=21, line_padding=6, \
|
||||
color=None, outline=None, \
|
||||
align='center')
|
||||
|
||||
Base class for the paragraph unit
|
||||
|
||||
:param str text: Text used in the paragraph
|
||||
:param nider.core.Font font: :class:`nider.core.Font` object that represents text's font
|
||||
:param int text_width: Paragraph's text width - number of characters in a line
|
||||
:param int line_padding: Paragraph's line padding - padding (in pixels) between the lines
|
||||
:param str color: string that represents a color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_
|
||||
:param nider.core.Outline outline: :class:`nider.core.Outline` object that represents text's outline
|
||||
:param align: Side with respect to which the text will be aligned
|
||||
:type align: 'left' or 'center' or 'right'
|
||||
:raises nider.exceptions.InvalidAlignException: if ``align`` is not one of 'left' or 'center' or 'right'
|
||||
:raises nider.exceptions.DefaultFontWarning: if ``font.path`` is ``None``
|
||||
:raises nider.exceptions.FontNotFoundWarning: if ``font.path`` does not exist
|
||||
.. autoclass:: nider.models.Paragraph
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -109,23 +73,7 @@ Example
|
||||
``nider.models.Linkback``
|
||||
===========================
|
||||
|
||||
.. class:: Linkback(text, \
|
||||
font=Font(), \
|
||||
color=None, outline=None, \
|
||||
align='center', bottom_padding=20)
|
||||
|
||||
Base class for the linkback unit
|
||||
|
||||
:param str text: Text used in the linkback
|
||||
:param nider.core.Font font: :class:`nider.core.Font` object that represents text's font
|
||||
:param str color: string that represents a color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_
|
||||
:param nider.core.Outline outline: :class:`nider.core.Outline` object that represents text's outline
|
||||
:param align: Side with respect to which the text will be aligned
|
||||
:type align: 'left' or 'center' or 'right'
|
||||
:param int bottom_padding: Linkback's bottom padding - padding (in pixels) between the bottom of the image and the linkback itself
|
||||
:raises nider.exceptions.InvalidAlignException: if ``align`` is not one of 'left' or 'center' or 'right'
|
||||
:raises nider.exceptions.DefaultFontWarning: if ``font.path`` is ``None``
|
||||
:raises nider.exceptions.FontNotFoundWarning: if ``font.path`` does not exist
|
||||
.. autoclass:: nider.models.Linkback
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -154,28 +102,12 @@ Example
|
||||
Image content
|
||||
*************
|
||||
|
||||
In order to aggregate all of the units together you need to create an instance of ``nider.models.Content`` class.
|
||||
In order to aggregate all of the units together you need to create an instance of :class:`nider.models.Content` class.
|
||||
|
||||
``nider.models.Content``
|
||||
==========================
|
||||
|
||||
.. class:: Content(paragraph=None, header=None, linkback=None, padding=45)
|
||||
|
||||
Class that aggregates different units into a sigle object
|
||||
|
||||
:param nider.models.Paragraph paragraph: Paragraph that will be used
|
||||
:param nider.models.Header header: Header that will be used
|
||||
:param nider.models.Linkback linkback: Linkback that will be used
|
||||
:param int padding: Content's padding - padding (in pixels) between the units.
|
||||
:raises nider.exceptions.ImageGeneratorException: if neither of paragraph, header or linkback is provided
|
||||
|
||||
.. warning::
|
||||
|
||||
Content has to consist at least of one unit: header, paragraph or linkback.
|
||||
|
||||
.. warning::
|
||||
|
||||
``padding`` is taken into account only if image is to get resized. If size allows content to fit freely, pre-calculated paddings will be used.
|
||||
.. autoclass:: nider.models.Content
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -197,23 +129,12 @@ Example
|
||||
Initializing an image
|
||||
*********************
|
||||
|
||||
After the content is prepared it's the right time to initialize an image. In ``nider`` a basic image is represented by ``nider.models.Image``
|
||||
After the content is prepared it's the right time to initialize an image. In ``nider`` a basic image is represented by ``nider.models.Image``.
|
||||
|
||||
``nider.models.Image``
|
||||
========================
|
||||
|
||||
.. class:: Image(content, fullpath, width=1080, height=1080, title=None, description=None)
|
||||
|
||||
Base class for a text based image
|
||||
|
||||
:param nider.models.Content content: Content object that has units to be rendered
|
||||
:param str fullpath: Path where the image has to be saved
|
||||
:param int width: Width of the image
|
||||
:param int height: Height of the image
|
||||
:param str title: Title of the image. Serves as metadata for latter rendering in html. May be used as alt text of the image. If no ``title`` is provided ``content.header.text will`` be set as the value
|
||||
:param str description: Description of the image. Serves as metadata for latter rendering in html. May be used as description text of the image. If no ``description`` is provided ``content.paragraph.text`` will be set as the value
|
||||
:raises AttributeError: if it's impossible to create a file at ``fullpath`` path
|
||||
:raises AttributeError: if width <= 0 or height <= 0
|
||||
.. autoclass:: nider.models.Image
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -234,26 +155,26 @@ Example
|
||||
Social media images
|
||||
-------------------
|
||||
|
||||
``nider`` comes with some pre-built models that can be used to generate images for some social networks. These are subclasses of ``nider.models.Image`` with changed size
|
||||
``nider`` comes with some pre-built models that can be used to generate images for some social networks. These are subclasses of ``nider.models.Image`` with changed size.
|
||||
|
||||
Instagram
|
||||
^^^^^^^^^
|
||||
|
||||
- ``nider.models.InstagramSquarePost`` - 1080x1080 image
|
||||
- ``nider.models.InstagramPortraitPost`` - 1080x1350 image
|
||||
- ``nider.models.InstagramLandscapePost`` - 1080x566 image
|
||||
- :class:`nider.models.InstagramSquarePost` - 1080x1080 image
|
||||
- :class:`nider.models.InstagramPortraitPost` - 1080x1350 image
|
||||
- :class:`nider.models.InstagramLandscapePost` - 1080x566 image
|
||||
|
||||
Facebook
|
||||
^^^^^^^^
|
||||
|
||||
- ``nider.models.FacebookSquarePost`` - 470x470 image
|
||||
- ``nider.models.FacebookLandscapePost`` - 1024x512 image
|
||||
- :class:`nider.models.FacebookSquarePost` - 470x470 image
|
||||
- :class:`nider.models.FacebookLandscapePost` - 1024x512 image
|
||||
|
||||
Twitter
|
||||
^^^^^^^
|
||||
|
||||
- ``nider.models.TwitterPost`` - 1024x512 image
|
||||
- ``nider.TwitterLargeCard`` - 506x506 image
|
||||
- :class:`nider.models.TwitterPost` - 1024x512 image
|
||||
- :class:`nider.models.TwitterLargeCard` - 506x506 image
|
||||
|
||||
============
|
||||
|
||||
@@ -280,16 +201,7 @@ Having an instance of ``nider.models.Image`` we are ready to create a real image
|
||||
``Image.draw_on_texture``
|
||||
=========================
|
||||
|
||||
.. method:: draw_on_texture(texture_path=None)
|
||||
|
||||
Draws preinitiated image and its attributes on a texture. If ``texture_path``
|
||||
is set to ``None``, takes random textures from ``textures/``
|
||||
|
||||
:param str texture_path: Path of the texture to use
|
||||
|
||||
:raises FileNotFoundError: if the file at ``texture_path`` cannot be found
|
||||
:raises nider.exceptions.ImageSizeFixedWarning: if the image size has to be adjusted to the provided content's size because the content takes much space
|
||||
|
||||
.. automethod:: nider.models.Image.draw_on_texture
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -310,7 +222,7 @@ Example
|
||||
img.draw_on_texture('example_texture.png')
|
||||
|
||||
|
||||
Check the full example `here <https://github.com/pythad/nider/blob/master/examples/draw_on_texture_example/script.py>`_
|
||||
Check the full example `here <https://github.com/pythad/nider/blob/master/examples/draw_on_texture_example/script.py>`_ .
|
||||
|
||||
============
|
||||
|
||||
@@ -319,16 +231,7 @@ Check the full example `here <https://github.com/pythad/nider/blob/master/exampl
|
||||
``Image.draw_on_bg``
|
||||
=========================
|
||||
|
||||
.. method:: draw_on_bg(bgcolor=None)
|
||||
|
||||
Draws preinitiated image and its attributes on a colored background. If ``bgcolor``
|
||||
is set to ``None``, random ``nider.colors.colormap.FLAT_UI`` color is generated
|
||||
|
||||
:param str bgcolor: string that represents a background color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_
|
||||
|
||||
:raises nider.exceptions.ImageSizeFixedWarning: if the image size has to be adjusted to the provided content's size because the content takes much space
|
||||
|
||||
|
||||
.. automethod:: nider.models.Image.draw_on_bg
|
||||
|
||||
Example
|
||||
-------
|
||||
@@ -348,20 +251,12 @@ Example
|
||||
|
||||
img.draw_on_bg('#efefef')
|
||||
|
||||
Check the full example `here <https://github.com/pythad/nider/blob/master/examples/draw_on_bg_example/script.py>`_
|
||||
Check the full example `here <https://github.com/pythad/nider/blob/master/examples/draw_on_bg_example/script.py>`_ .
|
||||
|
||||
``Image.draw_on_image``
|
||||
=========================
|
||||
|
||||
.. method:: draw_on_image(image_path)
|
||||
|
||||
Draws preinitiated image and its attributes on an image. Image size will be changed to the size of provided image.
|
||||
|
||||
:param str image_path: Path of the image to draw on
|
||||
:param itarable image_enhancements: itarable of tuples, each containing a class from ``PIL.ImageEnhance`` that will be applied and factor - a floating point value controlling the enhancement. Check `documentation <http://pillow.readthedocs.io/en/latest/reference/ImageEnhance.html>`_ of ``PIL.ImageEnhance`` for more info about availabe enhancements
|
||||
:param itarable image_filters: itarable of filters from ``PIL.ImageFilter`` that will be applied. Check `documentation <http://pillow.readthedocs.io/en/latest/reference/ImageFilter.html>`_ of ``PIL.ImageFilter`` for more info about availabe filters
|
||||
|
||||
:raises FileNotFoundError: if the file at ``image_path`` cannot be found
|
||||
.. automethod:: nider.models.Image.draw_on_image
|
||||
|
||||
Examples
|
||||
--------
|
||||
@@ -391,7 +286,7 @@ Using filters and enhancements:
|
||||
image_filters=((ImageFilter.BLUR),),
|
||||
)
|
||||
|
||||
Check the full example `here <https://github.com/pythad/nider/blob/master/examples/draw_on_image_example/script.py>`_
|
||||
Check the full example `here <https://github.com/pythad/nider/blob/master/examples/draw_on_image_example/script.py>`_ .
|
||||
|
||||
============
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ from nider.colors.utils import color_to_rgb
|
||||
class Font:
|
||||
'''Base class for text's font
|
||||
|
||||
Attributes:
|
||||
Args:
|
||||
path (str): path to the font used in the object.
|
||||
size (int): size of the font.
|
||||
|
||||
@@ -32,12 +32,11 @@ class Font:
|
||||
class Outline:
|
||||
'''Base class for text's outline
|
||||
|
||||
Attributes:
|
||||
Args:
|
||||
width (int): width of the stroke.
|
||||
color (str): string that represents a color. Must be compatible with PIL.ImageColor color names.
|
||||
|
||||
.. warning::
|
||||
color (str): string that represents outline color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_.
|
||||
|
||||
Warning:
|
||||
Due to PIL limitations - core library used for drawing, nider doesn't support 'true' outlineі. That is why high width outlines will look rather ugly and we don't recommend usign outlines with width > 3.
|
||||
'''
|
||||
|
||||
@@ -51,14 +50,7 @@ class Outline:
|
||||
|
||||
|
||||
class Text:
|
||||
'''Base class for the text
|
||||
|
||||
Attributes:
|
||||
text (str): text used in the object.
|
||||
font (nider.core.Font): nider.core.Font object that represents text's font.
|
||||
color (str): string that represents a color. Must be compatible with PIL.ImageColor.
|
||||
outline (nider.core.Outline): nider.core.Outline object that represents text's outline.
|
||||
'''
|
||||
'''Base class for the text'''
|
||||
|
||||
def __init__(self, text, font, color, outline):
|
||||
self._set_text(text)
|
||||
@@ -86,11 +78,23 @@ class MultilineText(MultilineTextMixin, Text):
|
||||
|
||||
|
||||
class SingleLineTextUnit(AlignMixin, Text):
|
||||
'''Base class for the single line text unit'''
|
||||
'''
|
||||
Args:
|
||||
text (str): text used for the unit.
|
||||
font (nider.core.Font): font object that represents text's font.
|
||||
color (str): string that represents a color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_.
|
||||
outline (nider.core.Outline): outline object that represents text's outline.
|
||||
align ('left' or 'center' or 'right'): side with respect to which the text will be aligned.
|
||||
|
||||
Raises:
|
||||
nider.exceptions.InvalidAlignException: if ``align` is not supported by nider.
|
||||
nider.exceptions.DefaultFontWarning: if ``font.path`` is ``None``.
|
||||
nider.exceptions.FontNotFoundWarning: if ``font.path`` does not exist.
|
||||
'''
|
||||
|
||||
def __init__(self, text, font=None,
|
||||
align='right',
|
||||
color=None, outline=None
|
||||
color=None, outline=None,
|
||||
align='right'
|
||||
):
|
||||
if font is None:
|
||||
font = Font()
|
||||
@@ -107,7 +111,22 @@ class SingleLineTextUnit(AlignMixin, Text):
|
||||
|
||||
|
||||
class MultilineTextUnit(AlignMixin, MultilineText):
|
||||
'''Base class for the multiline text unit'''
|
||||
'''
|
||||
Args:
|
||||
text (str): text used for the unit.
|
||||
font (nider.core.Font): font object that represents text's font.
|
||||
text_width (int): units's text width - number of characters in a line.
|
||||
line_padding (int): unit's line padding - padding (in pixels) between the lines.
|
||||
color (str): string that represents a color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_.
|
||||
outline (nider.core.Outline): outline object that represents text's outline.
|
||||
align ('left' or 'center' or 'right'): side with respect to which the text will be aligned.
|
||||
|
||||
Raises:
|
||||
nider.exceptions.InvalidAlignException: if ``align` is not supported by nider.
|
||||
AttributeError: if ``text_width`` < 0.
|
||||
nider.exceptions.DefaultFontWarning: if ``font.path`` is ``None``.
|
||||
nider.exceptions.FontNotFoundWarning: if ``font.path`` does not exist.
|
||||
'''
|
||||
|
||||
def __init__(self, text,
|
||||
font=None,
|
||||
@@ -128,7 +147,7 @@ class MultilineTextUnit(AlignMixin, MultilineText):
|
||||
'''Sets a unit used in the image
|
||||
|
||||
Sets textwraped unit's text that will be used in the image and also
|
||||
attachs header height to the obj instance
|
||||
attachs header height to the obj instance.
|
||||
'''
|
||||
self.wrapped_lines = textwrap.wrap(self.text, width=self.text_width)
|
||||
self._set_height()
|
||||
@@ -136,7 +155,7 @@ class MultilineTextUnit(AlignMixin, MultilineText):
|
||||
def _set_height(self):
|
||||
'''Sets unit's height used in the image
|
||||
|
||||
Calculates unit's height by adding height of each line and its padding
|
||||
Calculates unit's height by adding height of each line and its padding.
|
||||
'''
|
||||
self.height = 0
|
||||
for line in self.wrapped_lines:
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
class ImageGeneratorException(Exception):
|
||||
pass
|
||||
'''Base class for exceptions raised by nider'''
|
||||
|
||||
|
||||
class ImageGeneratorWarning(Warning):
|
||||
pass
|
||||
'''Base class for warnings raised by nider'''
|
||||
|
||||
|
||||
class InvalidAlignException(ImageGeneratorException):
|
||||
'''Exception raised when align is not supported by nider'''
|
||||
|
||||
def __init__(self, align_provided, available_aligns=None):
|
||||
if available_aligns is None:
|
||||
available_aligns = ['center', 'right', 'left']
|
||||
@@ -16,30 +18,40 @@ class InvalidAlignException(ImageGeneratorException):
|
||||
|
||||
|
||||
class FontNotFoundWarning(ImageGeneratorWarning):
|
||||
'''Warning raised when font cannot be found'''
|
||||
|
||||
def __init__(self, fontpath_provided):
|
||||
super().__init__(
|
||||
"Font {} hasn't been found. Default font has been set instead".format(fontpath_provided))
|
||||
|
||||
|
||||
class DefaultFontWarning(ImageGeneratorWarning):
|
||||
'''Warning raised when default font was used'''
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
"Font hasn't been provided. Default font has been set instead")
|
||||
|
||||
|
||||
class ImageSizeFixedWarning(ImageGeneratorWarning):
|
||||
'''Warning raised when the size of the image has to be adjusted to the provided content's size because the content takes much space'''
|
||||
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
"Image size has been adjusted to the provided content size because the content took too much space")
|
||||
|
||||
|
||||
class AutoGeneratedUnitColorUsedWarning(ImageGeneratorWarning):
|
||||
'''Warning raised when auto generated unit color was used'''
|
||||
|
||||
def __init__(self, unit, color_used):
|
||||
super().__init__(
|
||||
"You didn't provide a color for {}. Auto generated one('{}') has been used".format(unit.__class__.__name__, color_used))
|
||||
|
||||
|
||||
class AutoGeneratedUnitOutlinecolorUsedWarning(ImageGeneratorWarning):
|
||||
'''Warning raised when auto generated unit's outline color was used'''
|
||||
|
||||
def __init__(self, unit, color_used):
|
||||
super().__init__(
|
||||
"You didn't provide an outline color for {}. Auto generated one('{}') has been used".format(unit.__class__.__name__, color_used))
|
||||
|
||||
103
nider/models.py
103
nider/models.py
@@ -24,23 +24,38 @@ from nider.exceptions import AutoGeneratedUnitOutlinecolorUsedWarning
|
||||
|
||||
|
||||
class Header(MultilineTextUnit):
|
||||
'''Base class for the header unit'''
|
||||
__doc__ = '''Class that represents a header used in images''' + \
|
||||
'\n\n' + MultilineTextUnit.__doc__
|
||||
|
||||
|
||||
class Paragraph(MultilineTextUnit):
|
||||
'''Base class for the paragraph unit'''
|
||||
__doc__ = '''Class that represents a paragraph used in images''' + \
|
||||
'\n\n' + MultilineTextUnit.__doc__
|
||||
|
||||
|
||||
class Linkback(SingleLineTextUnit):
|
||||
'''Class that represents a linkback used in images
|
||||
|
||||
Attributes:
|
||||
bottom_padding (int): padding to step back from the bottom of the image.
|
||||
Args:
|
||||
text (str): text used for the unit.
|
||||
font (nider.core.Font): font object that represents text's font.
|
||||
color (str): string that represents a color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_
|
||||
outline (nider.core.Outline): outline object that represents text's outline.
|
||||
align ('left' or 'center' or 'right'): side with respect to which the text will be aligned.
|
||||
bottom_padding (int): linkback’s bottom padding - padding (in pixels) between the bottom of the image and the linkback itself.
|
||||
|
||||
Raises:
|
||||
nider.exceptions.InvalidAlignException: if ``align` is not supported by nider.
|
||||
nider.exceptions.DefaultFontWarning: if ``font.path`` is ``None``.
|
||||
nider.exceptions.FontNotFoundWarning: if ``font.path`` does not exist.
|
||||
'''
|
||||
|
||||
def __init__(self, text, bottom_padding=20, *args, **kwargs):
|
||||
def __init__(self, text, font=None,
|
||||
color=None, outline=None,
|
||||
align='right', bottom_padding=20):
|
||||
self.bottom_padding = bottom_padding
|
||||
super().__init__(text=text, *args, **kwargs)
|
||||
super().__init__(text=text, font=font, color=color, outline=outline,
|
||||
align=align)
|
||||
|
||||
def _set_height(self):
|
||||
'''Sets linkback\'s height'''
|
||||
@@ -49,7 +64,23 @@ class Linkback(SingleLineTextUnit):
|
||||
|
||||
|
||||
class Content:
|
||||
'''Class that aggregates different units into a sigle object'''
|
||||
'''Class that aggregates different units into a sigle object
|
||||
|
||||
Args:
|
||||
paragraph (nider.models.Paragraph): paragraph used for in the content.
|
||||
header (nider.models.Header): header used for in the content.
|
||||
linkback (nider.models.Linkback): linkback used for in the content.
|
||||
padding (int): content's padding - padding (in pixels) between the units.
|
||||
|
||||
Raises:
|
||||
nider.exceptions.ImageGeneratorException: if neither of ``paragraph``, ``header`` or ``linkback`` is provided.
|
||||
|
||||
Note:
|
||||
``padding`` is taken into account only if image is to get resized. If size allows content to fit freely, pre-calculated paddings will be used.
|
||||
|
||||
Note:
|
||||
Content has to consist at least of one unit: header, paragraph or linkback.
|
||||
'''
|
||||
# Variable to check if the content fits into the img. Default is true,
|
||||
# but it may changed by in Img._fix_image_size()
|
||||
fits = True
|
||||
@@ -83,13 +114,17 @@ class Content:
|
||||
class Image:
|
||||
'''Base class for a text based image
|
||||
|
||||
Attributes:
|
||||
content (nider.models.Content): object that has units to be rendered.
|
||||
Args:
|
||||
content (nider.models.Content): content object that has units to be rendered.
|
||||
fullpath (str): path where the image has to be saved.
|
||||
width (int): width of the image.
|
||||
height (int): height of the image.
|
||||
title (str): title of the image. Serves as metadata for latter rendering in html. May be used as alt text of the image. If no title is provided content.header.text will be set as the value.
|
||||
description (str): description of the image. Serves as metadata for latter rendering in html. May be used as description text of the image. If no description is provided content.paragraph.text will be set as the value.
|
||||
width (int): width of the image to be generated.
|
||||
height (int): height of the image to be generated.
|
||||
title (str): title of the image. Serves as metadata for latter rendering in html. May be used as alt text of the image. If no title is provided ``content.header.text`` will be set as the value.
|
||||
description (str): description of the image. Serves as metadata for latter rendering in html. May be used as description text of the image. If no description is provided ``content.paragraph.text`` will be set as the value.
|
||||
|
||||
Raises:
|
||||
nider.exceptions.ImageGeneratorException: if the current user has sufficient permissions to create the file at passed ``fullpath``.
|
||||
AttributeError: if width <= 0 or height <= 0.
|
||||
'''
|
||||
|
||||
def __init__(self, content, fullpath, width=1080, height=1080, title=None, description=None):
|
||||
@@ -102,11 +137,14 @@ class Image:
|
||||
def draw_on_texture(self, texture_path=None):
|
||||
'''Draws preinitialized image and its attributes on a texture
|
||||
|
||||
Draws preinitiated image and its attributes on a texture. If texture_path
|
||||
is set to None, takes random textures from textures/
|
||||
If ``texture_path`` is set to ``None``, random texture from ``nider/textures`` will be taken.
|
||||
|
||||
Attributes:
|
||||
Args:
|
||||
texture_path (str): path of the texture to use.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: if texture file at path ``texture_path`` cannot be found.
|
||||
nider.exceptions.ImageSizeFixedWarning: if the image size has to be adjusted to the provided content's size because the content takes too much space.
|
||||
'''
|
||||
if texture_path is None:
|
||||
texture_path = get_random_texture()
|
||||
@@ -126,11 +164,13 @@ class Image:
|
||||
def draw_on_bg(self, bgcolor=None):
|
||||
'''Draws preinitialized image and its attributes on a colored background
|
||||
|
||||
Draws preinitiated image and its attributes on a colored background. If bgcolor
|
||||
is set to None, random nider.colors.colormap.FLAT_UI color is generated
|
||||
If ``bgcolor`` is set to ``None``, random ``nider.colors.colormap.FLAT_UI_COLORS`` will be taken.
|
||||
|
||||
Attributes:
|
||||
bgcolor (str, tuple): either hex or rgb representation of background color.
|
||||
Args:
|
||||
bgcolor (str): string that represents a background color. Must be compatible with `PIL.ImageColor <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html>`_ `color names <http://pillow.readthedocs.io/en/latest/reference/ImageColor.html#color-names>`_
|
||||
|
||||
Raises:
|
||||
nider.exceptions.ImageSizeFixedWarning: if the image size has to be adjusted to the provided content's size because the content takes too much space.
|
||||
'''
|
||||
self.bgcolor = color_to_rgb(
|
||||
bgcolor) if bgcolor else get_random_bgcolor()
|
||||
@@ -145,10 +185,13 @@ class Image:
|
||||
def draw_on_image(self, image_path, image_enhancements=None, image_filters=None):
|
||||
'''Draws preinitialized image and its attributes on an image
|
||||
|
||||
Attributes:
|
||||
Args:
|
||||
image_path (str): path of the image to draw on.
|
||||
image_enhancements (itarable): itarable of tuples, each containing a class from PIL.ImageEnhance that will be applied and factor - a floating point value controlling the enhancement. Check docs of PIL.ImageEnhance for more info.
|
||||
image_filters (itarable): itarable of filters from PIL.ImageFilter that will be applied. Check docs of PIL.ImageFilter for more info.
|
||||
image_enhancements (itarable): itarable of tuples, each containing a class from ``PIL.ImageEnhance`` that will be applied and factor - a floating point value controlling the enhancement. Check `documentation <http://pillow.readthedocs.io/en/latest/reference/ImageEnhance.html>`_ of ``PIL.ImageEnhance`` for more info about availabe enhancements.
|
||||
image_filters (itarable): itarable of filters from ``PIL.ImageFilter`` that will be applied. Check `documentation <http://pillow.readthedocs.io/en/latest/reference/ImageFilter.html>`_ of ``PIL.ImageFilter`` for more info about availabe filters.
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: if image file at path ``image_path`` cannot be found.
|
||||
'''
|
||||
if not os.path.isfile(image_path):
|
||||
raise FileNotFoundError(
|
||||
@@ -187,7 +230,7 @@ class Image:
|
||||
if is_path_creatable(fullpath):
|
||||
self.fullpath = fullpath
|
||||
else:
|
||||
raise AttributeError(
|
||||
raise ImageGeneratorException(
|
||||
"Is seems impossible to create a file in path {}".format(fullpath))
|
||||
|
||||
def _set_image_size(self, width, height):
|
||||
@@ -364,7 +407,7 @@ class Image:
|
||||
|
||||
|
||||
class FacebookSquarePost(Image):
|
||||
'''Alias of models.Image with width=470 and height=470'''
|
||||
'''Alias of :class:`nider.models.Image` with ``width=470`` and ``height=470``'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['width'] = kwargs.get('width', 470)
|
||||
@@ -373,7 +416,7 @@ class FacebookSquarePost(Image):
|
||||
|
||||
|
||||
class FacebookLandscapePost(Image):
|
||||
'''Alias of models.Image with width=1024 and height=512'''
|
||||
'''Alias of :class:`nider.models.Image` with ``width=1024`` and ``height=512``'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['width'] = kwargs.get('width', 1024)
|
||||
@@ -385,7 +428,7 @@ TwitterPost = FacebookLandscapePost
|
||||
|
||||
|
||||
class TwitterLargeCard(Image):
|
||||
'''Alias of models.Image with width=506 and height=506'''
|
||||
'''Alias of :class:`nider.models.Image` with ``width=506`` and ``height=506``'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['width'] = kwargs.get('width', 506)
|
||||
@@ -394,7 +437,7 @@ class TwitterLargeCard(Image):
|
||||
|
||||
|
||||
class InstagramSquarePost(Image):
|
||||
'''Alias of models.Image with width=1080 and height=1080'''
|
||||
'''Alias of :class:`nider.models.Image` with ``width=1080`` and ``height=1080``'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['width'] = kwargs.get('width', 1080)
|
||||
@@ -403,7 +446,7 @@ class InstagramSquarePost(Image):
|
||||
|
||||
|
||||
class InstagramPortraitPost(Image):
|
||||
'''Alias of models.Image with width=1080 and height=1350'''
|
||||
'''Alias of :class:`nider.models.Image` with ``width=1080`` and ``height=1350``'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['width'] = kwargs.get('width', 1080)
|
||||
@@ -412,7 +455,7 @@ class InstagramPortraitPost(Image):
|
||||
|
||||
|
||||
class InstagramLandscapePost(Image):
|
||||
'''Alias of models.Image with width=1080 and height=566'''
|
||||
'''Alias of :class:`nider.models.Image` with ``width=1080`` and ``height=566``'''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['width'] = kwargs.get('width', 1080)
|
||||
|
||||
@@ -14,11 +14,18 @@ from nider.exceptions import FontNotFoundWarning
|
||||
|
||||
|
||||
def get_font(fontfullpath, fontsize):
|
||||
'''Returns a PIL ImageFont object
|
||||
'''Function to create a truetype ``PIL.ImageFont`` that provides fallbacks for invalid arguments
|
||||
|
||||
Attributes:
|
||||
fontfullpath (str): path to the desired font
|
||||
fontsize (int): size of the font
|
||||
Args:
|
||||
fontfullpath (str): path to the desired font.
|
||||
fontsize (int): size of the font.
|
||||
|
||||
Returns:
|
||||
PIL.ImageFont: Default PIL ImageFont if ``fontfullpath`` is either unreachable or provided ``fontfullpath`` is ``None``.
|
||||
|
||||
Raises:
|
||||
nider.exceptions.DefaultFontWarning: if ``fontfullpath`` is ``None``.
|
||||
nider.exceptions.FontNotFoundWarning: if ``fontfullpath`` does not exist.
|
||||
'''
|
||||
if fontfullpath is None:
|
||||
warnings.warn(DefaultFontWarning())
|
||||
@@ -30,9 +37,14 @@ def get_font(fontfullpath, fontsize):
|
||||
|
||||
|
||||
def is_path_creatable(pathname):
|
||||
'''
|
||||
`True` if the current user has sufficient permissions to create the passed
|
||||
pathname; `False` otherwise.
|
||||
'''Function to check if the current user has sufficient permissions to create the passed
|
||||
pathname
|
||||
|
||||
Args:
|
||||
pathname (str): path to check.
|
||||
|
||||
Returns:
|
||||
bool: ``True`` if the current user has sufficient permissions to create the passed ``pathname``. ``False`` otherwise.
|
||||
'''
|
||||
# Parent directory of the passed path. If empty, we substitute the current
|
||||
# working directory (CWD) instead.
|
||||
@@ -42,6 +54,7 @@ def is_path_creatable(pathname):
|
||||
|
||||
@contextmanager
|
||||
def create_test_image():
|
||||
'''Context manager to yield a ``PIL.Image``'''
|
||||
try:
|
||||
image = Image.new('RGBA', size=(50, 50), color=(155, 0, 0))
|
||||
image.save('test.png')
|
||||
@@ -51,7 +64,7 @@ def create_test_image():
|
||||
|
||||
|
||||
def get_random_texture():
|
||||
'''Returns the path to a random texture from the local /textures folder'''
|
||||
'''Returns the path to a random texture from the local ``nider/textures`` folder'''
|
||||
textures_folder = os.path.dirname(
|
||||
os.path.realpath(__file__)) + '/textures'
|
||||
texture = random.choice(os.listdir(textures_folder))
|
||||
@@ -60,5 +73,5 @@ def get_random_texture():
|
||||
|
||||
|
||||
def get_random_bgcolor():
|
||||
'''Returns random FLAT_UI_COLOR'''
|
||||
'''Returns random flat ui color from ``nider.colors.colormap.FLAT_UI_COLORS``'''
|
||||
return random.choice(list(FLAT_UI_COLORS.values()))
|
||||
|
||||
@@ -9,7 +9,6 @@ from PIL import ImageDraw
|
||||
from PIL import ImageEnhance
|
||||
from PIL import ImageFilter
|
||||
|
||||
from nider.core import Font
|
||||
from nider.core import Outline
|
||||
from nider.core import MultilineTextUnit
|
||||
|
||||
@@ -108,7 +107,7 @@ class TestImageInitializationMethods(unittest.TestCase):
|
||||
|
||||
def test_set_fullpath_with_invalid_path(self):
|
||||
fullpath = 'non/existent/directory/test.png'
|
||||
with self.assertRaises(AttributeError):
|
||||
with self.assertRaises(ImageGeneratorException):
|
||||
Image._set_fullpath(self.image_mock, fullpath)
|
||||
self.assertNotEqual(self.image_mock.fullpath, fullpath)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user