将图片切割并保存为图片数组代码
时间: 2024-05-04 22:16:42 浏览: 86
Sure, 我可以帮你提供一个基于Python的代码,用于将图片切割并保存为图片数组。下面是代码示例:
```python
import cv2
import numpy as np
# Load image
img = cv2.imread("your_image_path", cv2.IMREAD_UNCHANGED)
# Set crop dimensions
width = 100 # crop width
height = 100 # crop height
step_x = 100 # step size in x direction
step_y = 100 # step size in y direction
# Crop images using sliding window technique
images = []
for y in range(0, img.shape[0], step_y):
for x in range(0, img.shape[1], step_x):
crop_img = img[y:y+height, x:x+width]
if crop_img.shape[:2] == (height, width): # discard incomplete crops
images.append(crop_img)
# Save cropped images
for i, cropped in enumerate(images):
cv2.imwrite(f"cropped_{i}.png", cropped)
```
这个代码可以将原始图像进行分割,每个子图像大小均为 100x100 像素,步长为 100 像素。处理完后将分割的图像保存在当前工作目录下,文件名为 "cropped_x.png",其中x 是分割的图像的编号。希望这个回答对你有所帮助!
阅读全文