current position:Home>The mobile phone uses Python to operate picture files

The mobile phone uses Python to operate picture files

2022-01-29 16:00:54 User 8590376373986

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .



A few days ago, I went to the country to take a picture of a book , A book on psychology , Maybe you asked me why I didn't buy a , Or go to the Internet pdf. In fact! , It can be said that there are a lot of psychological books in the market , There are two reasons for taking this book , One is that there is no collection value and it's not worth buying , Only for emergency use , Then there is the author's writing characteristics of this book which are different from those of other people , It can be said that there are characteristics or side doors , So I took a bunch of pictures on my cell phone , Later it was sorted out as pdf, But yesterday when I read the original picture file is still nearly a thousand , It's annoying to delete one by one , Maybe you'll say why don't you import it to the computer and delete it , I can't help but think it's totally different , To learn the python I just like the whole operation .( Manual funny ) So I'll use it on my cell phone python Carry out a wave of operation ...

The preparation of the instruments

First you have to have a cell phone ,233( Just like I didn't say ), Cough Seriously , Now that I'm running on my phone python 了 , Here I recommend pydroid3, Because the code is based on python3.6 Written (f-string Use ) For your convenience , I provide you with Baidu cloud download address : Official account :python Learning to develop , The background to reply :pydroid that will do . It looks like this after the installation is opened


All that's left is to run the code , Let's talk about this later .


We need to know how to do it before we do it . 1. Determine the directory of the image file to be processed 2. Then decide which pictures to delete , Here we take the creation date of the image as the basis for screening . 3. To prevent the deletion of , For security, create a... In this folder trash Catalog , First move the target image to the folder , Then confirm that the picture can be deleted before deleting . Just like don't use it easily rm equally , Do you know . Leave yourself a time to repent .

Code implementation

OK, now that we've got the idea, we can do it . 1. Confirm the file folder , Use here pathlib modular ,pathlib yes python3 A standard library in , It's mainly the operation path , and os Module operation path than to say the difference , The splicing path is sometimes more convenient , The best way is to use the two together . OK, here's the code

import pathlib
dir_path = pathlib.Path(filename).resolve().parent
 Copy code 

resolve The method is to get the directory where the file runs ,parent Is to get the upper level directory , That is, the folder where the pictures are located 2. establish trash Catalog , The directory of the moved pictures . The purpose of this folder mentioned in the thought analysis is to delete it after confirming the garbage can .

 trash_dir = dir_path.joinpath("trash")
 Copy code 

We can go through joinpath Method for splicing path ,os.path.join() You need to fill in two parameters , Only one is needed here , That's the advantage . However, it should be noted that the final path obtained by this method is an object, not a string In later use, you need to convert the path to string form . 3. Next we know that the type of file we need to operate is picture , Here I use jpg For example , Of course if your picture is png You can write png,python3 The standard library of provides glob modular , This module can set matching rules to get the list of images to be matched .

import glob
image_path_list = glob.glob(to_str_path(dir_path.joinpath("*.jpg")))
 Copy code 

It was said that the path obtained is an object, so to_str_path It's a way to convert to a string path . *.jpg Can match so suffix is jpg List of production results of documents .

4. All the documents are obtained, but not all of them are what we need , We need documents within the specified date

import os
file_time = os.path.getctime(image_path)
 Copy code 

adopt getctime We got the creation time of the picture , Further screening . 5. The papers are all ready , We need to move to trash Catalog , Waiting to be processed . adopt os Modular rename Methods or shutils Of move Method can move files , So here I'm going to choose os modular

import os
os.rename(" Full file path before move ", " Full file path after move ")
 Copy code 

6. At this point the document is in trash Catalogue . Then you can delete it after confirming , If there's no problem, execute delete Method . There are two ways to delete. One is to delete all files , One is to delete the entire folder , The only difference between the two is that you don't need to create trash Catalogue , No big difference .

 if del_type == "dir":
        shutil.rmtree(trash_path)  #  Recursively delete a directory and all contents in the directory 
        if os.path.exists(trash_path):
            file_list = os.listdir(trash_path)
            if file_list:
                remove_f = partial(remove, trash_path=trash_path)
                start_thread(remove_f, file_list)
 Copy code 


First of all, determine the directory where the files need to be sorted . Take my previous screenshot directory for example . There is a small problem here , How do I know which directory my screenshot file is in …. Generally, there is a detailed information when the mobile phone views the picture, and there is a path in it . This way down .


Through the pictures we can see the path clearly in the screenshot folder , At this point, we have determined the directory of the files .

Then, for the convenience of us, we execute in the directory where we need to operate python Script , Open the... We installed earlier pydroid3 Then follow my instructions 1. Copy the code into the box below


You can put newfile Change one's name .

2. Call the method to pass in parameters

Method to pass in a date parameter format such as 20190101.

Here we use 20171227 Run the code for example .

No results after running , After a look at the picture, I know , The default image format of the program is jpg, My screenshots are all png, So I need to specify filetype Parameter is png, The same is date_str Parameter set to 20171012.

Run code :


After that, you will find that the specified file is already in trash Catalogue ,


3. After that, we can execute delete Method deleted .


Only this and nothing more , This is the end of the tutorial , Originally intended to be written as a multi-threaded version of speeding up, it turned out that the mobile phone didn't respond , So it's better to use circular operation files , This is just a way of thinking , The code is not that hard . The key is to see the purpose , How did you learn .

The original address comes from my Zhihu column

copyright notice
author[User 8590376373986],Please bring the original link to reprint, thank you.

Random recommended