Python Pillow - ImageChops.offset() 函式



PIL.ImageChops.offset 函式返回輸入影像的副本,其中資料已根據指定的水平和垂直距離偏移。資料圍繞邊緣環繞,如果垂直偏移 (yoffset) 被省略,則假定它等於水平偏移 (xoffset)。該函式採用以下引數:

語法

以下是函式的語法:

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

引數

以下是此函式引數的詳細資訊:

  • image - 輸入影像。

  • xoffset - 資料偏移的水平距離。

  • yoffset - 資料偏移的垂直距離。如果省略,則水平和垂直距離都設定為相同的值。

返回值

該函式返回 Image 型別,表示應用偏移後生成的影像。

示例

示例 1

這是一個僅根據給定的水平距離偏移影像的示例。

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()

輸出

輸入影像

balck yellow car

輸出影像

imagechops offset

示例 2

以下示例應用 ImageChops.offset() 函式來根據指定的水平和垂直偏移調整影像的位置。

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()

輸出

輸入影像

balck yellow car

輸出影像

chops offset

示例 3

以下示例演示瞭如何使用 ImageChops.offset() 來移動影像,然後使用 Image.paste() 函式填充環繞區域的黃色。

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()

輸出

輸入影像

balck yellow car

輸出影像

chops offset image
python_pillow_function_reference.htm
廣告