python-resize-image


Namepython-resize-image JSON
Version 1.1.20 PyPI version JSON
download
home_pagehttps://github.com/VingtCinq/python-resize-image
SummaryA Small python package to easily resize images
upload_time2021-11-04 08:40:07
maintainer
docs_urlNone
authorCharles TISSIER
requires_python
licenseMIT
keywords image resize resizing python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            |python-resize-image v1.1.20 on PyPi| |MIT license| |Stable|

A python package to easily resize images
========================================

This package provides function for easily resizing images.

Dependencies
------------

-  Pillow 2.7++
-  Python 2.7/3.4

Introduction
------------

The following functions are supported:

-  ``resize_crop`` crop the image with a centered rectangle of the
   specified size.
-  ``resize_cover`` resize the image to fill the specified area, crop as
   needed (same behavior as ``background-size: cover``).
-  ``resize_contain`` resize the image so that it can fit in the
   specified area, keeping the ratio and without crop (same behavior as
   ``background-size: contain``).
-  ``resize_height`` resize the image to the specified height adjusting
   width to keep the ratio the same.
-  ``resize_width`` resize the image to the specified width adjusting
   height to keep the ratio the same.
-  ``resize_thumbnail`` resize image while keeping the ratio trying its
   best to match the specified size.

Installation
------------

Install python-resize-image using pip:

::

    pip install python-resize-image

Usage
-----

python-resize-image takes as first argument a ``PIL.Image`` and then
``size`` argument which can be a single integer or tuple of two
integers.

In the following example, we open an image, *crop* it and save as new
file:

.. code:: python

    from PIL import Image

    from resizeimage import resizeimage


    with open('test-image.jpeg', 'r+b') as f:
        with Image.open(f) as image:
            cover = resizeimage.resize_cover(image, [200, 100])
            cover.save('test-image-cover.jpeg', image.format)

Before resizing, python-image-resize will check whether the operation
can be done. A resize is considered valid if it doesn’t require to
increase one of the dimension. To avoid the test add ``validate=False``
as argument:

.. code:: python

    cover = resizeimage.resize_cover(image, [200, 100], validate=False)

You can also create a two step process validation then processing using
``validate`` function attached to resized function which allows to test
the viability of the resize without doing it just after validation.
``validate`` is available using the dot ``.`` operator on every resize
function e.g. ``resize_cover.validate``.

The first exemple is rewritten in the following snippet to use this
feature:

.. code:: python

    from PIL import Image

    from resizeimage import resizeimage

    with open('test-image.jpeg', 'r+b')
        with Image.open() as image:
            is_valid = resizeimage.resize_cover.validate(image, [200, 100])

    # do something else...

    if is_valid:
        with Image.open('test-image.jpeg') as image:
            resizeimage.resize_cover.validate(image, [200, 100], validate=False)
            cover = resizeimage.resize_cover(image, [200, 100])
            cover.save('test-image-cover.jpeg', image.format)

Mind the fact that it’s useless to validate the image twice, so we pass
``validate=False`` to ``resize_cover.validate``.

API Reference
-------------

``resize_crop(image, size, validate=True)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Crop the image with a centered rectangle of the specified size.

Crop an image with a 200x200 cented square:

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize_crop(img, [200, 200])
    img.save('test-image-crop.jpeg', img.format)
    fd_img.close()

``resize_cover(image, size, validate=True, resample=Image.LANCZOS)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resize the image to fill the specified area, crop as needed. It’s the
same behavior as css ``background-size: cover`` property.

Resize and crop (from center) the image so that it covers a 200x100
rectangle.

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize_cover(img, [200, 100])
    img.save('test-image-cover.jpeg', img.format)
    fd_img.close()

``resize_contain(image, size, validate=True, resample=Image.LANCZOS, bg_color=(255, 255, 255, 0))``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resize the image so that it can fit in the specified area, keeping the
ratio and without crop. It’s the same behavior as css
``background-size: contain`` property. A white a background border is
created.

