Last active
February 11, 2024 19:15
-
-
Save glenn-jocher/715d3b6b86de7f9ae197ff8ebd4e1c2c to your computer and use it in GitHub Desktop.
iOS App Store Preview Image Resizer
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
""" | |
iOS App Store Preview Image Resizer | |
This script provides functionality to resize images for iOS app store previews. It offers two modes for resizing: | |
- "pad": This mode will pad the image with white space to fit the target dimensions without cropping. | |
- "crop": This mode will crop the image to match the target aspect ratio, then resize it to the exact target dimensions. | |
Usage: | |
- Modify the `input_dir` variable to point to the directory containing the images you want to resize. | |
- The script will create output directories named with the target dimensions and the device name. | |
- By default, the script resizes images for three iOS devices: iPhone 5.5", iPhone 6.5", and iPad 12.9". | |
- The resizing mode can be set to either "crop" or "pad" in the `__main__` section. | |
Dependencies: | |
- PIL (Pillow) | |
""" | |
from pathlib import Path | |
from PIL import Image | |
def resize_image_for_ios_preview(input_path: Path, output_path: Path, target_size=(1284, 2778), mode="pad"): | |
""" | |
Resizes an image to the specified target size for iOS app store previews. | |
Args: | |
input_path (Path): Path to the input image. | |
output_path (Path): Path to save the resized image. | |
target_size (tuple, optional): Target size for the image. Defaults to (1284, 2778). | |
mode (str, optional): Resizing mode. Can be "pad" or "crop". Defaults to "pad". | |
""" | |
# Open the input image | |
img = Image.open(input_path) | |
# Calculate aspect ratios | |
src_ratio = img.width / img.height | |
tgt_ratio = target_size[0] / target_size[1] | |
if mode == "crop": | |
# Calculate the cropping box | |
if src_ratio > tgt_ratio: | |
new_width = int(img.height * tgt_ratio) | |
left = (img.width - new_width) // 2 | |
img = img.crop((left, 0, left + new_width, img.height)) | |
else: | |
new_height = int(img.width / tgt_ratio) | |
top = (img.height - new_height) // 2 | |
img = img.crop((0, top, img.width, top + new_height)) | |
# Resize the cropped image to the target dimensions | |
img = img.resize(target_size, Image.LANCZOS) | |
elif mode == "pad": | |
# Continue with padding logic as before | |
blank_image = Image.new("RGB", target_size, "white") | |
img = img.resize((target_size[0], int(img.height * (target_size[0] / img.width))), Image.LANCZOS) | |
if img.height < target_size[1]: | |
img = img.resize((int(img.width * (target_size[1] / img.height)), target_size[1]), Image.LANCZOS) | |
img_position = ((target_size[0] - img.width) // 2, (target_size[1] - img.height) // 2) | |
blank_image.paste(img, img_position) | |
img = blank_image | |
# Save the final image | |
img.save(output_path) | |
if __name__ == "__main__": | |
input_dir = Path("/Users/glennjocher/Downloads/images") | |
names = "iPhone_5.5", "iPhone_6.5", "iPad_12.9" | |
sizes = (1242, 2208), (1284, 2778), (2048, 2732) | |
for i, size in enumerate(sizes): | |
output_dir = Path(f"/Users/glennjocher/Downloads/images{size[0]}x{size[1]}_{names[i]}") | |
output_dir.mkdir(parents=True, exist_ok=True) | |
for img_file in input_dir.iterdir(): | |
if img_file.suffix.lower() in [".jpg", ".jpeg", ".png"]: | |
output_file = output_dir / img_file.name | |
resize_image_for_ios_preview(img_file, output_file, target_size=size, mode="crop") # mode=crop or pad |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment