draw_gif.py
import os
import io
import imghdr
import imageio.v2 as imageio
from PIL import Image, ImageDraw, ImageFont
import numpy as np
from PIL import ImageFont, Image, ImageDraw, ImageSequence
def create_gif(image_list, gif_name, duration=0.35, date='未知时间'):
frames = []
result_list = []
font = ImageFont.truetype('simfang', size=130)
fillColor = (255, 0, 0)
position = (100, 100)
str_ = date
str_ = str_.encode('utf-8').decode('utf-8')
for image_name in image_list:
image_ = Image.fromarray(imageio.imread(image_name))
temp_draw = ImageDraw.Draw(image_)
image_ = np.asarray(image_)
result_list.append(image_)
imageio.mimsave(gif_name, result_list, 'GIF', duration=duration)
def main():
orgin = r'E:\workfiles\temp_img\date_img'
files = os.listdir(orgin)
for file in files:
file_path = os.path.join(orgin, file).replace('\\', '/')
if os.path.isdir(file_path):
image_list = []
for i in os.listdir(file_path):
path = os.path.join(file_path, i).replace('\\', '/')
if imghdr.what(path) == 'png':
image_list.append(path)
image_list = sorted(image_list)
gif_name = os.path.join(orgin, file, file + '.gif')
duration = 0.5
print('gif_name:', gif_name)
print('image_list:', image_list)
create_gif(image_list, gif_name, duration, file)
def watermark_on_gif(in_gif, out_gif, text='scratch8'):
"""本函数给gif动图加水印"""
frames = []
im = Image.open(in_gif)
for frame in ImageSequence.Iterator(im):
d = ImageDraw.Draw(frame)
d.text((50, 100), "Hello World")
del d
b = io.BytesIO()
frame.save(b, format="GIF")
frame = Image.open(b)
frames.append(frame)
newgif = frames[0]
newgif.save(out_gif, save_all=True,
append_images=frames[1:], quality=85, duration=100)
im.close()
if __name__ == '__main__':
main()
draw_gif_v2.py
import os
import io
import imghdr
import imageio.v2 as imageio
from datetime import datetime
import numpy as np
from PIL import ImageFont, Image, ImageDraw, ImageSequence
def create_gif(image_list, gif_name, folder_name, duration=0.35):
frames = []
result_list = []
for image_name in image_list:
image_ = Image.fromarray(imageio.imread(image_name))
temp_draw = ImageDraw.Draw(image_)
if folder_name == '后向轨迹':
font = ImageFont.truetype('simfang', size=60)
fillColor = (255, 0, 0)
position = (30, 30)
str_ = datetime.strptime('2022' + image_name.split('/')[-1].split('-')[1], '%Y%m%d').strftime('%Y-%m-%d')
elif folder_name in ['卫星图-河南', '卫星图-三门峡']:
font = ImageFont.truetype('simfang', size=20)
fillColor = (255, 0, 0)
position = (10, 10)
str_ = datetime.strptime('2022.' + image_name.split('/')[-1].strip('.png'), '%Y.%m.%d').strftime('%Y-%m-%d')
else:
str_ = 'no data'
str_ = str_.encode('utf-8').decode('utf-8')
if position and str_ and font and fillColor:
temp_draw.text(position, str_, font=font, fill=fillColor)
image_ = np.asarray(image_)
result_list.append(image_)
imageio.mimsave(gif_name, result_list, 'GIF', duration=duration)
def main():
orgin = r'E:\workfiles\temp_img\satellite_img'
files = os.listdir(orgin)
for file in files:
file_path = os.path.join(orgin, file).replace('\\', '/')
if os.path.isdir(file_path):
image_list = []
for i in os.listdir(file_path):
path = os.path.join(file_path, i).replace('\\', '/')
if imghdr.what(path) == 'png':
image_list.append(path)
image_list = sorted(image_list)
gif_name = os.path.join(orgin, file, file + '.gif').replace('\\', '/')
duration = 0.5
print('gif_name:', gif_name)
print('image_list:', image_list)
create_gif(image_list, gif_name, file, duration)
def watermark_on_gif(in_gif, out_gif, text='scratch8'):
"""本函数给gif动图加水印"""
frames = []
im = Image.open(in_gif)
for frame in ImageSequence.Iterator(im):
d = ImageDraw.Draw(frame)
d.text((50, 100), "Hello World")
del d
b = io.BytesIO()
frame.save(b, format="GIF")
frame = Image.open(b)
frames.append(frame)
newgif = frames[0]
newgif.save(out_gif, save_all=True,
append_images=frames[1:], quality=85, duration=100)
im.close()
if __name__ == '__main__':
main()
draw_gif_v3.py
import imageio
from PIL import Image, ImageDraw, ImageFont
import numpy as np
image_list = imageio.mimread(r'E:\workfiles\temp_img\2022-05-05\wind.gif')
font = ImageFont.truetype('simfang', size=130)
fillColor = (255, 0, 0)
position = (100, 100)
str_ = '2022-05-06'
str_ = str_.encode('utf-8').decode('utf-8')
print(len(str_))
result_list = []
print(len(image_list))
for index, image_ in enumerate(image_list):
image_ = Image.fromarray(image_)
temp_draw = ImageDraw.Draw(image_)
temp_draw.text(position, str_, font=font, fill=fillColor)
image_ = np.asarray(image_)
result_list.append(image_)
imageio.mimsave(r'E:\workfiles\temp_img\2022-05-05\new_wind.gif', result_list, 'GIF', duration=0.5)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!