# patchify
patchfy can split images into small overlappable patches by given patch cell size, and merge patches into original image.
This library provides two functions: `patchify`, `unpatchify`.
## Installation
```
pip install patchify
```
## Usage
### Split image to patches
`patchify(image_to_patch, patch_shape, step=1)`
2D image:
```python
#This will split the image into small images of shape [3,3]
patches = patchify(image, (3, 3), step=1)
```
3D image:
```python
#This will split the image into small images of shape [3,3,3]
patches = patchify(image, (3, 3, 3), step=1)
```
### Merge patches into original image
`unpatchify(patches_to_merge, merged_image_size)`
```python
reconstructed_image = unpatchify(patches, image.shape)
```
This will reconstruct the original image that was patchified in previous code.
**Caveat**: in order for `unpatchify` to work, you need to create patchies with equal step size. e.g. if the original image has width 3 and the patch has width 2, you cannot really create equal step size patches with step size 2. (first patch [elem0, elem1] and second patch [elem2, elem3], which is out of bound).
The required condition for unpatchify to success is to have (width - patch_width) mod step_size = 0.
### Full running examples
#### 2D image patchify and merge
```python
import numpy as np
from patchify import patchify, unpatchify
image = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
patches = patchify(image, (2,2), step=1) # split image into 2*3 small 2*2 patches.
assert patches.shape == (2, 3, 2, 2)
reconstructed_image = unpatchify(patches, image.shape)
assert (reconstructed_image == image).all()
```
#### 3D image patchify and merge
```python
import numpy as np
from patchify import patchify, unpatchify
image = np.random.rand(512,512,3)
patches = patchify(image, (2,2,3), step=1) # patch shape [2,2,3]
print(patches.shape) # (511, 511, 1, 2, 2, 3). Total patches created: 511x511x1
assert patches.shape == (511, 511, 1, 2, 2, 3)
reconstructed_image = unpatchify(patches, image.shape)
print(reconstructed_image.shape) # (512, 512, 3)
assert (reconstructed_image == image).all()
```
Raw data
{
"_id": null,
"home_page": "https://github.com/dovahcrow/patchify.py",
"name": "patchify",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "",
"keywords": "patch,image,split",
"author": "Weiyuan Wu",
"author_email": "doomsplayer@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/79/b7/1b281a31d8fdbdfc44af2fcb7a0750158e160f3a8c608fcb602e62be24a8/patchify-0.2.3.tar.gz",
"platform": "",
"description": "# patchify\n\npatchfy can split images into small overlappable patches by given patch cell size, and merge patches into original image.\n\nThis library provides two functions: `patchify`, `unpatchify`.\n\n## Installation\n```\npip install patchify\n```\n\n## Usage\n\n### Split image to patches\n\n`patchify(image_to_patch, patch_shape, step=1)`\n\n2D image:\n```python\n#This will split the image into small images of shape [3,3]\npatches = patchify(image, (3, 3), step=1)\n```\n\n3D image:\n```python\n#This will split the image into small images of shape [3,3,3]\npatches = patchify(image, (3, 3, 3), step=1)\n```\n\n### Merge patches into original image\n\n`unpatchify(patches_to_merge, merged_image_size)`\n\n```python\nreconstructed_image = unpatchify(patches, image.shape)\n```\nThis will reconstruct the original image that was patchified in previous code.\n\n**Caveat**: in order for `unpatchify` to work, you need to create patchies with equal step size. e.g. if the original image has width 3 and the patch has width 2, you cannot really create equal step size patches with step size 2. (first patch [elem0, elem1] and second patch [elem2, elem3], which is out of bound).\n\nThe required condition for unpatchify to success is to have (width - patch_width) mod step_size = 0.\n\n### Full running examples\n\n#### 2D image patchify and merge\n\n```python\nimport numpy as np\nfrom patchify import patchify, unpatchify\n\nimage = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])\n\npatches = patchify(image, (2,2), step=1) # split image into 2*3 small 2*2 patches.\n\nassert patches.shape == (2, 3, 2, 2)\nreconstructed_image = unpatchify(patches, image.shape)\n\nassert (reconstructed_image == image).all()\n```\n\n#### 3D image patchify and merge\n\n```python\nimport numpy as np\nfrom patchify import patchify, unpatchify\n\nimage = np.random.rand(512,512,3)\n\npatches = patchify(image, (2,2,3), step=1) # patch shape [2,2,3]\nprint(patches.shape) # (511, 511, 1, 2, 2, 3). Total patches created: 511x511x1\n\nassert patches.shape == (511, 511, 1, 2, 2, 3)\nreconstructed_image = unpatchify(patches, image.shape)\nprint(reconstructed_image.shape) # (512, 512, 3)\n\nassert (reconstructed_image == image).all()\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A library that helps you split image into small, overlappable patches, and merge patches back into the original image.",
"version": "0.2.3",
"project_urls": {
"Homepage": "https://github.com/dovahcrow/patchify.py",
"Repository": "https://github.com/dovahcrow/patchify.py"
},
"split_keywords": [
"patch",
"image",
"split"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "00ced76647d95aad98d5532b5d25801dedda788c6b5d4c7457d30ae3612132af",
"md5": "8628c93c912938a9a38bcf6b5bc66bb3",
"sha256": "4bd4f80c83280b36c6968cb4d802bde28cd11446cc8ace94e0aa14f573fcf41b"
},
"downloads": -1,
"filename": "patchify-0.2.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8628c93c912938a9a38bcf6b5bc66bb3",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 6640,
"upload_time": "2021-03-14T20:05:42",
"upload_time_iso_8601": "2021-03-14T20:05:42.407114Z",
"url": "https://files.pythonhosted.org/packages/00/ce/d76647d95aad98d5532b5d25801dedda788c6b5d4c7457d30ae3612132af/patchify-0.2.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "79b71b281a31d8fdbdfc44af2fcb7a0750158e160f3a8c608fcb602e62be24a8",
"md5": "97b7e7a0d900446c6cbb0ea610e21596",
"sha256": "6cc409124f34ceee672f1931d818923f88f5116f323ac7bb9be7e6c5d0845502"
},
"downloads": -1,
"filename": "patchify-0.2.3.tar.gz",
"has_sig": false,
"md5_digest": "97b7e7a0d900446c6cbb0ea610e21596",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 5888,
"upload_time": "2021-03-14T20:05:43",
"upload_time_iso_8601": "2021-03-14T20:05:43.703545Z",
"url": "https://files.pythonhosted.org/packages/79/b7/1b281a31d8fdbdfc44af2fcb7a0750158e160f3a8c608fcb602e62be24a8/patchify-0.2.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-03-14 20:05:43",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dovahcrow",
"github_project": "patchify.py",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "patchify"
}