Python filtra imágenes adecuadas para papel tapiz por tamaño

La idea es muy simple, el proceso es muy irregular
1. Encuentre las bibliotecas y métodos necesarios: configuración de directorio, lectura de archivo, adquisición de tamaño, movimiento de archivo
2. depuración: eliminación de archivo, error de lectura de archivo, 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并不会中止程序,只是提醒而已

Inserte la descripción de la imagen aquí
Hablando casualmente, para el webp disfrazado de png, Windows no muestra su información de resolución, ni puede formar su vista previa, ni puede usarse como fondo de pantalla, por lo que Windows actualmente no es compatible con webp y debe convertirse.

Inserte la descripción de la imagen aquí
Después de eso, solo necesita seleccionar las adecuadas de estas imágenes para obtener y eliminar imágenes inapropiadas, y convertirse en paquetes de papel tapiz. Por ejemplo, el sistema procesa automáticamente algunas imágenes que no son 16: 9 y el centro de gravedad es inapropiado, como como algunas fotos de Cat Boss y Gus.

Hay un error de muy bajo nivel. Si lo ve y cree erróneamente que es cierto y no lo ve, le pediré disculpas con picardía:

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']

Supongo que te gusta

Origin blog.csdn.net/jhsxy2005/article/details/115044899
Recomendado
Clasificación