Python filters pictures suitable for wallpaper by size

The idea is very simple, the process is very twists and turns
1. Find the required libraries and methods: directory setting, file reading, size acquisition, file movement
2. debug: file removal, file reading error, etc.

import os,shutil
from PIL import Image

l=l1=os.listdir() # listdir可以读取到子目录和隐藏文件等,于是使用下面的集合推到式过滤出后缀名,这是之后的,之前还或其了ini,通过后缀名过滤掉不需要的文件名/目录名,其实只有两个,一个是子目录,一个是隐藏的ini文件

{
    
    i.split('.')[1] for i in l if i.find('.')>0}
Out[78]: {
    
    'gif', 'jpg', 'png'}

for i in l1:
    if not ('jpg' in i or 'gif' in i or 'png' in i or 'gif' in i):l.remove(i)

for f in l:
    img=Image.open(f).size
    if img[0]>img[1] and img[1]>=1080:
        shutil.move(f,'./CoolMarket+')
# 这个是所有的横大于竖,竖尺寸大于1080的文件,将其移动到子目录

i=0
while i<lens:
    img=Image.open(l[i]).size
    if img[0]>img[1] and img[1]>=1080:
        shutil.move(f,'./CoolMarket+')
    i+=1
# 这个是用i和while的方法,因为我遇到了提醒,但之后发现提醒并没有停止程序运行,使用这个方法的妙处在于,知道i在那里出错,而且之后可以继续i之后的运行,调试用

packages\PIL\TiffImagePlugin.py:793: UserWarning: Corrupt EXIF data.  Expecting to read 4 bytes but only got 0. 
  warnings.warn(str(msg))
# 这个是我上面提到的跳出的情况,正如上所说,warning并不会中止程序,只是提醒而已

Insert picture description here
Casually speaking, for the webp disguised as png, windows does not display its resolution information, nor can it form its preview, nor can it be used as a wallpaper, so windows currently does not support webp and needs to be converted.

Insert picture description here
After that, you only need to select suitable ones from these pictures to get and remove inappropriate pictures, and become wallpaper packs. For example, some non-16:9 pictures are automatically processed by the system and the center of gravity is inappropriate, such as a few pictures of Cat Boss and Gus’s. That one.

There is a very low-level bug on it. If you see that and mistakenly believe it is true, and you don’t see it, I’ll say sorry to you mischievously:

os.listdir()
Out[27]: 
['System Volume Information',
 '1.txt',
 '1 - 副本 (2).txt.txt',
 '1 - 副本 (3).txt.jpg',
 '1 - 副本 (4).txt',
 '1 - 副本 (5).txt',
 '1 - 副本 (6).txt',
 '1 - 副本 (2).txt.png']

l=l1=os.listdir()

c=0
for i in l1:
    c+=1
    if not ('jpg' in i or 'jpeg' in i or 'png' in i or 'gif' in i):l.remove(i)
    
c
Out[32]: 4

l
Out[33]: ['1.txt', '1 - 副本 (3).txt.jpg', '1 - 副本 (5).txt', '1 - 副本 (2).txt.png']

Guess you like

Origin blog.csdn.net/jhsxy2005/article/details/115044899