DEV Community

Cover image for Building Images from Scratch in Python with pyaitk.CLSE
Divyanshu Sinha
Divyanshu Sinha

Posted on

Building Images from Scratch in Python with pyaitk.CLSE

When most developers think about image processing in Python, they immediately think of large external libraries. But what if you need a lightweight image toolkit that supports pixel manipulation, drawing primitives, color utilities, validation, NumPy interoperability, and multi-format image export?

That's exactly what pyaitk.CLSE provides.

In this article, we'll explore the core image APIs available in CLSE and see how they can be used to create, modify, validate, and save images with just a few lines of code.


Creating a New Image

Everything starts with TTIImage.

from pyaitk.CLSE import TTIImage

img = TTIImage(
    width=512,
    height=512,
    bpp=24,
    background=(20, 30, 60)
)
Enter fullscreen mode Exit fullscreen mode

This creates a 512×512 RGB image with a dark blue background.

Unlike many image systems that immediately require file loading, CLSE allows images to be created entirely from memory.


Direct Pixel Access

One of the most fundamental image operations is reading and writing pixels.

img.set_pixel(100, 100, (255, 0, 0))

color = img.get_pixel(100, 100)

print(color)
# (255, 0, 0)
Enter fullscreen mode Exit fullscreen mode

This makes CLSE useful for:

  • Procedural graphics
  • Scientific visualizations
  • Pixel-art generation
  • AI dataset creation
  • Custom rendering engines

Because every image is ultimately a collection of pixels.


NumPy Integration

Modern image workflows often depend on NumPy arrays.

CLSE provides seamless interoperability.

arr = img.to_array()

print(arr.shape)
# (512, 512, 3)

img.from_array(arr)
Enter fullscreen mode Exit fullscreen mode

This allows developers to:

  • Apply vectorized operations
  • Use scientific computing tools
  • Connect image pipelines to machine learning workflows
  • Exchange data with other Python libraries

without manually converting image formats.


Saving and Loading Images

Exporting images is straightforward.

img.save("output.png")

img.save("output.jpg", fmt="jpeg")

img.save("output.bmp", fmt="bmp")
Enter fullscreen mode Exit fullscreen mode

Loading existing images is equally simple.

img2 = TTIImage.load("output.png")
Enter fullscreen mode Exit fullscreen mode

Supported workflows include:

  • PNG
  • JPEG
  • BMP

allowing images to move easily between applications.


Drawing Shapes with ImageCanvas

Working pixel-by-pixel is powerful, but sometimes you need higher-level drawing tools.

CLSE provides ImageCanvas.

from pyaitk.CLSE import ImageCanvas

canvas = ImageCanvas(img)

canvas.line(
    0, 0,
    511, 511,
    (255, 255, 0)
)

canvas.rectangle(
    50, 50,
    200, 200,
    (255, 100, 0),
    filled=True
)

canvas.circle(
    256, 256,
    100,
    (0, 200, 255),
    filled=False
)
Enter fullscreen mode Exit fullscreen mode

This enables quick creation of:

  • Diagrams
  • Procedural artwork
  • Geometric datasets
  • Debug visualizations
  • Educational graphics

You can even replace the entire image background.

canvas.fill_background((10, 10, 30))
Enter fullscreen mode Exit fullscreen mode

Working with Colours

Color manipulation is a common requirement in image generation.

CLSE includes several helper utilities.

HSV to RGB

rgb = ColorUtils.hsv_to_rgb(
    0.6,
    0.8,
    0.9
)
Enter fullscreen mode Exit fullscreen mode

Colour Interpolation

blended = ColorUtils.lerp(
    (255, 0, 0),
    (0, 0, 255),
    t=0.5
)
Enter fullscreen mode Exit fullscreen mode

Result:

(127, 0, 127)
Enter fullscreen mode Exit fullscreen mode

RGB to RGBA

rgba = ColorUtils.to_rgba(
    (100, 150, 200)
)
Enter fullscreen mode Exit fullscreen mode

Result:

(100, 150, 200, 255)
Enter fullscreen mode Exit fullscreen mode

Value Clamping

clamped = ColorUtils.clamp(300)

print(clamped)
# 255
Enter fullscreen mode Exit fullscreen mode

These utilities simplify many common graphics operations.


Multi-Format Image I/O

For projects that require generic image loading and saving, CLSE provides ImageIO.

from pyaitk.CLSE import ImageIO

img = ImageIO.load("photo.png")

ImageIO.save(
    img,
    "photo.jpeg",
    quality=85
)
Enter fullscreen mode Exit fullscreen mode

This abstraction makes it easier to build reusable image pipelines.


Image Validation

Before processing or exporting an image, validation can help catch issues early.

from pyaitk.CLSE import ImageValidator

ImageValidator.validate(img)
Enter fullscreen mode Exit fullscreen mode

If corruption or invalid image data is detected, CLSE raises a dedicated exception.

This makes image workflows more robust and easier to debug.


Why This Matters

Many image libraries focus exclusively on editing existing files.

CLSE takes a different approach.

It provides a complete toolkit for:

  • Image creation
  • Pixel manipulation
  • Drawing
  • Color processing
  • NumPy interoperability
  • Multi-format export
  • Validation

all while remaining part of the broader pyaitk ecosystem.

Whether you're generating AI datasets, building procedural graphics, creating scientific visualizations, or experimenting with custom rendering pipelines, CLSE provides the building blocks needed to work directly with image data in Python.


Final Thoughts

What began as a component of the pyaitk (Pythonaibrain) ecosystem has grown into a capable image toolkit that supports both low-level pixel operations and higher-level drawing APIs.

The combination of TTIImage, ImageCanvas, ColorUtils, ImageIO, and ImageValidator provides a clean and practical workflow for image generation and manipulation.

And the best part?

You can start with a single pixel and scale all the way up to massive generated images using the same API philosophy.

Top comments (0)