Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Portrait Snow scene Mini Program based on Python

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to achieve portrait Snow scene Mini Program based on Python, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Text

Start making snow scenes:

1) material environment (partial only)

2) operating environment

The environment involved in this paper: Python3, Pycharm, Pygame, PIL modules and some of their own modules.

Module installation: what we are used to use, the use of mirror source installation is faster and less easy to report errors.

Pip install + module name or pip install-I https://pypi.douban.com/simple/ + module name with Douban mirror source 3) Code demonstration

3.1 Import module

Import pygameimport randomimport osfrom PIL import Imagefrom pygame.sprite import Spritefrom pygame.sprite import Groupfrom PIL import ImageGrabimport shutil

3.2Defining snowflake ❄ class, snowflake falling location, size, etc.

# class Snow (Sprite) for a single snowflake: def _ _ init__ (self, image, pos, speed, size, screen): super (). _ _ init__ () self.screen = screen self.speed= speed self.pos = pos self.image = pygame.transform.scale (image Size) self.rect = self.image.get_rect () self.rect.x = pos [0] self.rect.y = pos [1] def blitme (self): self.screen.blit (self.image) Self.rect) def update (self): self.rect.x + = self.speed [0] self.rect.y + = self.speed [1] # Snowflake rotation self.image = pygame.transform.rotate (self.image 90) if self.check_edges (): self.rect.x = self.pos [0] self.rect.y = self.pos [1] def check_edges (self): screen_rect = self.screen.get_rect () if self.rect.top > = screen_rect.bottom: return True return False def add_snow (path): pygame .init () size = Image.open (path). Size screen = pygame.display.set_mode (size Pygame.NOFRAME) s = pygame.display.get_surface () bg = pygame.image.load_extended (path). Convert () screen.blit (bg, (0,0)) # load snowflake picture snow_image = pygame.image.load_extended ('snow.png') snow_group = Group () for i in range: # snowflake start position pos = (random.randint (- size [0]) Size [0]), random.randint (- size [1], 0) # Control snowflake size n = random.randint (4,12) snow_size = (n, n) # snowflake falling speed speed = (2, random.randint (2,7)) snow_group.add (Snow (snow_image, pos, speed, snow_size, screen)) clock = pygame.time.Clock ()

Create a "frames" folder to save each picture

If not os.path.exists ("frames"): os.makedirs ("frames") flag = True num = 1 While flag: for event in pygame.event.get (): # exit window if event.type = = pygame.QUIT: flag = False screen.blit (bg, (0) 0) for snow in snow_group.copy (): snow.blitme () snow_group.update () # Save the current screen pygame.image.save (screen) "frames\" + str (num) + ".jpg") # Refresh screen pygame.display.update () # set fps clock.tick (30) if num > = 250: break num + = 1

3.4.The effect of making GIF diagram is more intuitive.

Im = Image.open ("frames\ 1.jpg") images = [] size = (int (im.size [0] / 2), int (im.size [1] / 2)) for file in range (2, num + 1): filepath = "frames\" + str (file) + ".jpg" temp = Image.open (filepath) temp = temp.resize (size) Image.ANTIALIAS) images.append (temp) im = im.resize (size, Image.ANTIALIAS) im.save ('snow.gif', save_all=True, append_images=images, loop=2, duration=5) 4) effect display

There is no need to say much about the picture folder of each frame, just show you a column. The rest just show the generated GIF diagram.

4.1 Super cute Miss Sister Snow scene.

-- the original picture is as follows:

-- the effect picture of GIF is as follows:

-- Pictures for each frame .jpg

4.2 Snow scene picture collection

All the pictures shown here are effect pictures, and the original pictures have been uploaded lazily! Too many pictures can not be uploaded, found a lot of ways, Nuggets passed and then straight

Then copy to csdn, so sprinkle with watermark! Maybe the picture I'm looking for is too high-definition.

Thank you for reading this article carefully. I hope the article "how to implement Portrait Snow Mini Program based on Python" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report