Python makes a gif emoticon package generation tool, and Doutu will never lose again

In the current era when there are no emoji packs and you can’t chat, how can you not lose in the lack of emoji packs? Today we will make a gif generator tool, which is also very useful for making emoji packs.

First look at the overall effect

picture

Technology Exchange

Technology must learn to share and communicate, and it is not recommended to work behind closed doors. One person can go fast, and a group of people can go farther.

This article is shared and recommended by fans, dry data, data sharing, data, and technical exchange improvements, all of which can be obtained by adding to the exchange group. The group has more than 2,000 members. The best way to add notes is: source + interest direction, which is convenient Find like-minded friends.

Method ①, add WeChat account: pythoner666, remarks: from CSDN + add group
Method ②, WeChat search official account: Python learning and data mining, background reply: add group

page design

The page of the tool in this article is designed by Qt Designer. We can find the EXE executable file designer.exe directly in the directory where PyQt5-tools is installed, and execute it directly. For example, here is the following directory

C:\Python3\Lib\site-packages\qt5_applications\Qt\bin\designer.exe

Of course, it can also be configured directly into PyCharm, such as this

picture

The configuration here is just a one-shot. If there are friends who fail to configure it, they can learn about it privately or directly on Baidu.

After we open the Qt Designer tool, we can see the following page

picture

At this point, we only need to properly drag and drop to complete the layout of the page

picture

Let's briefly talk about the components used in the above layout

  • The components with text "select image" and "generate gif" are two QPushButtons

  • Below the "Select Image" button is a QListVIew component

  • The next three paragraphs of text are the QLabel components, followed by the corresponding QLineEdit components

  • Below the "Generate gif" button are also two QLabel components, which are used to preview the picture and the author's statement respectively

In this way, after we complete the layout, save the current configuration, for example, save it as "gif.ui"

Next we use PySide2 to implement page logic, so we need to install this library first

pip install PySide2

After the installation is complete, we can directly enter the following command on the cmd command line to convert the newly generated ui file into py code

pyside2-uic -o gif.py gif.ui

Such a page layout is completed, and the converted page layout py file basically does not need to be modified, it can be directly quoted

core logic

Next, we write the core logic, create a main.py file in the same directory as the generated py file, and then refer to the content of the newly generated py file

from gif import Ui_Form
from PySide2.QtWidgets import QMainWindow, QApplication, QFileDialog


class CreateGif(QMainWindow, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setup()
        self.show()

Next, we set the setup function to set the initial value for the corresponding component

    def setup(self):
        self.imgs = None
        self.gifImgName = None
        self.lineEdit.setText("0")
        self.lineEdit_2.setText("0")
        self.lineEdit_3.setText("500")

Then write the function to select the picture

    def choose_img(self):
        self.imgs = QFileDialog.getOpenFileNames(
            caption="选择图片", filter="")[0]  # (*.png, *.jpg, *.PNG)
        if self.imgs:
            imgwidth, imgheight = Image.open(self.imgs[0]).size
            self.lineEdit.setText(str(imgwidth))
            self.lineEdit_2.setText(str(imgheight))
            new_imgs = []
            for img in self.imgs:
                img_name = os.path.basename(img)
                new_imgs.append(img_name)
            strings = QStringListModel(new_imgs)
            self.listView.setModel(strings)

First, get the picture file through the method provided by QFileDialog, then use the PIL library to read the size of the picture, and update the two lineEdit component values, and finally display all the selected pictures in the listView component

picture

Here is the function to generate gif

    def gen_gif(self):
        frames = []
        imgwidth = int(self.lineEdit.text())
        imgheight = int(self.lineEdit_2.text())
        imgspeed = int(self.lineEdit_3.text())
        for img in self.imgs:
            img = Image.open(img).resize((imgwidth, imgheight)).convert("RGBA")
            frames.append(img)
        self.gifImgName = os.path.splitext(os.path.basename(self.imgs[0]))[0]
        frames[0].save(f"{
      
      self.gifImgName}.gif", append_images=frames[1:], loop=0, save_all=True, duration=imgspeed)
        self.displayGif()

It's not complicated. Name the gif image to be generated by the first name of the selected image, and then save the rest of the images to the first image through the save method, and select gif as the type.

The last is to show the code to generate gif

    def displayGif(self):
        self.movie = QMovie()
        self.movie.setFileName(f"{
      
      self.gifImgName}.gif")
        self.label.setMovie(self.movie)
        self.movie.start()

Use the QMovie method in PySide2.QtGui to display the gif image in the label

In this way, this little gif making tool is completed, let's do it together soon!

If you think it's not bad, give it a thumbs up ~

Guess you like

Origin blog.csdn.net/m0_59596937/article/details/131624355