-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle image pixel limits, add tests for image resizing
- Loading branch information
Showing
4 changed files
with
72 additions
and
12 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,29 @@ | ||
from polybot.image import Image | ||
from io import BytesIO | ||
from PIL import Image as PILImage | ||
from pathlib import Path | ||
|
||
|
||
def count_pixels(image): | ||
img = PILImage.open(BytesIO(image.data)) | ||
return img.size[0] * img.size[1] | ||
|
||
|
||
def test_image_resize(): | ||
img = Image( | ||
path=Path(__file__).parent / "images" / "sample.png", mime_type="image/png" | ||
) | ||
|
||
original_size = len(img.data) | ||
|
||
resized = img.resize_to_target(original_size + 1) | ||
assert img == resized | ||
|
||
for target_bytes in (1000000, 1500000): | ||
resized = img.resize_to_target(target_bytes) | ||
assert img.mime_type == resized.mime_type | ||
assert len(resized.data) <= target_bytes | ||
|
||
for target_pixels in (1200 * 1200, 1000 * 1000): | ||
resized = img.resize_to_target(5000000, target_pixels) | ||
assert count_pixels(resized) <= target_pixels |