Python programming: Use Pillow to convert photos into 1-inch registration photos

Introduction:
In the modern technological age, we often need to adjust and process photos to suit specific needs and uses. This article will introduce how to use wxPython and Pillow libraries to convert selected photos into JPG format of specified size through a simple graphical interface program and save it on the desktop.

C:\pythoncode\new\1inchpicture.py

Preparation

Before starting, make sure you have the wxPython and Pillow libraries installed. You can install via pip using the following command:

Copy

pip install wxPython
pip install Pillow

Write code

We will use Python to write a graphical interface program, use the wxPython library to create windows and buttons, and use the Pillow library for image processing.

import wx
import os
from PIL import Image


class ImageConverter(wx.Frame):
    def __init__(self, parent, title):
        super(ImageConverter, self).__init__(parent, title=title, size=(400, 200))

        panel = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)

        self.file_picker = wx.FilePickerCtrl(panel, message="选择照片", wildcard="JPEG files (*.jpg)|*.jpg",
                                             style=wx.FLP_USE_TEXTCTRL)
        vbox.Add(self.file_picker, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)

        export_button = wx.Button(panel, label="Export")
        export_button.Bind(wx.EVT_BUTTON, self.on_export)
        vbox.Add(export_button, proportion=1, flag=wx.ALIGN_CENTER | wx.ALL, border=10)

        panel.SetSizer(vbox)
        self.Center()

    def on_export(self, event):
        file_path = self.file_picker.GetPath()
        if file_path:
            image = Image.open(file_path)
            resized_image = image.resize((400, 500))  # 1 inch = 72 pixels
            desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop')
            save_path = os.path.join(desktop_path, "converted_image.jpg")
            resized_image.save(save_path, "JPEG")
            wx.MessageBox("照片已转换并保存在桌面上!", "导出成功", wx.OK | wx.ICON_INFORMATION)


if __name__ == '__main__':
    app = wx.App()
    frame = ImageConverter(None, "Image Converter")
    frame.Show()
    app.MainLoop()

parse code

  1. Import the necessary libraries: We imported the wxPython library and Pillow library.

  2. Create a window and button: Use wxPython to create a window and a "Select Photo" file picker and an "Export" button.

  3. Image conversion and saving: When the "Export" button is clicked, we will get the selected photo path and open the photo using the Pillow library. We will then resize the image to the specified 1-inch dimensions (72 pixels x 96 pixels) and save the converted image using a desktop path.

Run program

Run the program and a simple window will open. Click the "Select Photo" button, select the JPG photo you want to convert, and then click the "Export" button. The selected photo will be converted to the specified size and saved in  converted_image.jpg a file on the desktop.

in conclusion

Using wxPython and the Pillow library, we can easily create a graphical interface program that selects photos and converts them to specified sizes. This example is just a simple starting point and can be further customized and extended to suit your needs.

This technology can not only be used for personal image processing needs, but can also be applied to many practical scenarios such as batch processing of photos and automated image processing processes.

Guess you like

Origin blog.csdn.net/winniezhang/article/details/133281323