dealpng.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import sys
  3. import time
  4. import _thread
  5. import PIL.Image as Image
  6. threadNum_dealPng = 0
  7. # 将PNG图片背景色设置为透明
  8. def dealPng(pngFile):
  9. global threadNum_dealPng
  10. threadNum_dealPng += 1
  11. img = Image.open(pngFile).convert('RGBA') # 从RGB(24位)模式转成RGBA(32位)模式
  12. W, L = img.size
  13. # targetColor = (128,128,128,255) # 待替换掉的背景颜色
  14. targetColor = (95,235,140,255) # 待替换掉的背景颜色
  15. for h in range(W):
  16. for i in range(L):
  17. # print(img.getpixel((h, i)))
  18. if img.getpixel((h, i)) == targetColor:
  19. # img.putpixel((h, i), (0, 0, 0, 0)) # 设置透明
  20. img.putpixel((h, i), (0, 0, 0, 153)) # 设置半透明
  21. img.save(pngFile) # 自己设置保存地址
  22. # img.save(pngFile + '.png') # 自己设置保存地址
  23. threadNum_dealPng -= 1
  24. def dealAllPng(rootdir):
  25. for root,dirs,files in os.walk(rootdir):
  26. # for dir in dirs:
  27. # print(os.path.join(root,dir))
  28. for file in files:
  29. print("dealAllPng: " + os.path.join(root,file))
  30. dealPng(os.path.join(root,file))
  31. # _thread.start_new_thread(dealPng, (os.path.join(root,file),))
  32. # time.sleep(0.3)
  33. if __name__ == '__main__':
  34. paramNum = len(sys.argv)
  35. if paramNum != 2:
  36. print("param err! please input fileid.")
  37. exit()
  38. # sys.argv[0] 表示脚本名
  39. fileid = int(sys.argv[1]) #csv文件编号 详见 fileinfo.js
  40. filepath = r"pic\\%d" % fileid
  41. # dealPng(filepath + "\\00003_141535_3.png")
  42. print("dealAllPng start !")
  43. dealAllPng(filepath)
  44. time.sleep(0.1)
  45. while 1:
  46. if threadNum_dealPng == 0:
  47. print("dealAllPng finished !")
  48. break
  49. else:
  50. print("dealAllPng threadNum_dealPng: %d" % threadNum_dealPng)
  51. time.sleep(1)