微信登录

工具 - 裁剪成4张图片

MJ裁剪4张图片

有幽默宽高,可以调整
还有比例,需要自己调整

  1. from PIL import Image
  2. import os
  3. import os.path
  4. # long=int(input('图片长度像素(2048,2912)\n'))
  5. # wide=int(input('图片宽度像素(2048,1632)\n'))
  6. # 2688 1792
  7. long = input("默认2688")
  8. if long :
  9. pass
  10. else:
  11. long = 2688
  12. wide = input("默认1792")
  13. if wide :
  14. pass
  15. else:
  16. wide = 1792
  17. # 1:1图比例是2048
  18. # 16:9图片比例 2912:1632
  19. # 定义文件所在文件夹
  20. image_dir = '/xxx/xxx/图片放这里双击cut'
  21. # 本文件文件名
  22. this_name = "cut.py"
  23. for parent, dir_name, file_names in os.walk(image_dir): # 遍历每一张图片
  24. file_names.remove(this_name)
  25. try:
  26. file_names.remove(".DS_Store")
  27. except:
  28. pass
  29. for filename in file_names:
  30. print(filename)
  31. pic_name = os.path.join(parent, filename)
  32. image = Image.open(pic_name)
  33. _width, _height = image.size
  34. print(_width, _height)
  35. # 四张图片的坐标
  36. t1=[0,0,float(long/2),float(wide/2)]
  37. t2=[float(long/2),0,long,float(wide/2)]
  38. t3=[0,float(wide/2),float(long/2),wide]
  39. t4=[float(long/2),float(wide/2),long,wide]
  40. pic=t1+t2+t3+t4
  41. print(pic)
  42. # [0, 0, 1456.0, 816.0, 1456.0, 0, 2912, 816.0, 0, 816.0, 1456.0, 1632, 1456.0, 816.0, 2912, 1632]
  43. for p in range(int(len(pic)/4)):
  44. pp=pic[p*4:p*4+4]
  45. print(pp)
  46. # 定义裁剪范围(left, upper, right, lower)
  47. # # box = image.crop((0,0,123,123))
  48. box = image.crop((pp[0],pp[1],pp[2],pp[3]))
  49. box = box.resize((long/2, wide/2))
  50. name = filename[:-4]+'_'+str(p) +'.png'
  51. box.save('/xxx/xxx/xxx/图片放这里双击cut/%s' % name)
  52. print('Done!')
工具 - 裁剪成4张图片