Say that I have two images concatenate, so that the whole image is shape (256,512) like this: [X|X]
I want to be able to do this:
from PIL import Image
both_image = Image.open(path)
img_left = both_image[:,:256]
img_right = both_image[:,256:]
but *** TypeError: 'PngImageFile' object is not subscriptable
To fix it, im writing: both_image = np.array(Image.open(path)) and using Image.fromarray() at the end
Is there a better way to do so, without going from Image type to numpy array back and forth?
CodePudding user response:
I would use Image.crop and crop both_image twice.
From the Pllow documentation:
Image.crop(box=None)Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.
With the same setup:
from PIL import Image
both_image = Image.open(path)
Crop the left image, from (0,0) to (256,256):
img_left = both_image.crop((0,0,256,256))
Cropt the right image,from (256,0) to (512,256):
img_right = both_image.crop((256,0,512,256))