Resize the image to minimum so that it is contained in a 200x100
rectangle is the ratio between source and destination image.

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize_contain(img, [200, 100])
    img.save('test-image-contain.jpeg', img.format)
    fd_img.close()

``resize_width(image, width, validate=True, resample=Image.LANCZOS)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resize the image to the specified height adjusting width to keep the
ratio the same.

Resize the image to be 200px width:

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize_width(img, 200)
    img.save('test-image-width.jpeg', img.format)
    fd_img.close()

``resize_height(image, height, validate=True, resample=Image.LANCZOS)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resize the image to the specified width adjusting height to keep the
ratio the same.

Resize the image to be 200px height:

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize_height(img, 200)
    img.save('test-image-height.jpeg', img.format)
    fd_img.close()

``resize_thumbnail(image, size, validate=True, resample=Image.LANCZOS)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resize image while keeping the ratio trying its best to match the
specified size.

Resize the image to be contained in a 200px square:

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize_thumbnail(img, [200, 200])
    img.save('test-image-thumbnail.jpeg', img.format)
    fd_img.close()

``resize(method, *args, **kwargs)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Resize Image with the specified method : ‘crop’, ‘cover’, ‘contain’,
‘width’, ‘height’ or ‘thumbnail’.

.. code:: python

    from PIL import Image
    from resizeimage import resizeimage

    fd_img = open('test-image.jpeg', 'r')
    img = Image.open(fd_img)
    img = resizeimage.resize('thumbnail', img, [200, 200])
    img.save('test-image-thumbnail.jpeg', img.format)
    fd_img.close()

Tests
-----

::

    pip install -r requirements.dev.txt
    pip install -e .
    python setup.py test

Contribute
----------

python-resize-image is hosted at
`github.com/VingtCinq/python-resize-image/ <https://github.com/VingtCinq/python-resize-image>`__.

Before coding install ``pre-commit`` as git hook using the following
command:

::

    cp pre-commit .git/hooks/

And install the hook and pylint:

::

    pip install git-pylint-commit-hook pylint

If you want to force a commit (you need a good reason to do that) use
``commit`` with the ``-n`` option e.g. ``git commit -n``.

Support
-------

If you are having issues, please let us know.

License
-------

The project is licensed under the MIT License.

.. |python-resize-image v1.1.20 on PyPi| image:: https://img.shields.io/badge/pypi-1.1.20-green.svg
   :target: https://pypi.python.org/pypi/python-resize-image
