Skip to content

Image Operations

After acquiring your images as Frame objects in the previous section, you can perform a variety of operations on them.

These operations are useful for pre-processing the images before running inferences on them, especially if your model was trained with images acquired in slightly different conditions from what you have when running inference.

For example, if your train dataset images were taken in a room with more light, you might want to adjust the brightness and contrast before running inference on your frames.

Below, you can see the list of useful operations that can be performed on a Frame object.

Cropping

Cropping is the operation of extracting regions of interest from an image.

from landingai.pipeline.frameset import Frame

frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.crop((16, 16, 112, 112))  # (1)!
frame.save_image("docs/images/cereal-ops/cereal-cropped.jpeg")
  1. (16, 16, 112, 112) is the bounding-box coordinates to crop, meaning that the cropped image will start at (16, 16) and end at (112, 112).

Cereal image Cereal cropped image

Crop predictions

Appart from cropping arbitrary regions of a Frame, you can also automatically crop detected objects from a Frame.

from landingai.pipeline.frameset import Frame
from landingai.predict import Predictor

frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
predictor = Predictor(  # (1)!
    endpoint_id=ENDPOINT_ID,
    api_key=API_KEY,
)
frame.run_predict(predictor) # (2)!
for i, cropped_frame in enumerate(frame.crop_predictions()):  # (3)!
    cropped_frame.resize(width=64)  # (4)!
    cropped_frame.save_image(f"docs/images/cereal-ops/cereal-crop-predictions-{i}.png") # (5)!
  1. Creates a predictor to run inference using a LandingLens model. See the Running Inferences section for more details on how to build and deploy a computer vision model.
  2. Runs inference on the frame.
  3. Iterate over the objects detected in the image. Each iteration will yield a new Frame object with just the detected object in it.
  4. Resizes the cropped frame to 64 pixels width, keeping original aspect ratio.
  5. Save the cropped frame to a file.

Cereal image Cereal screw 1 Cereal screw 2 Cereal screw 3

For more details about running inferences, see the Running Inferences section.

Resizing

You can resize your image to any aspect ratio by passing both width and height:

from landingai.pipeline.frameset import Frame

frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.resize(width=64, height=128)
frame.save_image("docs/images/cereal-ops/cereal-resized-both.jpeg")

Cereal image Cereal resized both dimentions

You can also keep the original aspect ratio by passing only one of the dimensions:

from landingai.pipeline.frameset import Frame

frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.resize(width=64)
frame.save_image("docs/images/cereal-ops/cereal-resized-width.jpeg")

Cereal image Cereal resized only one dimention

Resizing images before running inference can speed up the inference process. But don't downscale too much, or you might lose important image details.

Color

You can adjust color intensity:

  • No change: 1.0
  • Less intensity: Less than 1.0
  • More intensity: Greater than 1.0

See the following examples.

from landingai.pipeline.frameset import Frame

# Adjust color to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_color(0.1)
frame.save_image("docs/images/cereal-ops/cereal-color-0.1.jpeg")

# Adjust color to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_color(2.0)
frame.save_image("docs/images/cereal-ops/cereal-color-2.0.jpeg")

Cereal image Cereal low color adjustment Cereal high color adjustment

Contrast

You can adjust the contrast intensity:

  • No change: 1.0
  • Less intensity: Less than 1.0
  • More intensity: Greater than 1.0

See the following examples.

from landingai.pipeline.frameset import Frame

# Adjust contrast to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_contrast(0.1)
frame.save_image("docs/images/cereal-ops/cereal-contrast-0.1.jpeg")

# Adjust contrast to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_contrast(2.0)
frame.save_image("docs/images/cereal-ops/cereal-contrast-2.0.jpeg")

Cereal image Cereal low contrast adjustment Cereal high contrast adjustment

Brightness

You can adjust the brightness intensity:

  • No change: 1.0
  • Less intensity: Less than 1.0
  • More intensity: Greater than 1.0

See the following examples.

from landingai.pipeline.frameset import Frame

# Adjust brightness to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_brightness(0.1)
frame.save_image("docs/images/cereal-ops/cereal-brightness-0.1.jpeg")

# Adjust brightness to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_brightness(2.0)
frame.save_image("docs/images/cereal-ops/cereal-brightness-2.0.jpeg")

Cereal image Cereal low brightness adjustment Cereal high brightness adjustment

Sharpness

You can adjust the sharpness intensity:

  • No change: 1.0
  • Less intensity: Less than 1.0
  • More intensity: Greater than 1.0

See the following examples.

from landingai.pipeline.frameset import Frame

# Adjust sharpness to 0.1
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_sharpness(0.1)
frame.save_image("docs/images/cereal-ops/cereal-sharpness-0.1.jpeg")

# Adjust sharpness to 2.0
frame = Frame.from_image("docs/images/cereal-ops/cereal.jpeg")
frame.adjust_sharpness(2.0)
frame.save_image("docs/images/cereal-ops/cereal-sharpness-2.0.jpeg")

Cereal image Cereal low sharpness adjustment Cereal high sharpness adjustment