app_utils.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import os
  2. import requests
  3. import random
  4. import _thread as thread
  5. from uuid import uuid4
  6. import numpy as np
  7. import skimage
  8. from skimage.filters import gaussian
  9. from PIL import Image
  10. def compress_image(image, path_original):
  11. size = 1920, 1080
  12. width = 1920
  13. height = 1080
  14. name = os.path.basename(path_original).split('.')
  15. first_name = os.path.join(os.path.dirname(path_original), name[0] + '.jpg')
  16. if image.size[0] > width and image.size[1] > height:
  17. image.thumbnail(size, Image.ANTIALIAS)
  18. image.save(first_name, quality=85)
  19. elif image.size[0] > width:
  20. wpercent = (width/float(image.size[0]))
  21. height = int((float(image.size[1])*float(wpercent)))
  22. image = image.resize((width,height), PIL.Image.ANTIALIAS)
  23. image.save(first_name,quality=85)
  24. elif image.size[1] > height:
  25. wpercent = (height/float(image.size[1]))
  26. width = int((float(image.size[0])*float(wpercent)))
  27. image = image.resize((width,height), Image.ANTIALIAS)
  28. image.save(first_name, quality=85)
  29. else:
  30. image.save(first_name, quality=85)
  31. def convertToJPG(path_original):
  32. img = Image.open(path_original)
  33. name = os.path.basename(path_original).split('.')
  34. first_name = os.path.join(os.path.dirname(path_original), name[0] + '.jpg')
  35. if img.format == "JPEG":
  36. image = img.convert('RGB')
  37. compress_image(image, path_original)
  38. img.close()
  39. elif img.format == "GIF":
  40. i = img.convert("RGBA")
  41. bg = Image.new("RGBA", i.size)
  42. image = Image.composite(i, bg, i)
  43. compress_image(image, path_original)
  44. img.close()
  45. elif img.format == "PNG":
  46. try:
  47. image = Image.new("RGB", img.size, (255,255,255))
  48. image.paste(img,img)
  49. compress_image(image, path_original)
  50. except ValueError:
  51. image = img.convert('RGB')
  52. compress_image(image, path_original)
  53. img.close()
  54. elif img.format == "BMP":
  55. image = img.convert('RGB')
  56. compress_image(image, path_original)
  57. img.close()
  58. def blur(image, x0, x1, y0, y1, sigma=1, multichannel=True):
  59. y0, y1 = min(y0, y1), max(y0, y1)
  60. x0, x1 = min(x0, x1), max(x0, x1)
  61. im = image.copy()
  62. sub_im = im[y0:y1,x0:x1].copy()
  63. blur_sub_im = gaussian(sub_im, sigma=sigma, multichannel=multichannel)
  64. blur_sub_im = np.round(255 * blur_sub_im)
  65. im[y0:y1,x0:x1] = blur_sub_im
  66. return im
  67. def download(url, filename):
  68. data = requests.get(url).content
  69. with open(filename, 'wb') as handler:
  70. handler.write(data)
  71. return filename
  72. def generate_random_filename(upload_directory, extension):
  73. filename = str(uuid4())
  74. filename = os.path.join(upload_directory, filename + "." + extension)
  75. return filename
  76. def clean_me(filename):
  77. if os.path.exists(filename):
  78. os.remove(filename)
  79. def clean_all(files):
  80. for me in files:
  81. clean_me(me)
  82. def create_directory(path):
  83. os.system("mkdir -p %s" % os.path.dirname(path))
  84. def get_model_bin(url, output_path):
  85. if not os.path.exists(output_path):
  86. create_directory(output_path)
  87. cmd = "wget -O %s %s" % (output_path, url)
  88. print(cmd)
  89. os.system(cmd)
  90. return output_path
  91. #model_list = [(url, output_path), (url, output_path)]
  92. def get_multi_model_bin(model_list):
  93. for m in model_list:
  94. thread.start_new_thread(get_model_bin, m)