【发布时间】:2018-05-22 20:56:17
【问题描述】:
我想选择图像的一部分并将其保存为图像文件,但我不想使用 imcrop() 函数。对此有很多答案,但我不想使用裁剪函数。任何其他方法?
【问题讨论】:
-
您使用的是什么图像处理库?
imcrop是什么,你为什么不想使用它?
标签: python image python-2.7 image-processing crop
我想选择图像的一部分并将其保存为图像文件,但我不想使用 imcrop() 函数。对此有很多答案,但我不想使用裁剪函数。任何其他方法?
【问题讨论】:
imcrop 是什么,你为什么不想使用它?
标签: python image python-2.7 image-processing crop
您可以将其转换为数组并通过索引“裁剪”它,然后再转换回图像。
from PIL import Image
import numpy as np
# Create an image for example
w, h = 512, 512
data = np.zeros((h, w, 3), dtype=np.uint8)
data[:, :, :] = 100 # Grey image
img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.show() # View the original image
img_array = np.asarray(img) # Convert image to an array
cropped = img_array[:100, :100, :] # Select portion to keep
img = Image.fromarray(cropped, 'RGB') # Convert back to image
img.save('my.png')
img.show() # View the cropped image
【讨论】: