DEV Community

Ajit Kumar
Ajit Kumar

Posted on

Why Your Images Fail to Upload on ImageKit and How to Fix It (With Ubuntu, Shell, and Python Scripts)

If you’ve ever tried uploading a high-resolution image to ImageKit and received a confusing “Bad Request” without any useful error message, you’re not alone. Many developers (especially new users) struggle with this because ImageKit enforces limits on image size and resolution, and these limits aren’t always obvious from the UI.

In this article, we’ll explain what those limits are (with links to official docs), why they cause the upload to fail, and how to resize and automate image processing on Ubuntu. We’ll also include ready-to-use shell scripts and Python scripts — including a script to upload images to AWS S3 for use as ImageKit external storage.


What ImageKit Limits You Need to Know

ImageKit enforces the following limits on uploaded files and image transformations:

1. File Size Limitation

  • The maximum upload file size for images via the ImageKit upload API is 25 MB (this limit applies to free and many paid plans by default) — see the API type definition. (Gist)

2. Megapixel / Total Pixel Count Limit

  • On free plans (and often enforced regardless of plan), ImageKit restricts image processing to 25 MP (megapixels), i.e., width × height must not exceed ~25 million. Images above this threshold can result in 400 “Bad Request” errors. (ImageKit)

Example:
5000 × 5000 ≈ 25 MP — This is the approximate upper limit free plan users should target when resizing images locally before uploading.


Why You Saw “Bad Request” Without Clear Errors

ImageKit may return a generic 400 Bad Request if:

  • The uploaded file exceeds the max file size for uploading (25 MB). (Gist)
  • The resolution (pixel count) of the image exceeds the processing limits (e.g., above 25 MP). (ImageKit Community)

For new users, this can be confusing because there’s no explicit error “Image too large” in the dashboard UI — only the HTTP error.


Best Practices Before Uploading

  1. Resize images to under 5000×5000 pixels (which is ~25 MP).
  2. Compress the image to be below 25 MB.
  3. Use efficient formats such as WebP or AVIF when possible.

Image Resizing on Ubuntu

Install ImageMagick

You’ll use magick (ImageMagick CLI) to resize images.

sudo apt update
sudo apt install imagemagick
Enter fullscreen mode Exit fullscreen mode

Resize Image to Fit Under Limits

magick original.jpg -resize 5000x5000\> -quality 80 -strip resized.jpg

# Legacy command

convert original.jpg -resize 2500 original_optimized.jpg

Enter fullscreen mode Exit fullscreen mode
  • 5000x5000\> ensures the longest side is ≤ 5000px without upscaling.
  • -quality 80 balances compression and visual quality.
  • -strip removes metadata to reduce filesize.

Convert to WebP

magick resized.jpg -quality 80 output.webp
Enter fullscreen mode Exit fullscreen mode

WebP often yields smaller files than JPEG.


Batch Resize Script (Ubuntu Shell)

For multiple images in a directory:

#!/bin/bash
# batch_resize.sh

SRC_DIR="./originals"
DST_DIR="./processed"
mkdir -p "$DST_DIR"

for img in "$SRC_DIR"/*.{jpg,jpeg,png}; do
  base=$(basename "$img")
  echo "Processing $base ..."
  magick "$img" -resize 5000x5000\> -quality 80 -strip "$DST_DIR/$base"
done

echo "Done resizing all images."
Enter fullscreen mode Exit fullscreen mode

Save as batch_resize.sh, make executable, and run:

chmod +x batch_resize.sh
./batch_resize.sh
Enter fullscreen mode Exit fullscreen mode

Python Script for Resizing and Renaming

This script resizes all images in a folder and renames them with a suffix (e.g., _optimized):

from PIL import Image
import os

SRC_DIR = "originals"
DST_DIR = "processed"
MAX_SIZE = (5000, 5000)

os.makedirs(DST_DIR, exist_ok=True)

for file in os.listdir(SRC_DIR):
    if file.lower().endswith((".jpg", ".jpeg", ".png")):
        img = Image.open(os.path.join(SRC_DIR, file))
        img.thumbnail(MAX_SIZE)
        base, ext = os.path.splitext(file)
        out_file = f"{base}_optimized{ext}"
        img.save(os.path.join(DST_DIR, out_file), optimize=True, quality=80)
        print(f"Saved {out_file}")
Enter fullscreen mode Exit fullscreen mode

Run with:

pip install pillow
python resize_images.py
Enter fullscreen mode Exit fullscreen mode

Upload Resized Images to AWS S3 (for ImageKit External Storage)

If you use S3 as external storage, here’s a complete Python upload script:

import boto3
import os

BUCKET_NAME = "your-bucket-name"
SRC_DIR = "processed"
AWS_REGION = "ap-northeast-1"

s3 = boto3.client("s3", region_name=AWS_REGION)

for filename in os.listdir(SRC_DIR):
    file_path = os.path.join(SRC_DIR, filename)
    object_key = f"images/{filename}"
    print(f"Uploading {filename} to s3://{BUCKET_NAME}/{object_key}")
    s3.upload_file(file_path, BUCKET_NAME, object_key,
                   ExtraArgs={"ACL": "public-read"})
print("Upload complete.")
Enter fullscreen mode Exit fullscreen mode

Install AWS SDK for Python and configure credentials:

pip install boto3
aws configure
Enter fullscreen mode Exit fullscreen mode

Summary Checklist

Before uploading your images to ImageKit:

  • [ ] Resize to ≤ 5000×5000 px (≈25 MP). (ImageKit)
  • [ ] Ensure file size ≤ 25 MB. (Gist)
  • [ ] Optionally convert to WebP or AVIF.
  • [ ] Batch automate resizing and compression.

With the above scripts and practices, even beginners can confidently prepare images for upload without encountering “Bad Request” errors.

Top comments (0)