Not tested, but basically, your program is this:
I didn't look into height/crop_areas mapping - there might be better approach, but even this is better than yours.
import os
from PIL import Image
import pandas as pd
CROPS = {4083:((656, 285, 1120, 730), (656, 845, 1545, 1272), (656, 1292, 1545, 1720), (656, 3120, 1545, 3555), (656, 3580, 1545, 3925)),
8166:((656, 285, 1120, 730), (656, 845, 1545, 1272), (656, 1292, 1545, 1720), (656, 2260, 1545, 2635), (656, 3120, 1545, 3460)),
3995:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3115, 1545, 3560), (656, 3580, 1545, 3925)),
3996:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3115, 1545, 3560), (656, 3580, 1545, 3925)),
3536:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2655, 1545, 3100), (656, 3120, 1545, 3465)),
3537:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2655, 1545, 3100), (656, 3120, 1545, 3465)),
3872:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2995, 1545, 3430), (656, 3460, 1545, 3800)),
3873:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2995, 1545, 3430), (656, 3460, 1545, 3800)),
4552:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3675, 1545, 4115), (656, 4140, 1545, 4480)),
4312:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3435, 1545, 3875), (656, 3900, 1545, 4240)),
4189:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3310, 1545, 3750), (656, 3775, 1545, 4120)),
4432:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3552, 1545, 3990), (656, 4015, 1545, 4360)),
4352:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3475, 1545, 3915), (656, 3935, 1545, 4280)),
4472:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3595, 1545, 4035), (656, 4055, 1545, 4400)),
4512:(656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3635, 1545, 4075), (656, 4095, 1545, 4440)),
4392:(656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 3515, 1545, 3955), (656, 3975, 1545, 4320)),
3475:(656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2600, 1545, 3035), (656, 3060, 1545, 3405)),
3352:((656, 285, 1120, 730), (656, 845, 1545, 1270), (656, 1292, 1540, 1715), (656, 2475, 1545, 2915), (656, 2935, 1545, 3280),
3304:((705, 240, 1080, 605), (705, 705, 1445, 1060), (705, 1080, 1445, 1425), (705, 2590, 1445, 2955). (705, 2975, 1445, 3260)),
3310:((705, 240, 1080, 605), (705, 705, 1445, 1060), (705, 1080, 1445, 1425), (705, 2590, 1445, 2955). (705, 2975, 1445, 3260))}
def crop(image, crop_areas):
basename_without_ext = os.path.splitext(os.path.basename(image.filename))[0]
print(basename_without_ext, image.height)
for idx, crop_area in enumerate(crop_areas, start=1):
im_crop = image.crop(crop_area)
im_crop.save(f'{basename_without_ext}crop{idx}.png', quality=100)
def export2csv(group):
df = pd.DataFrame(group)
df.to_csv ('NokoriList.csv', encoding='utf-8-sig')
if __name__ == '__main__':
pathtodirectory = 'E:/testimages/images'
nokori = []
for item in os.scandir(path):
if item.is_file:
im = Image.open(item.path)
width, height = im.size
crop_areas = CROPS.get(im.height)
if crop_areas:
crop(im, crop_areas)
else:
basename_without_ext = os.path.splitext(os.path.basename(im.filename))[0]
nokori.append({'name':basename_without_ext, 'height':height})
export2csv(nokori)