-
Notifications
You must be signed in to change notification settings - Fork 6
/
图片按坐标裁剪(拖拽,多个文件或文件夹).py
35 lines (30 loc) · 1.16 KB
/
图片按坐标裁剪(拖拽,多个文件或文件夹).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# encoding:utf-8
# https://github.com/wangandi520/andyspythonscript
from PIL import Image
from pathlib import Path
import sys
def main(inputPath):
# 设置文件类型
#fileType = ['.png']
fileType = ['.png','.jpg']
# 新图片的尺寸(左下角横坐标, 左下角纵坐标, 右上角横坐标, 右上角纵坐标)
newImgSize = (0, 0, 200 / 2, 100)
del inputPath[0]
for aPath in inputPath:
if Path.is_dir(Path(aPath)):
for file in Path(aPath).glob('**/*'):
if file.suffix.lower() in fileType:
img = Image.open(file)
newImg = img.crop(newImgSize)
newImg.save(file.parent.joinpath(file.stem + '_new' + file.suffix))
if Path.is_file(Path(aPath)):
if Path(aPath).suffix.lower() in fileType:
img = Image.open(Path(aPath))
newImg = img.crop(newImgSize)
newImg.save(Path(aPath).parent.joinpath(Path(aPath).stem + '_new' + Path(aPath).suffix))
if __name__ == '__main__':
try:
if len(sys.argv) >= 2:
main(sys.argv)
except IndexError:
pass