.. |MIT license| image:: https://img.shields.io/badge/licence-MIT-blue.svg
.. |Stable| image:: https://img.shields.io/badge/status-stable-green.svg




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/VingtCinq/python-resize-image",
    "name": "python-resize-image",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "image resize resizing python",
    "author": "Charles TISSIER",
    "author_email": "charles@vingtcinq.io",
    "download_url": "https://files.pythonhosted.org/packages/ea/5e/f84b810b290cfd5fb72ffbdd96046f97871db09d26bfcff0353a60cde569/python-resize-image-1.1.20.tar.gz",
    "platform": "",
    "description": "|python-resize-image v1.1.20 on PyPi| |MIT license| |Stable|\n\nA python package to easily resize images\n========================================\n\nThis package provides function for easily resizing images.\n\nDependencies\n------------\n\n-  Pillow 2.7++\n-  Python 2.7/3.4\n\nIntroduction\n------------\n\nThe following functions are supported:\n\n-  ``resize_crop`` crop the image with a centered rectangle of the\n   specified size.\n-  ``resize_cover`` resize the image to fill the specified area, crop as\n   needed (same behavior as ``background-size: cover``).\n-  ``resize_contain`` resize the image so that it can fit in the\n   specified area, keeping the ratio and without crop (same behavior as\n   ``background-size: contain``).\n-  ``resize_height`` resize the image to the specified height adjusting\n   width to keep the ratio the same.\n-  ``resize_width`` resize the image to the specified width adjusting\n   height to keep the ratio the same.\n-  ``resize_thumbnail`` resize image while keeping the ratio trying its\n   best to match the specified size.\n\nInstallation\n------------\n\nInstall python-resize-image using pip:\n\n::\n\n    pip install python-resize-image\n\nUsage\n-----\n\npython-resize-image takes as first argument a ``PIL.Image`` and then\n``size`` argument which can be a single integer or tuple of two\nintegers.\n\nIn the following example, we open an image, *crop* it and save as new\nfile:\n\n.. code:: python\n\n    from PIL import Image\n\n    from resizeimage import resizeimage\n\n\n    with open('test-image.jpeg', 'r+b') as f:\n        with Image.open(f) as image:\n            cover = resizeimage.resize_cover(image, [200, 100])\n            cover.save('test-image-cover.jpeg', image.format)\n\nBefore resizing, python-image-resize will check whether the operation\ncan be done. A resize is considered valid if it doesn\u2019t require to\nincrease one of the dimension. To avoid the test add ``validate=False``\nas argument:\n\n.. code:: python\n\n    cover = resizeimage.resize_cover(image, [200, 100], validate=False)\n\nYou can also create a two step process validation then processing using\n``validate`` function attached to resized function which allows to test\nthe viability of the resize without doing it just after validation.\n``validate`` is available using the dot ``.`` operator on every resize\nfunction e.g. ``resize_cover.validate``.\n\nThe first exemple is rewritten in the following snippet to use this\nfeature:\n\n.. code:: python\n\n    from PIL import Image\n\n    from resizeimage import resizeimage\n\n    with open('test-image.jpeg', 'r+b')\n        with Image.open() as image:\n            is_valid = resizeimage.resize_cover.validate(image, [200, 100])\n\n    # do something else...\n\n    if is_valid:\n        with Image.open('test-image.jpeg') as image:\n            resizeimage.resize_cover.validate(image, [200, 100], validate=False)\n            cover = resizeimage.resize_cover(image, [200, 100])\n            cover.save('test-image-cover.jpeg', image.format)\n\nMind the fact that it\u2019s useless to validate the image twice, so we pass\n``validate=False`` to ``resize_cover.validate``.\n\nAPI Reference\n-------------\n\n``resize_crop(image, size, validate=True)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nCrop the image with a centered rectangle of the specified size.\n\nCrop an image with a 200x200 cented square:\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize_crop(img, [200, 200])\n    img.save('test-image-crop.jpeg', img.format)\n    fd_img.close()\n\n``resize_cover(image, size, validate=True, resample=Image.LANCZOS)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nResize the image to fill the specified area, crop as needed. It\u2019s the\nsame behavior as css ``background-size: cover`` property.\n\nResize and crop (from center) the image so that it covers a 200x100\nrectangle.\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize_cover(img, [200, 100])\n    img.save('test-image-cover.jpeg', img.format)\n    fd_img.close()\n\n``resize_contain(image, size, validate=True, resample=Image.LANCZOS, bg_color=(255, 255, 255, 0))``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nResize the image so that it can fit in the specified area, keeping the\nratio and without crop. It\u2019s the same behavior as css\n``background-size: contain`` property. A white a background border is\ncreated.\n\nResize the image to minimum so that it is contained in a 200x100\nrectangle is the ratio between source and destination image.\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize_contain(img, [200, 100])\n    img.save('test-image-contain.jpeg', img.format)\n    fd_img.close()\n\n``resize_width(image, width, validate=True, resample=Image.LANCZOS)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nResize the image to the specified height adjusting width to keep the\nratio the same.\n\nResize the image to be 200px width:\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize_width(img, 200)\n    img.save('test-image-width.jpeg', img.format)\n    fd_img.close()\n\n``resize_height(image, height, validate=True, resample=Image.LANCZOS)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nResize the image to the specified width adjusting height to keep the\nratio the same.\n\nResize the image to be 200px height:\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize_height(img, 200)\n    img.save('test-image-height.jpeg', img.format)\n    fd_img.close()\n\n``resize_thumbnail(image, size, validate=True, resample=Image.LANCZOS)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nResize image while keeping the ratio trying its best to match the\nspecified size.\n\nResize the image to be contained in a 200px square:\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize_thumbnail(img, [200, 200])\n    img.save('test-image-thumbnail.jpeg', img.format)\n    fd_img.close()\n\n``resize(method, *args, **kwargs)``\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nResize Image with the specified method : \u2018crop\u2019, \u2018cover\u2019, \u2018contain\u2019,\n\u2018width\u2019, \u2018height\u2019 or \u2018thumbnail\u2019.\n\n.. code:: python\n\n    from PIL import Image\n    from resizeimage import resizeimage\n\n    fd_img = open('test-image.jpeg', 'r')\n    img = Image.open(fd_img)\n    img = resizeimage.resize('thumbnail', img, [200, 200])\n    img.save('test-image-thumbnail.jpeg', img.format)\n    fd_img.close()\n\nTests\n-----\n\n::\n\n    pip install -r requirements.dev.txt\n    pip install -e .\n    python setup.py test\n\nContribute\n----------\n\npython-resize-image is hosted at\n`github.com/VingtCinq/python-resize-image/ <https://github.com/VingtCinq/python-resize-image>`__.\n\nBefore coding install ``pre-commit`` as git hook using the following\ncommand:\n\n::\n\n    cp pre-commit .git/hooks/\n\nAnd install the hook and pylint:\n\n::\n\n    pip install git-pylint-commit-hook pylint\n\nIf you want to force a commit (you need a good reason to do that) use\n``commit`` with the ``-n`` option e.g. ``git commit -n``.\n\nSupport\n-------\n\nIf you are having issues, please let us know.\n\nLicense\n-------\n\nThe project is licensed under the MIT License.\n\n.. |python-resize-image v1.1.20 on PyPi| image:: https://img.shields.io/badge/pypi-1.1.20-green.svg\n   :target: https://pypi.python.org/pypi/python-resize-image\n.. |MIT license| image:: https://img.shields.io/badge/licence-MIT-blue.svg\n.. |Stable| image:: https://img.shields.io/badge/status-stable-green.svg\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A Small python package to easily resize images",
    "version": "1.1.20",
    "split_keywords": [
        "image",
        "resize",
        "resizing",
        "python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "adb343c3560bc10fe16824f411eb539f",
                "sha256": "0977bcf8454949b2d37e56c66d95494146c4d9da295a8f7af8eff43d2c35ca6a"
            },
            "downloads": -1,
            "filename": "python_resize_image-1.1.20-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "adb343c3560bc10fe16824f411eb539f",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 8405,
            "upload_time": "2021-11-04T08:40:06",
            "upload_time_iso_8601": "2021-11-04T08:40:06.252463Z",
            "url": "https://files.pythonhosted.org/packages/67/44/7fb293646ed21bab5cfe8d17ec5524214cc51cb9c3ee186704f844583e57/python_resize_image-1.1.20-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "6f1dfa83c3e718ceb7dbda6326541ac5",
                "sha256": "b055dab919d623ece8ec95262d4bdbf006cb1a10e818e9b36221c8b1885f9922"
            },
            "downloads": -1,
            "filename": "python-resize-image-1.1.20.tar.gz",
            "has_sig": false,
            "md5_digest": "6f1dfa83c3e718ceb7dbda6326541ac5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6940,
            "upload_time": "2021-11-04T08:40:07",
            "upload_time_iso_8601": "2021-11-04T08:40:07.795965Z",
            "url": "https://files.pythonhosted.org/packages/ea/5e/f84b810b290cfd5fb72ffbdd96046f97871db09d26bfcff0353a60cde569/python-resize-image-1.1.20.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-11-04 08:40:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "VingtCinq",
    "github_project": "python-resize-image",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "python-resize-image"
}
        
Elapsed time: 0.01580s