python生成壁纸
橙橙小狸猫 人气:0前言
之前在csdn上看见用python写春联的,这次突发奇想用python制作一张壁纸,其元素包括背景、文字、图片。
知识点 用PIL创建一张纯色背景Image.new;图片上添加文字ImageDraw,cv2只能显示英文;转换图片的透明度;图片上叠加图片;jpg与png的区别。
代码
import os from PIL import ImageFont, ImageDraw, Image import cv2 import random def white2transparent(img): """ 将白色部分设置为透明 :param img: 需要修改的图片 :return: 修改后的图片 """ # Image读取形式---------------------------------------------- # img = img.convert('RGBA') # img = img.convert("RGBA") # 转换格式,确保像素包含alpha通道 # 加一个判断,不是四通道就推出 # width, height = img.size # 长度和宽度 # for i in range(0, width): # 遍历所有长度的点 # for j in range(0, height): # 遍历所有宽度的点 # data = img.getpixel((i, j)) # 获取一个像素 # if (data.count(0) == 4): # RGBA都是255,改成透明色 # img.putpixel((i, j), (255, 255, 255, 255)) # return img # CV2读取形式---------------------------------------------- width, height = img.shape[:2] # 长度和宽度 for i in range(0, width): # 遍历所有长度的点 for j in range(0, height): # 遍历所有宽度的点 if (img[i, j, 0], img[i, j, 1], img[i, j, 2]) == (255, 255, 255): img[i, j, 3] = 0 return img def merge_img(bg_img, target_img, x1, y1): """ 将图片与背景图片融合 :param bg_img: 背景图片,要求4通道 :param target_img: 目标图片,要求四通道 :param x1: target_img在bg_img的坐标,如果需要调节图片大小,请在函数外resize :param y1: :return: """ # 判断jpg图像是否已经为4通道 if bg_img.shape[2] == 3 or target_img.shape[2] == 3: print("merge image error, image must 4 channels.") return ''' 当叠加图像时,可能因为叠加位置设置不当,导致png图像的边界超过背景jpg图像,而程序报错 这里设定一系列叠加位置的限制,可以满足png图像超出jpg图像范围时,依然可以正常叠加 ''' x2 = x1 + target_img.shape[1] y2 = y1 + target_img.shape[0] xx1 = 0 yy1 = 0 xx2 = target_img.shape[1] yy2 = target_img.shape[0] if x1 >= bg_img.shape[1]: x1 -= bg_img.shape[1] if y1 >= bg_img.shape[0]: y1 -= bg_img.shape[0] if x2 >= bg_img.shape[1]: xx2 = target_img.shape[1] - (x2 - bg_img.shape[1]) x2 = bg_img.shape[1] if y2 >= bg_img.shape[0]: yy2 = target_img.shape[0] - (y2 - bg_img.shape[0]) y2 = bg_img.shape[0] # 获取要覆盖图像的alpha值,将像素值除以255,使值保持在0-1之间 alpha_target = target_img[yy1:yy2, xx1:xx2, 3] / 255.0 alpha_bg = 1 - alpha_target # 开始叠加 for c in range(0, 3): bg_img[y1:y2, x1:x2, c] = ((alpha_bg * bg_img[y1:y2, x1:x2, c]) + (alpha_target * target_img[yy1:yy2, xx1:xx2, c])) return bg_img def set_wallpaper(phone_model, content_list, out_file=None): """ phone_model - 手机型号 content_list - 内容 out_file - 输出文件 """ resolution = (0, 0) # 分辨率 if phone_model == 'iphone se2': resolution = (750, 1334) # 设置背景 if resolution != (0, 0): bg_img = Image.new('RGBA', resolution, '#fbedb2') # bg_im = cv2.cvtColor(np.asarray(bg_im), cv2.COLOR_RGB2BGR) else: print("don't have this version" ) return # 添加语句 # font = cv2.FONT_HERSHEY_DUPLEX # cv2设置字体 draw_text = ImageDraw.Draw(bg_img) # font = ImageFont.truetype('C:\Windows\Fonts\simhei.ttf', 30) # 设置字体,系统内置,也可下载,如自由字体 font = ImageFont.truetype(r'C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\千图小兔体.ttf', 30) i = 0 for content in content_list: # cv2.putText(bg_im, content, (100, 100), font, 5, (254, 67, 101)) # cv2.putText() 只能显示英文字符,中文会出现乱码问题, offset = 120 * i draw_text.text((100, 120 + offset), content, font=font, fill=(0, 0, 0)) i += 1 # 将包含字体的读取为cv2形式,以便添加图片 bg_img.save('./background.png') bg_img = cv2.imread('./background.png', cv2.IMREAD_UNCHANGED) os.remove('./background.png') # 添加图标 icon_img = cv2.imread('gold.png', cv2.IMREAD_UNCHANGED) white2transparent(icon_img) for j in range(1, len(content_list)+1): offset = 120 * j bg_img = merge_img(bg_img, icon_img, 66, offset) # 添加装饰 decorate_milktea_img = cv2.imread('milktea.png', cv2.IMREAD_UNCHANGED) decorate_fruit_img = cv2.imread('fruit.png', cv2.IMREAD_UNCHANGED) decorate_img_list = [decorate_milktea_img, decorate_fruit_img] for decorate_img in decorate_img_list: white2transparent(decorate_img) random_pos_x = random.randint(0,resolution[0]-300) random_pos_y = random.randint(740, resolution[1]-200) # 范围内的随机数 bg_img = merge_img(bg_img, decorate_img, random_pos_x, random_pos_y) # 保存图片 if out_file: cv2.imwrite('./wallpaper.png', bg_img) cv2.imshow('bgim', bg_img) cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == '__main__': content_list = [ '天天开心', '心想事成' ] set_wallpaper('iphone se2', content_list, './wallpaper.png')
效果
总结
加载全部内容