Skip to content

Commit

Permalink
Updated the image rescale function in image_utils.py
Browse files Browse the repository at this point in the history
Updated the rescale function in the image_utils.py file so that it maintains aspect ration of the image when it rescales it to 480p. Any image that is smaler than 480p will not be resized anymore.
  • Loading branch information
AlTimofeyev committed May 17, 2024
1 parent 4184f65 commit 4c41e37
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

<br>

## Unreleased
- ...



<br>

## [1.3.4] - 2024-05-16
- CHANGED: The image rescale function in `image_utils.py`.
- Images are now rescaled according to a proper mathematical formula and maintain their aspect ratio instead of hard-rescaling images to 480p with a 16:9 or 9:16 aspect ratio.
- The math behind rescaling the image came from: https://math.stackexchange.com/a/3078131
- If an image is smaller the required sampling size of 480p, it is not rescaled.

<br>

## [1.3.3] - 2023-04-07
- ADDED: Added a `--save-check` option to ask if the user wants to save the extracted color palettes.
- ADDED: Added a `--preview` option to show a preview of extracted color palette.
Expand Down Expand Up @@ -93,6 +108,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- [filenam]-color_palette.json


[1.3.4]: https://github.com/AlTimofeyev/pypalex/compare/1.3.3...1.3.4
[1.3.3]: https://github.com/AlTimofeyev/pypalex/compare/1.3.2...1.3.3
[1.3.2]: https://github.com/AlTimofeyev/pypalex/compare/1.3.1...1.3.2
[1.3.1]: https://github.com/AlTimofeyev/pypalex/compare/1.3.0...1.3.1
Expand Down
43 changes: 27 additions & 16 deletions pypalex/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# - Modified by Al Timofeyev on April 21, 2022.
# - Modified by Al Timofeyev on March 6, 2023.
# - Modified by Al Timofeyev on April 5, 2023.
# - Modified by Al Timofeyev on May 16, 2024.


# ---- IMPORTS ----
Expand Down Expand Up @@ -58,7 +59,10 @@ def process_image(image):
# **************************************************************************
# **************************************************************************

## Rescales image to a smaller sampling size.
## Rescales image to a smaller sampling size while maintaining aspect ration.
#
# @note The math behind rescaling the image came
# from: https://math.stackexchange.com/a/3078131
#
# @param image PIL Image object.
#
Expand All @@ -68,21 +72,28 @@ def rescale_image(image):
default_480p = [854, 480] # 480p SD resolution with 16:9 ratio.
default_360p = [640, 360] # 360p SD resolution with 16:9 ratio.

# Try scaling images down to 480p with 16:9 ratio.
if height < width: # ---- Landscape, 16:9 ratio.
percent_change_width = default_480p[0] / width
percent_change_height = default_480p[1] / height
elif width < height: # ---- Portrait, 9:16 ratio.
percent_change_width = default_480p[1] / width
percent_change_height = default_480p[0] / height
else: # ---- Square, 1:1 ratio.
percent_change_width = default_360p[0] / width
percent_change_height = default_360p[0] / height

width *= percent_change_width
height *= percent_change_height

return round(width), round(height)
# Try scaling images down to 480p.
# new_width = (width/height) * new_height
# new_height = (height/width) * new_width
if height < width: # ---- Landscape.
new_width = default_480p[0]
new_height = round((height / width) * new_width)

if new_height > default_480p[1]:
new_height = default_480p[1]
new_width = round((width / height) * new_height)
elif width < height: # ---- Portrait.
new_height = default_480p[0]
new_width = round((width / height) * new_height)

if new_width > default_480p[1]:
new_width = default_480p[1]
new_height = round((height / width) * new_width)
else: # ---- Square, 1:1 ratio.
new_width = default_360p[0]
new_height = round((width / height) * new_width)

return round(new_width), round(new_height)


# --------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion pypalex/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# - Modified by Al Timofeyev on March 22, 2023.
# - Modified by Al Timofeyev on March 26, 2023.
# - Modified by Al Timofeyev on April 7, 2023.
# - Modified by Al Timofeyev on May 16, 2024.


import os
import platform

__version__ = "1.3.3"
__version__ = "1.3.4"
__cache_version__ = "1.0.0"

HOME = os.getenv("HOME", os.getenv("USERPROFILE"))
Expand Down

0 comments on commit 4c41e37

Please sign in to comment.