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 make a scalable Santa Claus with Python

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to use Python to do a zoom Santa Claus, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Christmas is coming again, although we Chinese do not advocate Western festivals, but businesses still like it, and it is estimated that boys and girls who have a date also like it.

Today's theme is to show you how to use python to make a growing Santa Claus, just like the fairies and monsters who can change their size at will in Journey to the West, it is a small gift for everyone. Let's take a picture first.

Don't be impatient, stare at the picture for 5 seconds

Key points of thinking:

Get a group of pictures of equal size by zooming

Overlay the above image into a fixed-size base image

Assemble pictures sequentially to generate dynamic pictures

1. Zoom the picture

Most of the work in this article is based on opencv implementation, and it is extremely easy for opencv to zoom images, but this time we are going to generate a group of images with proportional scaling, so the use of cv2.resize method may be slightly different from that in the past, let's take a look at the function prototype:

Cv2.resize (src, dsize [, dst [, fx [, fy [, interpolation])

Src is the original image and dsize is the target image size. When dsize is 0, we can set the zoom ratio of horizontal axis and vertical axis respectively through fx and fy parameters. This may be a little abstract, let's give an example to illustrate:

For i in range (1,40,1): img = cv2.resize (image, (0,0), fx=i/30, fy=i/30) cv2.imwrite (str (I) + '.png', img)

Running the above code will generate 39 images of different proportions. The size of the target image is controlled by the zoom ratios fx and fy. The smallest image edge length is 30 times that of the original image, and the largest image edge length is 1.3 times that of the original image (below):

Now that we have a proportionally scaled picture, is it possible to select a coordinate origin and directly synthesize the dynamic picture? The answer is no, because the conventional dynamic image generation method requires that the material image must be the same size (pixels), let's focus on solving this problem.

2. Overlay of base map

There are many ways to overlay two images in python, but they all have drawbacks-either the superimposed images must be the same size, or it is difficult to control the exact location of the superimposed images. In this regard, the editor's approach is to replace the "pixel level" between the two images.

1)。 Generate a base map

In the picture to be superimposed, the upper picture uses a series of proportional zooms just obtained, and the lower picture generates a blank picture of a fixed size. It should be noted that the blank image generated here must be larger than the largest thumbnail.

The blank background image is generated in two steps: the first step is to generate a two-dimensional array of fixed size (the length of vertical axis and horizontal axis); the second step is to use cv2.cvtColor for color space transformation. The code is as follows:

Blank = np.ones ((blankh, blankw), dtype=np.uint8) * 255ret = cv2.cvtColor (blank, cv2.COLOR_GRAY2BGR)

In fact, the ret in the above code is essentially a three-dimensional array, which we can print out and view (below), but it is a blank image shown by the cv2.imshow method. This involves some relatively low-level content, as long as we understand it, I will not repeat it in the article.

2)。 Pixel replacement

As I just said, a picture in opencv is actually a three-dimensional array, and it can also be thought of as a two-dimensional array.

Each element is a list in the shape of [255,255,255], in which the color parameters of each pixel of the picture are stored. That is, if we want to achieve the visual effect of superimposing one picture on another, we can match the position of the superimposed image.

The pixel is replaced and assigned. The code form is shown in the following figure, where I and j are the vertical and horizontal coordinates of the picture, respectively.

Ret [I, j, 0] = image [I, j, 0] ret [I, j, 1] = image [I, j, 1] ret [I, j, 2] = image [I, j, 2]

For a picture, the coordinate origin is in the upper-left corner (shown in the following image). In addition, in order to ensure the final effect of the dynamic picture, the picture can not be simply superimposed on the basis of the coordinate origin. A better way is to set the overlay origin at the center of the lower edge of the base map.

After the principle is clear, you can start the image overlay operation. During this period, you need to calculate the corresponding position of some pixels. Although it is a little winding, it is not complicated. The detailed conversion formula will not be written. Let's just look at the code:

The image in the above code is the scaled picture of Santa Claus. Blankh and blankw are the height and width of the blank image, respectively. This size can be set according to your needs.

The following image shows the superimposed effect of a picture with a scale of about 1max 2 and the base image. For convenience of observation, I added a frame to the picture.

3. Generate dynamic diagrams

We have solved the overlay of a single image and the base image before. in order to prepare the material needed to synthesize the dynamic image, we also need to overlay multiple proportional scaled images. The smaller the zoom interval and the more picture material you prepare, the smoother the resulting motion picture will be.

Of course, the effect of the dynamic picture should also take into account a number of factors, here the editor still uses a combination of 39 pictures. The smallest figure height is 30 times that of the original image, and the maximum figure height is 1.3 times that of the original image. The picture superimposed with the base image looks like this.

Let's talk about the composition of dynamic images. The library imageio can be used to synthesize multiple images of the same size. There is only one core code:

Imageio.mimwrite ('target file name .gif', gifList, duration=0.15)

The first parameter is the name of the git target file; gifList is a group of images to be synthesized, that is, those shown in the image above; and the last parameter, duration, indicates the screen switching interval in seconds.

Now use the following code for dynamic graph synthesis.

File_path = 'pic' imgList = os.listdir (file_path) imgList = [' pic/'+img for img in imgList] gifList = [imageio.imread (img) for img in imgList] imageio.mimwrite ('gif.gif', gifList, duration=0.15)

Let's take a look at the effect of the composite dynamic picture (below). If you take a closer look at it, there seems to be something wrong with it. How can Santa Claus in the picture change from big to small? This is not what we expected.

In fact, the problem lies in the order in which the images are synthesized. We try to print the imgList variable in the above code, and the result is as follows:

As you can see, the footage pictures are not sorted in the order we expected. This is also a common problem in python file processing. One of the solutions is to sort the images according to the creation time, by inserting a statement after the second line of code above:

ImgList = sorted (imgList,key=lambda x: os.path.getmtime (os.path.join (file_path, x)

Now carry on the dynamic picture synthesis again, you can achieve the effect at the beginning of the article.

Of course, this method of making dynamic pictures is not limited to Santa Claus, any picture is theoretically possible. For example, we can also make a growing Christmas tree!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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