PIL Notebook (basic)
由于使用emacs-org进行编辑,为方便使用了英文
Table of Contents
from PIL import Image
basic usage
(original image)
open and save
>>> im = Image.open('./menhera.jpg')# except: 'IOError'
>>> im.show()
>>> im.size, im.format, im.mode
((2048, 1152), 'JPEG', 'RGB')
>>> im.save('./mhrcopy.jpg')
crop
>>> box = (700, 400, 1100, 1100)# two coordinates
# (col1, row1, col2, row2)
>>> cropped = im.crop(box)
>>> box = (w // 2 - 512, h // 2 - 512, w // 2 + 512, h // 2 + 512)
>>> im.crop(box).resize((512, 512)).show()# simulate central crop
convert
>>> im.convert('L').show()
with numpy and pyplot
both Image and pyplot suggests channels last mode.
arr = np.array(im)
plt.imshow(im)
plt.imshow(arr)
img = Image.fromarray(arr)