-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7fd5df
commit 74cf94b
Showing
12 changed files
with
213 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Process Image | ||
|
||
[Nxtheme-creator Index](../README.md#nxtheme-creator-index) / [Nxtheme Creator](./index.md#nxtheme-creator) / Process Image | ||
|
||
> Auto-generated documentation for [nxtheme_creator.process_image](../../../nxtheme_creator/process_image.py) module. | ||
- [Process Image](#process-image) | ||
- [resize_center_crop](#resize_center_crop) | ||
- [resize_image](#resize_image) | ||
- [resize_outer_crop_letterbox](#resize_outer_crop_letterbox) | ||
- [resize_stretch](#resize_stretch) | ||
|
||
## resize_center_crop | ||
|
||
[Show source in process_image.py:9](../../../nxtheme_creator/process_image.py#L9) | ||
|
||
Resize the image using center crop method. | ||
|
||
#### Signature | ||
|
||
```python | ||
def resize_center_crop(image: Image.Image): ... | ||
``` | ||
|
||
|
||
|
||
## resize_image | ||
|
||
[Show source in process_image.py:35](../../../nxtheme_creator/process_image.py#L35) | ||
|
||
Resize the image using the specified method and save the output. | ||
|
||
#### Signature | ||
|
||
```python | ||
def resize_image(input_path, output_path, method="stretch"): ... | ||
``` | ||
|
||
|
||
|
||
## resize_outer_crop_letterbox | ||
|
||
[Show source in process_image.py:28](../../../nxtheme_creator/process_image.py#L28) | ||
|
||
Resize the image using outer crop (letterbox) method. | ||
|
||
#### Signature | ||
|
||
```python | ||
def resize_outer_crop_letterbox(image: Image.Image): ... | ||
``` | ||
|
||
|
||
|
||
## resize_stretch | ||
|
||
[Show source in process_image.py:5](../../../nxtheme_creator/process_image.py#L5) | ||
|
||
Resize the image by stretching it to the target SIZE. | ||
|
||
#### Signature | ||
|
||
```python | ||
def resize_stretch(image: Image.Image): ... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from PIL import Image, ImageOps | ||
|
||
SIZE = (1280, 720) | ||
|
||
def resize_stretch(image: Image.Image): | ||
"""Resize the image by stretching it to the target SIZE.""" | ||
return image.resize(SIZE, Image.Resampling.LANCZOS) | ||
|
||
def resize_center_crop(image: Image.Image): | ||
"""Resize the image using center crop method.""" | ||
# Find the aspect ratio of the target SIZE and original image | ||
aspect_ratio_target = SIZE[0] / SIZE[1] | ||
aspect_ratio_original = image.width / image.height | ||
|
||
if aspect_ratio_original > aspect_ratio_target: | ||
# Image is wider than target, crop horizontally | ||
new_width = int(aspect_ratio_target * image.height) | ||
offset = (image.width - new_width) // 2 | ||
cropped_image = image.crop((offset, 0, offset + new_width, image.height)) | ||
else: | ||
# Image is taller than target, crop vertically | ||
new_height = int(image.width / aspect_ratio_target) | ||
offset = (image.height - new_height) // 2 | ||
cropped_image = image.crop((0, offset, image.width, offset + new_height)) | ||
|
||
return cropped_image.resize(SIZE, Image.Resampling.LANCZOS) | ||
|
||
def resize_outer_crop_letterbox(image: Image.Image): | ||
"""Resize the image using outer crop (letterbox) method.""" | ||
# Add padding if necessary (letterbox) | ||
image.thumbnail(SIZE, Image.Resampling.LANCZOS) | ||
letterbox_image = ImageOps.pad(image, SIZE, color=(0, 0, 0)) | ||
return letterbox_image | ||
|
||
def resize_image(input_path, output_path, method='stretch'): | ||
"""Resize the image using the specified method and save the output.""" | ||
image = Image.open(input_path) | ||
|
||
if method == 'stretch': | ||
resized_image = resize_stretch(image) | ||
elif method == 'centerCrop': | ||
resized_image = resize_center_crop(image) | ||
elif method == 'outerCrop': | ||
resized_image = resize_outer_crop_letterbox(image) | ||
else: | ||
return input_path | ||
|
||
resized_image.save(output_path, progressive=False) | ||
return output_path |
Oops, something went wrong.