utils.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from .consts import BitmapType
  2. import math
  3. class NumberUtils(object):
  4. @classmethod
  5. def round_pixels_20(cls, pixels):
  6. return round(pixels * 100) / 100
  7. @classmethod
  8. def round_pixels_400(cls, pixels):
  9. return round(pixels * 10000) / 10000
  10. class ColorUtils(object):
  11. @classmethod
  12. def alpha(cls, color):
  13. return int(color >> 24) / 255.0
  14. @classmethod
  15. def rgb(cls, color):
  16. return (color & 0xffffff)
  17. @classmethod
  18. def to_rgb_string(cls, color):
  19. c = "%x" % color
  20. while len(c) < 6: c = "0" + c
  21. return "#"+c
  22. class ImageUtils(object):
  23. @classmethod
  24. def get_image_size(cls, data):
  25. pass
  26. @classmethod
  27. def get_image_type(cls, data):
  28. pos = data.tell()
  29. image_type = 0
  30. data.seek(0, 2) # moves file pointer to final position
  31. if data.tell() > 8:
  32. data.seek(0)
  33. b0 = ord(data.read(1))
  34. b1 = ord(data.read(1))
  35. b2 = ord(data.read(1))
  36. b3 = ord(data.read(1))
  37. b4 = ord(data.read(1))
  38. b5 = ord(data.read(1))
  39. b6 = ord(data.read(1))
  40. b7 = ord(data.read(1))
  41. if b0 == 0xff and (b1 == 0xd8 or 1 == 0xd9):
  42. image_type = BitmapType.JPEG
  43. elif b0 == 0x89 and b1 == 0x50 and b2 == 0x4e and b3 == 0x47 and \
  44. b4 == 0x0d and b5 == 0x0a and b6 == 0x1a and b7 == 0x0a:
  45. image_type = BitmapType.PNG
  46. elif b0 == 0x47 and b1 == 0x49 and b2 == 0x46 and b3 == 0x38 and b4 == 0x39 and b5 == 0x61:
  47. image_type = BitmapType.GIF89A
  48. data.seek(pos)
  49. return image_type