Skip to content

Commit

Permalink
create image_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeyi-Lin committed Sep 2, 2024
1 parent abcc3a3 commit 2e1850a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*.pyc
__pycache__/
**/__pycache__
.idea
.DS_Store
hivision_modnet.onnx
55 changes: 55 additions & 0 deletions image_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from PIL import Image
import io


def resize_image_to_kb(input_image_path, output_image_path, target_size_kb):
"""
Resize an image to a target size in KB.
将图像调整大小至目标文件大小(KB)。
:param input_image_path: Path to the input image. 输入图像的路径。
:param output_image_path: Path to save the resized image. 保存调整大小后的图像的路径。
:param target_size_kb: Target size in KB. 目标文件大小(KB)。
Example:
resize_image_to_kb('input_image.jpg', 'output_image.jpg', 50)
"""

# Open an image file
with Image.open(input_image_path) as img:
# Convert image to RGB mode if it's not
if img.mode != 'RGB':
img = img.convert('RGB')

# Initial quality value
quality = 95

while True:
# Create a BytesIO object to hold the image data in memory
img_byte_arr = io.BytesIO()

# Save the image to the BytesIO object with the current quality
img.save(img_byte_arr, format='JPEG', quality=quality)

# Get the size of the image in KB
img_size_kb = len(img_byte_arr.getvalue()) / 1024

# Check if the image size is within the target size
if img_size_kb <= target_size_kb:
# If the image is smaller than the target size, add padding
if img_size_kb < target_size_kb:
padding_size = int((target_size_kb * 1024) - len(img_byte_arr.getvalue()))
padding = b'\x00' * padding_size
img_byte_arr.write(padding)

# Save the image to the output path
with open(output_image_path, 'wb') as f:
f.write(img_byte_arr.getvalue())
break

# Reduce the quality if the image is still too large
quality -= 5

# Ensure quality does not go below 1
if quality < 1:
quality = 1

0 comments on commit 2e1850a

Please sign in to comment.