2017-12-08 14 views
-2

이미지의 일부를 선택하여 이미지 파일로 저장하려고하지만 imcrop() 함수를 사용하고 싶지 않습니다. 하지만 자르기 기능을 사용하고 싶지 않습니다. 다른 방법은 없습니까?Python에서 Image의 일부를 선택하십시오 (imcrop을 사용하지 않고)

+1

어떤 이미지 처리 라이브러리를 사용하고 있습니까? 'imcrop'은 무엇이고 왜 그것을 사용하고 싶지 않으십니까? – martineau

답변

0

배열로 변환하여 인덱싱하여 "자르기"한 다음 다시 이미지로 변환 할 수 있습니다.

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