【问题标题】:Select a part of Image in Python(without using imcrop)在 Python 中选择图像的一部分(不使用 imcrop)
【发布时间】:2018-05-22 20:56:17
【问题描述】:

我想选择图像的一部分并将其保存为图像文件,但我不想使用 imcrop() 函数。对此有很多答案,但我不想使用裁剪函数。任何其他方法?

【问题讨论】:

  • 您使用的是什么图像处理库? imcrop 是什么,你为什么不想使用它?

标签: python image python-2.7 image-processing crop


【解决方案1】:

您可以将其转换为数组并通过索引“裁剪”它,然后再转换回图像。

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

【讨论】:

    猜你喜欢
    • 2020-09-30
    • 2011-09-23
    • 2014-10-20
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多