import os from PIL import Image def batch_crop_images(input_dir, output_dir, x, y, width, height): if not os.path.exists(output_dir): os.makedirs(output_dir) for filename in os.listdir(input_dir): if filename.endswith('.jpg') or filename.endswith('.png'): img = Image.open(os.path.join(input_dir, filename)) crop_img = img.crop((x, y, x + width, y + height)) crop_img.save(os.path.join(output_dir, filename)) if __name__ == '__main__': batch_crop_images(input_dir="images", output_dir="images2",x=0,y=0,width=1920,height=1000)
python图片裁剪
import os from PIL import Image def batch_crop_images ( input_dir , output_dir , x , y , width , height ): if not os.path.exists( output_dir ): os.makedirs( out…