from PIL import Image, ImageSequence
from matplotlib import pyplot as plt
im8 = Image.open('/data/demo/img8.gif')
num=0
for frame in ImageSequence.Iterator(im8):
if num <9:
num+=1
plt.subplot(int('33{}'.format(num))),plt.imshow(frame)
from PIL import ImageFont,ImageDraw
img=Image.open('/data/demo/L1020120.JPG')
draw=ImageDraw.Draw(img)
ImageFont.truetype
参数是设置字体类型以及字体大小。
font=ImageFont.truetype('VeraMoBI.ttf',40)
draw.text((100, 100), 'Pillow',(150,200,0),font=font)
plt.imshow(img)
plt.show()
--------------------------------------------------------------------------- OSError Traceback (most recent call last) Cell In[4], line 1 ----> 1 font=ImageFont.truetype('VeraMoBI.ttf',40) 2 draw.text((100, 100), 'Pillow',(150,200,0),font=font) 3 plt.imshow(img) File /opt/conda/lib/python3.12/site-packages/PIL/ImageFont.py:879, in truetype(font, size, index, encoding, layout_engine) 876 return FreeTypeFont(font, size, index, encoding, layout_engine) 878 try: --> 879 return freetype(font) 880 except OSError: 881 if not is_path(font): File /opt/conda/lib/python3.12/site-packages/PIL/ImageFont.py:876, in truetype.<locals>.freetype(font) 875 def freetype(font: StrOrBytesPath | BinaryIO) -> FreeTypeFont: --> 876 return FreeTypeFont(font, size, index, encoding, layout_engine) File /opt/conda/lib/python3.12/site-packages/PIL/ImageFont.py:284, in FreeTypeFont.__init__(self, font, size, index, encoding, layout_engine) 282 load_from_bytes(f) 283 return --> 284 self.font = core.getfont( 285 font, size, index, encoding, layout_engine=layout_engine 286 ) 287 else: 288 load_from_bytes(cast(IO[bytes], font)) OSError: cannot open resource
用前面讲到的复制粘贴,添加图片相当于添加了图片水印效果。