![](/img/trans.png)
[英]How to solve this error: AttributeError: 'numpy.ndarray' object has no attribute 'crop'
[英]blurring an image with image.crop() - AttributeError: 'numpy.ndarray' object has no attribute 'crop'
我有一張圖像,我想模糊它的一部分。
我正在尋找的 output 就像在使用 PIL、python 答案的圖像的此過濾器部分中看到的部分模糊的棋盤。
我嘗試使用上述答案中的代碼,但收到一條錯誤消息:
----> 2 ic = image.crop(box)
AttributeError: 'numpy.ndarray' object has no attribute 'crop'
我該如何前進? 我應該嘗試將 np.ndarry 轉換為圖像嗎?
謝謝!
# libraries
import cv2
from PIL import Image
# read in
path = 'C://Users/my_account//Desktop//robert_downey_jr_image.png'
image = cv2.imread(path)
# blurring
box = (30, 30, 110, 110)
ic = image.crop(box)
for i in range(10):
ic = ic.filter(ImageFilter.BLUR)
image.paste(ic, box)
image.show()
# ERROR MESSAGE
----> 2 ic = image.crop(box)
AttributeError: 'numpy.ndarray' object has no attribute 'crop'
您的圖像只是一個 NumPy 數組,它沒有您所說的屬性crop。
您需要使用 PIL Image 庫打開它:
image = Image.open(path)
然后就可以調用crop了。 見https://pillow.readthedocs.io/en/stable/reference/Image.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.