I want to create an image file so it's exactly 5MB for testing purposes. Is it possible?
7 Answers
Most implementations of image formats ignore trailing zeros. Therefore, it is relatively straightforward to pad a file to a desired size. (5 megabytes is 10240 blocks.)
#! /usr/bin/env bash
print_usage_and_exit(){
echo "Usage: $0 FILE BLOCKS"
echo "This script will pad FILE with zeros up to BLOCKS."
echo "FILE must not already be larger than BLOCKS."
exit
}
FILE=$1
BLOCKS=$2
if [ -z $FILE ] || [ -z $BLOCKS ] || [ -z "${BLOCKS##*[!0-9]*}" ]; then
print_usage_and_exit
fi
FILE_SIZE=$(stat $FILE | awk '/Blocks:/ { print $4 }')
SIZE_DIFFERENCE=$(($BLOCKS-$FILE_SIZE))
if [ $SIZE_DIFFERENCE -le 0 ]; then
print_usage_and_exit
fi
dd if=/dev/zero iflag=append count=$SIZE_DIFFERENCE >> $FILE 2>/dev/null
- 1,274
Few other answers calculate how many trailing zeros you need to append, then they append. In Linux there's this simple way:
truncate -s 5MB image
(this assumes you want 5 MB, i.e. 5*1000*1000 bytes. For 5 MiB, i.e. 5*1024*1024 bytes, use -s 5M).
If originally image is larger than the specified size, then the extra data will be lost. If originally image is smaller, then padding zeros will be added. Added fragment will be sparse, if possible.
If for any reason you don't want sparseness, use fallocate:
fallocate -l 5MB image
(similarly, yet not identically: -l 5MB for 5 MB, -l 5MiB for 5 MiB).
fallocate used this way can only extend image. If the file is already at least that big then its size won't change (sparseness can change, I won't elaborate).
- 81,893
if you insist on Windows - I suggest going to CMD , find a suitable small file ...
>copy /B file1+file1 file2 while replacing file1 and file2 with suitable filenames .. you can repeat said copy command by using file2 as source (first 2 names) .. the files will be binary concatenated - so you get a target file of increasing size - until you are satisfied
not as nice as a script - but it works out of the (windows)-box only needs 1 source file .. that doesn't even necessarily has to be an image - as mime-type for uploads mostly is determined by the file-ending
- 234
As suggested in the comments, you can use the image metadata. jpg supports exif data. You mentioned php, which can write raw exif data with iptcembed(). Generate a string of null bytes (binary 0) long enough to pad out the file
<?php
$path = '/path/to/image.jpg';
$size = filesize($path);
$goal = 1024 * 1024 * 5; //5MB
$zeros = str_pad("", $goal - $size, "\0"); // make a string of ($goal - $size) binary 0s
$paddedImage = iptcembed($zeros, $path);
file_put_contents("/path/to/padded-image.jpg", $paddedImage);
- 1,253
Does it have to be any specific format? For instance, would a BMP file do?
Uncompressed BMP files are exactly that: completely uncompressed. Thus, each pixel takes up a fixed amount of storage, so it's just a matter of working backwards from the file size you want to the dimensions of the image. So, for an 8-bit BMP, in theory you'd need a 5 megapixel image to get a 5 megabyte file.
Unfortunately, there are a few things that complicate the calculation somewhat:
You're going to have to take the size of the BMP header into account. There are several different versions of the format, each with a different header size, so you'll need to experiment with your image editor to figure out what size header it produces.
Each line of the image is padded to be a multiple of four bytes, so there might be a few extra bytes per line.
BMP files can be compressed, indexed, and have color profile information included. These options are rarely used, but they are out there. If your graphics program uses these and you can't turn them off, your size calculation is going to get a lot more complicated (if it's even possible at all).
- 328
Here's a Python function to create a BMP image with a specific size in bytes:
import struct
def create_bmp_with_size(filename: str, size_in_bytes: int) -> None:
"""
Creates a 1×1 pixel 24-bit BMP image of a specified file size in bytes.
The minimum valid BMP size here is 58 bytes. If size_in_bytes < 58,
this function will raise a ValueError.
:param filename: Path to the output BMP file.
:param size_in_bytes: Desired total size of the BMP file in bytes.
"""
# A minimal 1×1 24-bit BMP requires 14-byte file header, 40-byte DIB header,
# and 4 bytes of pixel data (3 bytes for one pixel + 1 padding).
MIN_BMP_SIZE = 58
if size_in_bytes < MIN_BMP_SIZE:
raise ValueError(f"Minimum size for a 1×1 BMP is {MIN_BMP_SIZE} bytes.")
# -- BMP FILE HEADER (14 bytes) --
# "BM" signature (2 bytes), file size (4 bytes), reserved (4 bytes), offset to pixel data (4 bytes)
bfType = b'BM' # Signature
bfSize = struct.pack('<I', size_in_bytes) # Total file size
bfReserved = b'\x00\x00\x00\x00' # Reserved (2×2 bytes)
bfOffBits = struct.pack('<I', 54) # Pixel data starts at byte 54
bmp_header = bfType + bfSize + bfReserved + bfOffBits
# -- DIB HEADER (BITMAPINFOHEADER, 40 bytes) --
# biSize (4), biWidth (4), biHeight (4), biPlanes (2), biBitCount (2),
# biCompression (4), biSizeImage (4), biXPelsPerMeter (4), biYPelsPerMeter (4),
# biClrUsed (4), biClrImportant (4).
biSize = 40
biWidth = 1
biHeight = 1
biPlanes = 1
biBitCount = 24
biCompression = 0
biSizeImage = 4 # For a 1×1 pixel, 3 bytes + 1 padding = 4
biXPelsPerMeter = 0
biYPelsPerMeter = 0
biClrUsed = 0
biClrImportant = 0
dib_header = struct.pack(
'<IIIHHIIIIII',
biSize,
biWidth,
biHeight,
biPlanes,
biBitCount,
biCompression,
biSizeImage,
biXPelsPerMeter,
biYPelsPerMeter,
biClrUsed,
biClrImportant
)
# -- PIXEL DATA (for 1×1 pixel, padded to 4 bytes) --
pixel_data = b'\x00\x00\x00\x00' # A single black pixel plus a padding byte
# Combine header + pixel data
bmp_data = bmp_header + dib_header + pixel_data
# If the file needs to be bigger, pad with extra null bytes
if size_in_bytes > MIN_BMP_SIZE:
bmp_data += b'\x00' * (size_in_bytes - MIN_BMP_SIZE)
# Write out the file
with open(filename, 'wb') as f:
f.write(bmp_data)
Example usage:
if name == 'main':
# Create a 1×1 BMP of exactly 1000 bytes
create_bmp_with_size('example_1000_bytes.bmp', 1000)
print("Created 'example_1000_bytes.bmp' with size = 1000 bytes.")
# Create a BMP of exactly 9,999,999 bytes
create_bmp_with_size('example_9999999_bytes.bmp', 9999999)
print("Created 'example_9999999_bytes.bmp' with size = 9,999,999 bytes.")
# Create a BMP of exactly 10,000,000 bytes
create_bmp_with_size('example_10000000_bytes.bmp', 10000000)
print("Created 'example_10000000_bytes.bmp' with size = 10,000,000 bytes.")
# Create a BMP of exactly 10,485,760 bytes (10 MiB)
create_bmp_with_size('example_10485760_bytes.bmp', 10485760)
print("Created 'example_10485760_bytes.bmp' with size = 10,485,760 bytes.")
# Create a BMP of exactly 10,485,759 bytes (10 MiB minus 1 byte)
create_bmp_with_size('example_10485759_bytes.bmp', 10485759)
print("Created 'example_10485759_bytes.bmp' with size = 10,485,759 bytes.")
# Create a BMP of exactly 5,000,000 bytes
create_bmp_with_size('example_5000000_bytes.bmp', 5000000)
print("Created 'example_5000000_bytes.bmp' with size = 5,000,000 bytes.")
# Create a BMP of exactly 9,000,000 bytes
create_bmp_with_size('example_9000000_bytes.bmp', 9000000)
print("Created 'example_9000000_bytes.bmp' "
"with size = 9,000,000 bytes.")
# Create a BMP of exactly 10,480,009 bytes
create_bmp_with_size('example_10480009_bytes.bmp', 10480009)
print("Created 'example_10480009_bytes.bmp' with size = 10,480,009 bytes.")
# Create a BMP of exactly 10,485,750 bytes (10 MiB minus 10 byte)
create_bmp_with_size('example_10485750_bytes.bmp', 10485750)
print("Created 'example_10485750_bytes.bmp' with size = 10,485,750 bytes.")
Tested with Python 3.12 on Windows 24H2 Pro.
Example: 10 MiB image.
- 1,739
- 24,246
- 64
- 231
- 400
One can use https://iamtester.ru/ (gratis):
Example with bytes = 700000 (i.e., 700 KB) and type = PNG:
FYI, related: Make a GIF image with an exact given byte size.
- 24,246
- 64
- 231
- 400

