Python库用PIL创建引用/歌词和tweet图形。
quotesp的Python项目详细描述
报价单
Python库创建引用/歌词和tweet图形与PIL
它可以通过pip使用pip install quotespy
安装。
使用
引语/歌词图形
使用默认设置为歌词创建图形(.png),保存在当前目录中:
importquotespy.graphics.graphicsasggraphic_info={"title":"strange_days","text":"Say goodbye to the silence, we can dance to the sirens"}g.create_graphic(graphic_info,{},default_settings_format="lyrics")
我建议您也试试“quote”default_settings_format
选项。
或者,可以指定自定义图形设置并忽略默认设置选项(注意,如果同时指定了自定义设置和默认设置,则选择自定义设置而不是默认设置)。
^{pr2}$另外,在第二个例子中,还指定了保存创建的图形的路径。
请注意,示例中为graphic_info
和custom_settings
显示的所有字段/键都是必需的。
如果您有一个带有多个歌词/引号的.txt或.json文件,您还可以加载它并使用单个函数创建单个图形,只需指定源文件的路径和图形设置(自定义或默认格式)。
importquotespy.graphics.graphicsasgg.gen_graphics("samples\\lyrics.txt",{},default_settings_format="lyrics",save_dir="some_path")
有关这些.txt和.json源文件所需的文本格式的详细信息,请参阅此存储库中的samples文件夹。它包含示例文件。
Tweet图形
Tweet图形的工作原理与graphics
对应图形基本相同。最大的区别是它使用了一个不同的模块,字典需要两个额外的字段。
从最基本的用法开始:
importquotespy.tweet_graphics.tweet_graphicsasttweet_info={"tweet_name":"mistakes","user_name":"José Fernando Costa","user_tag":"@ze1598","user_pic":"user_photo2.png","tweet_text":"Some mistakes and, dare I say, failures may lead to results you had never thought you could achieve."}t.create_tweet(tweet_info,{},default_settings_format="blue",save_dir="some_path")
就像另一个模块有自己的默认设置格式一样,tweets有三个选项,它们在配色方案上差别很大:“蓝色”、“浅色”和“深色”。
tweet_info
字典中的“user_pic”键的值可以是指向.png文件的路径,也可以保留为空字符串。换句话说,在图形中有一个配置文件图片是可选的,但是dicitonary必须始终拥有密钥。另外,请注意,图片是通过将其尺寸减小到图形尺寸的10%进行预处理的,并使用圆形裁剪。
如果要使用自定义图形设置,可以参考以下示例:
importquotespy.tweet_graphics.tweet_graphicsasttweet_info={"tweet_name":"mistakes","user_name":"José Fernando Costa","user_tag":"@ze1598","user_pic":"user_photo2.png","tweet_text":"Some mistakes and, dare I say, failures may lead to results you had never thought you could achieve."}graphic_settings={"font_family":"arial.ttf","font_size_text":100,"font_size_header":80,"size":[1800,1800],"color_scheme":["#000000","#ffffff"],"wrap_limit":32,"margin_bottom":30}t.create_tweet(tweet_info,graphic_settings)
而且,与create_graphics
模块一样,您也可以批量生成tweet图形,但这次只能从.json源文件生成:
importquotespy.tweet_graphics.tweet_graphicsastt.gen_tweets("samples\\tweets.json",{},default_settings_format="dark")
v1.2中的新功能:透明背景
从1.2版开始,quotespy现在接受RGBA颜色字符串来创建透明背景。红色、绿色和蓝色通道是介于0和255之间的整数,alpha/透明度值是介于0和1之间的浮点值。
importquotespy.tweet_graphics.tweet_graphicsasttweet_info={"tweet_name":"mistakes","user_name":"José Fernando Costa","user_tag":"@ze1598","user_pic":"user_photo2.png","tweet_text":"Some mistakes and, dare I say, failures may lead to results you had never thought you could achieve."}graphic_settings={"font_family":"arial.ttf","font_size_text":100,"font_size_header":80,"size":[1800,1800],"color_scheme":["rgba(255, 255, 255, 0)","#ffffff"],"wrap_limit":32,"margin_bottom":30}t.create_tweet(tweet_info,graphic_settings)
或者,None
可以作为背景色传递,以创建透明的背景。这些新的颜色选项可用于tweet_graphics
和graphics
。
v1.3中的新功能(tweet_图形):自定义个人资料图片大小
从quotespy 1.3开始,可以指定要裁剪轮廓图片的尺寸。默认情况下,图片被裁剪为图形宽度和高度的十分之一。
在下面的示例中,配置文件图片将被裁剪为120x120大小,由graphic_settings
中的profile_pic_size
键指定。
importquotespy.tweet_graphics.tweet_graphicsasttweet_info={"tweet_name":"mistakes","user_name":"José Fernando Costa","user_tag":"@ze1598","user_pic":"user_photo2.png","tweet_text":"Some mistakes and, dare I say, failures may lead to results you had never thought you could achieve."}graphic_settings={"font_family":"arial.ttf","font_size_text":40,"font_size_header":25,"size":[700,700],"profile_pic_size":[40,40],"color_scheme":["#fff","#000"],"wrap_limit":32,"margin_bottom":20}t.create_tweet(tweet_info,graphic_settings)
但是,如果您想坚持默认的裁剪尺寸,那么可以在profile_pic_size
列表中传递两个None
。这在下面的示例中显示。
importquotespy.tweet_graphics.tweet_graphicsasttweet_info={"tweet_name":"mistakes","user_name":"José Fernando Costa","user_tag":"@ze1598","user_pic":"user_photo2.png","tweet_text":"Some mistakes and, dare I say, failures may lead to results you had never thought you could achieve."}graphic_settings={"font_family":"arial.ttf","font_size_text":40,"font_size_header":25,"size":[700,700],"profile_pic_size":[None,None],"color_scheme":["#fff","#000"],"wrap_limit":32,"margin_bottom":20}t.create_tweet(tweet_info,graphic_settings)
自定义配置文件图片大小的注意事项:
- 图片在标题中没有垂直对齐
- 宽度和高度必须使用相同的值(即,它必须是正方形)。
示例
最后,我想向您展示这个tweet_graphics
模块的一些“高级”用法(希望它也能作为graphics
模块的灵感):
importquotespy.tweet_graphics.tweet_graphicsastimportosSAVEDIR="imgs"USERNAME="José Fernando Costa"USERTAG="@ze1598"# List of `tweet_info` dictionariestweets=[{"tweet_name":"compare_to_others_sometimes","user_name":USERNAME,"user_tag":USERTAG,"user_pic":"","tweet_text":"Compare yourself to others once in a while (using a reasonable scale!). If you completely isolate yourself you will end up working aimlessly without ever knowing when it is enough or how much you've improved."},{"tweet_name":"merit_in_positives","user_name":USERNAME,"user_tag":USERTAG,"user_pic":"","tweet_text":"There is merit in talking about the positive aspects of terrible situations. It helps those going through the experience to see a glimpse of light at the end of the tunnel and it may help others who go through the same experience in the future."},{"tweet_name":"write_down_ideas","user_name":USERNAME,"user_tag":USERTAG,"user_pic":"","tweet_text":"Write down ideas that pop up in your head in a reliable place (note-taking app, physical notebook, etc.). We often come up with the ideas or inspiration we are looking for when we least expect it, but it's easy to let them escape."}]# Get all the titles (tweet names) from the previous listtitles=[tweet["tweet_name"]fortweetintweets]# Directory in which to save graphicsPATH="some_path"# Create custom light and dark mode settings# With `None` for the profile picture size it will default to be resized to one tenth of the graphic's sizes_light={"font_family":"arial.ttf","font_size_text":80,"font_size_header":70,"size":[1800,1800],"profile_pic_size":[None,None],"color_scheme":["#ffffff","#000000"],"wrap_limit":36,"margin_bottom":30}s_dark={"font_family":"arial.ttf","font_size_text":80,"font_size_header":70,"size":[1800,1800],"profile_pic_size":[None,None],"color_scheme":["#000000","#ffffff"],"wrap_limit":36,"margin_bottom":30}# Create a graphic for each `tweet_info` in the listfortweetintweets:tweet_name=tweet["tweet_name"]# Each tweet is stored in its own foldertweet_path=os.path.join(PATH,tweet_name)os.system(f"mkdir {PATH}\\{tweet_name}")# And each folder has light and dark mode versions of the tweett.create_tweet(tweet,s_light,save_dir=tweet_path)tweet["tweet_name"]=tweet_name+"_DM"t.create_tweet(tweet,s_dark,save_dir=tweet_path)
- 项目
标签: