26 lines
576 B
Python
26 lines
576 B
Python
import numpy as np
|
||
|
||
# 定义3x3灰度图像
|
||
image = np.array([
|
||
[100, 150, 200],
|
||
[80, 120, 180],
|
||
[60, 90, 140]
|
||
], dtype=np.uint8)
|
||
|
||
print("原图:")
|
||
print(image)
|
||
|
||
# 1. 变暗20:每个像素值减20(需确保数值非负,uint8会自动截断,这里手动保证)
|
||
image_dark = np.clip(image - 20, 0, 255)
|
||
print("\n变暗20后:")
|
||
print(image_dark)
|
||
|
||
# 2. 裁剪左上角2x2区域
|
||
image_crop = image[:2, :2]
|
||
print("\n裁剪左上角2x2区域:")
|
||
print(image_crop)
|
||
|
||
# 3. 水平翻转
|
||
image_flip = np.fliplr(image)
|
||
print("\n水平翻转:")
|
||
print(image_flip) |