How to generate a wallpaper with python code
This article mainly introduces how to use python code to generate a wallpaper-related knowledge, detailed and easy to understand, simple and fast operation, with certain reference value, I believe that everyone will have a harvest after reading this article how to use python code to generate a wallpaper article, let's take a look at it.
code import osfrom PIL import ImageFont, ImageDraw, Imageimport cv2import randomdef white2transparent(img): """ Make white transparent :param img: Images that need to be modified :return: Modified image """ # Image reading form---------------------------------------------- # img = img.convert('RGBA') # img = img.convert("RGBA") #Convert format to ensure pixels contain alpha channels #Add a judgment, not four channels will be launched # width, height = img.size #Length and width # for i in range(0, width): #traverse all length points # for j in range(0, height): #traverse all width points # data = img.getpixel((i, j)) #Get a pixel # if (data.count(0) == 4): # RGBA is 255, change to transparent color # img.putpixel((i, j), (255, 255, 255, 255)) # return img #CV2 Reading Form---------------------------------------------- width, height = img.shape[:2] #Length and width for i in range(0, width): #traverse all length points for j in range(0, height): #traverse all width points if (img[i, j, 0], img[i, j, 1], img[i, j, 2]) == (255, 255, 255): img[i, j, 3] = 0 return imgdef merge_img(bg_img, target_img, x1, y1): """ Blend images with background images :param bg_img: Background image, requires 4 channels :param target_img: Target image, requires four channels :param x1: target_img Coordinates in bg_img, if you need to resize the image, please resize outside the function :param y1: :return: """ #Determine if jpg image is already 4 channels if bg_img.shape[2] == 3 or target_img.shape[2] == 3: print("merge image error, image must 4 channels. ") return ''' When superimposing images, the program may report an error because the boundary of the png image exceeds the background jpg image due to improper superposition position setting. Set a series of overlay position restrictions here, which can meet the requirement that png images can still be superimposed normally when they exceed the range of jpg images. ''' 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] #Get the alpha value of the image you want to overwrite, divide the pixel value by 255, keeping the value between 0-1 alpha_target = target_img[yy1:yy2, xx1:xx2, 3] / 255.0 alpha_bg = 1 - alpha_target #Start stacking 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_imgdef set_wallpaper(phone_model, content_list, out_file=None): """ phone_model - phone model content_list - content out_file - output file """ resolution = (0, 0) #Resolution if phone_model == 'iphone se2': resolution = (750, 1334) #Setting the Background 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 #Add statements # font = cv2.FONT_HERSHEY_DUPLEX # cv2 Set font draw_text = ImageDraw.Draw(bg_img) # font = ImageFont.truetype ('C:\Windows\Fonts\simhei. ttf', 30) #Set font, built-in system, can also be downloaded, such as free font font = ImageFont.truetype(r'C:\Users\Administrator\AppData\Local\Microsoft\Windows\Fonts\ImageFont. ttf', 30) i = 0 for content in content_list: # cv2.putText(bg_im, content, (100, 100), font, 5, (254, 67, 101)) # cv2.putText() can only display English characters, Chinese will appear garbled, offset = 120 * i draw_text.text((100, 120 + offset), content, font=font, fill=(0, 0, 0)) i += 1 #Read fonts as cv2 to add pictures bg_img.save('./ background.png') bg_img = cv2.imread('./ background.png', cv2.IMREAD_UNCHANGED) os.remove('./ background.png') #Add icons 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) #Add decoration 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. random (740, resolution[1]-200) # bg_img = merge_img(bg_img, decorate_img, random_pos_x, random_pos_y) #Save pictures if out_file: cv2.imwrite('./ wallpaper.png', bg_img) cv2.imshow('bgim', bg_img) cv2.waitKey(0) cv2.destroyAllWindows()if __name__ == '__main__': content_list = [ "happy every day." 'All my wishes come true' ] set_wallpaper('iphone se2', content_list, './ wallpaper.png') effect
About "how to generate a wallpaper with python code" The content of this article is introduced here, thank you for reading! I believe everyone has a certain understanding of "how to generate a wallpaper with python code." If you still want to learn more knowledge, please pay attention to the industry information channel.