Python Pillow - ImageChops.offset() Function



The PIL.ImageChops.offset function returns a copy of the input image where the data has been offset by the specified horizontal and vertical distances. The data wraps around the edges, and if the vertical offset (yoffset) is omitted, it is assumed to be equal to the horizontal offset (xoffset). The function takes the following parameters −

Syntax

Following is the syntax of the function −

PIL.ImageChops.offset(image, xoffset, yoffset=None)

Parameters

Here are the details of this function parameters −

  • image − Input image.

  • xoffset − The horizontal distance by which the data is offset.

  • yoffset − The vertical distance by which the data is offset. If omitted, both horizontal and vertical distances are set to the same value.

Return Value

The function returns an Image type, representing the resulting image after applying the offset.

Examples

Example 1

Here is an example Offset an image by a given horizontal distance only.

from PIL import Image, ImageChops

# Open an Image 
original_image = Image.open("Images/Car_2.jpg")

# Set the horizontal offset
x_offset = 100

# Apply the offset to the image (y_offset defaults to x_offset)
result_image = ImageChops.offset(original_image, x_offset)

# Display the input and resulting image
original_image.show()
result_image.show()

Output

Input Image

balck yellow car

Output Image

imagechops offset

Example 2

The following example applies the ImageChops.offset() function to adjust the position of an Image by specified horizontal and vertical Offsets.

from PIL import Image, ImageChops

# Open an Image 
original_image = Image.open("Images/Car_2.jpg")

# Set the horizontal and vertical offsets
x_offset = 100
y_offset = -50

# Apply the offset to the image
result_image = ImageChops.offset(original_image, x_offset, y_offset)

# Display the input and resulting image
original_image.show()
result_image.show()

Output

Input Image

balck yellow car

Output Image

chops offset

Example 3

The following example demonstrates the use of ImageChops.offset() to shift the image and then use Image.paste() function to fill the wrapped area with yellow color.

from PIL import Image, ImageChops

# Open an Image 
original_image = Image.open('Images/Car_2.jpg')
width, height = original_image.size

# Apply the offset to the image
result_image = ImageChops.offset(original_image, 10, 20)

# Fill the wrapped area with the yellow color
result_image.paste((255, 255, 255), (0, 0, 10, height))
result_image.paste((255, 255, 255), (0, 0, width, 20))

# Display the input and resulting image
original_image.show()
result_image.show()

Output

Input Image

balck yellow car

Output Image

chops offset image
python_pillow_function_reference.htm
Advertisements