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

A case study of Python interesting script

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

Share

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

This article mainly introduces the Python interesting script case study, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. The picture size is reduced.

Required libraries:

Opencv-python

We often encounter the problem of zooming in and out of the picture. The script in the following figure shows the use of opencv to reduce the size of the picture.

The fifth line of the program is to read the picture, you can change it to your own picture address.

Line 9-11 program, according to the scaling factor k to reduce the size of the picture in proportion.

Line 14 procedure, through the opencv library resize function to shrink the picture.

The 18-23 line program displays the scaled-down image and saves the scaled-down image locally.

The picture above shows the effect of the zooming out of the picture, and the upper left corner is the zoomed out picture.

2.pdf to txt document

Required libraries:

PyPDF2

Os

As a common document format, pdf needs to extract the text and edit it in many applications. The following script provides to extract the text and write it to the text document.

Lines 5-6 create a folder called "temp"

The 13-22 line program allows users to enter pdf documents and text documents. When the user does not enter a save path for the text document, save the text document to the "temp" folder

23-25 lines of code to convert a read pdf document into a PdfFileReader object

Lines 30-33 use a for loop to extract the text from each page of the pdf document and save it to the txt file.

The extraction results are shown in the following figure.

3. Game of guessing numbers

Required libraries:

Random

The principle of the number guessing game is that the computer randomly generates one of the numbers from 1 to 9, and then the user inputs the data, and the computer gives the judgment of whether the number is high or low.

Although the logic of the number guessing game is simple, it tests the programming ability of beginners very much; the design logic of the game is realized through code. It is recommended that beginners read the code and write it on their own.

Brother Cainiao made a detailed note on the content of the program. Where CHANCE represents the largest number of guesses. The effect of the program is as follows:

4. Battery alarm

Required libraries:

Psutil==5.7.2

Py-notifier==0.1

Win10toast==0.9

The design principle of the program is to use the psutil library to obtain the battery information of the current equipment, including whether to connect the power supply, as well as the battery power information.

The 7-9 line program gets the information about the battery.

In the 11-18 line program, according to the condition judgment of if, if the electricity is less than a certain value and the charger is not connected, a reminder will be issued to the user, please connect the power supply and charge in time.

5. Add a watermark to the picture

Required libraries:

PIL==1.1.6

Adding watermark to the picture can protect the copyright of the picture to a certain extent. More and more friends begin to pay attention to and add watermark content to the picture.

In the program shown above:

Line 6 sets the size and format of the watermark text

Line 7-10 program, using the PIL library to read the picture, and add the background to the picture

The 13-26 line program completes the addition of the watermark. 18-23 line program, through the way of double-layer for loop, add a text watermark for the picture. In order to better match the format of the common watermark, the 25-line program rotates the watermark text 20 degrees counterclockwise.

The 29-line program cuts the watermarked picture to the size of the original picture.

Line 41-53 program, by judging whether the file under the user input folder is a picture format, if it is a picture format, then call the watermark_photo function to add the watermark.

The effect of adding a watermark is shown in the following figure.

Thank you for reading this article carefully. I hope the article "case study of Python interesting script" 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