温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用Python制作图片

发布时间:2021-06-26 14:41:08 阅读:1449 作者:chen 栏目:编程语言
Python开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要介绍“怎么用Python制作图片”,在日常操作中,相信很多人在怎么用Python制作图片问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Python制作图片”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

制作母亲节词云图-祝福方式1

这个世界只有一个母亲,包容我的一千万任性!

今天我用 Python 制作一个词云图给妈妈送祝福,制作代码如下:

import numpy import multidict import matplotlib.pyplot as plt from scipy.misc import imread from wordcloud import WordCloud, ImageColorGenerator  def transform_format(val):     """     用于去除杂色     Arguments:         val {[array]} -- RGB颜色组     Returns:         [array] -- 去除杂色后的值     """     if val[0] > 245 and val[1] > 245 and val[2] > 245:         val[0] = val[1] = val[2] = 255         return val     else:         return val   def gen_happy_birthday_cloud(file, name):     words = multidict.MultiDict()     # 必须先初始化两个最大权重的     words.add('母亲节快乐'10)     words.add(name, 12)      # 随意插入新的词语     for i in range(1000):         words.add('妈妈', numpy.random.randint(15))         words.add('您辛苦了', numpy.random.randint(15))         words.add(name, numpy.random.randint(15))      # 设定图片     bimg = imread(file)     for color in range(len(bimg)):         bimg[color] = list(map(transform_format, bimg[color]))      wordcloud = WordCloud(         background_color='white',         mask=bimg,         font_path='simhei.ttf'     ).generate_from_frequencies(words)      # 生成词云     bimgColors = ImageColorGenerator(bimg)      # 渲染词云     plt.axis("off")     plt.imshow(wordcloud.recolor(color_func=bimgColors))     plt.savefig(name + '.png')     plt.show()  gen_happy_birthday_cloud("mother.jpg""母亲节快乐")

思路为: 导入一张图片后再输入节日祝福语后进行图片渲染,最后再根据图片形状生成相应词云图。

怎么用Python制作图片

运行结果如下:

怎么用Python制作图片

另一个形状图片生成的词云图:图片

飘落爱心玫瑰-祝福方式2

接下来还要制作一个唯美飘落桃心给妈妈们送祝福!

import time from random import randint  for i in range(135):  # 打印抬头     print('')  heartStars = [2481014202628404452606476]  # *的位置 heartBreakLines = [132741556977]  # 空格的位置 flowerBreakLines = [71523313946]  # 玫瑰的空列位置  def addSpaces(a):  # 添加空列     count = a     while count > 0:         print(' ', end='')         count -= 1   def newLineWithSleep():  # 添加空行     time.sleep(0.3)     print('\n', end='')   play = 0 while play == 0:     Left_Spaces = randint(880)     addSpaces(Left_Spaces)      for i in range(078):  # 比心的形状         if i in heartBreakLines:             newLineWithSleep()             addSpaces(Left_Spaces)         elif i in heartStars:             print('*', end='')         elif i in (3236):             print('M', end='')         elif i == 34:             print('O', end='')         else:             print(' ', end='')      newLineWithSleep()     addSpaces(randint(880))     print("H a p p y  M o t h e r ' s   D a y !", end='')     newLineWithSleep()     newLineWithSleep()      Left_Spaces = randint(880)     addSpaces(Left_Spaces)     for i in range(047):  # 向母亲献花         if i in flowerBreakLines:             newLineWithSleep()             addSpaces(Left_Spaces)         elif i in (281218):             print('{', end='')         elif i in (391319):             print('_', end='')         elif i in (4101420):             print('}', end='')         elif i in (273543):             print('|', end='')         elif i in (3444):             print('~', end='')         elif i == 11:             print('o', end='')         else:             print(' ', end='')      print('\n', end='')

实现效果如下:

怎么用Python制作图片

到此,关于“怎么用Python制作图片”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://mp.weixin.qq.com/s/HJ6o3yUq6r47l4lPiXs_FA

AI

开发者交流群×