Python
Image Processing
Additive Gaussian Noise
Error Handling
Computer Vision

Adding AdditiveGaussianNoise to a single image - AssertionError Expected boolean as argument for 'return_batch'

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

This error usually appears when imgaug receives a positional argument where it expects a boolean flag named return_batch. For a single image, the fix is to call the augmenter with a named image argument or use the single-image helper method instead of calling the augmenter like a plain function.

Why the error happens

In imgaug, augment() is a keyword-oriented API. The library expects calls such as augment(image=...) or augment(images=...). If you call the augmenter as aug(resized_img), the array is treated as the first positional parameter, which in that API slot corresponds to return_batch.

That is why the error message says it expected a boolean. The library is not complaining about the image content yet. It is complaining that the function was called with the wrong argument style.

The correct way to augment one image

Here is a working example with one image. It uses image= for a single input and also converts the resized result into a dtype that matches a pixel-scale noise setting.

python
1from skimage.io import imread
2from skimage.transform import resize
3import imgaug.augmenters as iaa
4
5image = resize(
6    imread("example.jpg"),
7    (224, 224),
8    preserve_range=True
9).astype("uint8")
10
11aug = iaa.AdditiveGaussianNoise(scale=(0, 0.2 * 255))
12
13augmented_image = aug(image=image)
14
15print(augmented_image.shape)
16print(augmented_image.dtype)

You can also use images=[image] if you want batch-style behavior and then index the first result. The key point is that the input must be passed by name.

Single image versus batch input

There are two valid patterns:

  • 'aug(image=image) for one image'
  • 'aug(images=[image]) for a batch-like call'

The batch form is useful when your code path already expects a list or array of images. The single-image form is more direct when you are just testing or building a small preprocessing step.

Using the correct call signature also makes the code easier to read. Anyone reviewing it can immediately tell whether the augmenter is being applied to one image or a batch.

Watch the dtype and value range

There is a second issue that often follows the API fix. skimage.transform.resize commonly returns floating-point data, often in the zero-to-one range, while the noise scale in many imgaug examples is expressed in pixel units based on zero to 255.

If you keep the resized image as floating-point zero-to-one data but use scale=(0, 0.2 * 255), the noise is far larger than you probably intend. Either preserve pixel range and cast to uint8, or keep the floating-point representation and scale the noise to match that range.

That mismatch does not cause the return_batch assertion, but it does cause bad augmentations once the call starts working.

Common Pitfalls

  • Calling aug(image_array) positionally instead of using image= or images=.
  • Fixing the API call but leaving the image in zero-to-one float format while using a zero-to-255 noise scale.
  • Forgetting that resize may change dtype and range unless you manage them deliberately.
  • Using batch mode accidentally and then being confused by the extra leading dimension.
  • Debugging the augmenter parameters first when the initial error is only about argument passing.

Summary

  • The return_batch assertion usually means imgaug received a positional image argument in the wrong API slot.
  • Pass a single image with image=image or a batch with images=[image].
  • The library is expecting named arguments, not a raw array as the first positional input.
  • After fixing the call, check the image dtype and value range so the noise scale still makes sense.
  • For single-image augmentation, the named single-image call is the clearest option.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design