Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

common.py 1.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  1. import numpy as np
  2. def random_crop(img, w, h):
  3. height, width = img.shape[:2]
  4. h_rnd = height - h
  5. w_rnd = width - w
  6. y = np.random.randint(0, h_rnd) if h_rnd > 0 else 0
  7. x = np.random.randint(0, w_rnd) if w_rnd > 0 else 0
  8. return img[y:y+height, x:x+width]
  9. def normalize_channels(img, target_channels):
  10. img_shape_len = len(img.shape)
  11. if img_shape_len == 2:
  12. h, w = img.shape
  13. c = 0
  14. elif img_shape_len == 3:
  15. h, w, c = img.shape
  16. else:
  17. raise ValueError("normalize: incorrect image dimensions.")
  18. if c == 0 and target_channels > 0:
  19. img = img[...,np.newaxis]
  20. c = 1
  21. if c == 1 and target_channels > 1:
  22. img = np.repeat (img, target_channels, -1)
  23. c = target_channels
  24. if c > target_channels:
  25. img = img[...,0:target_channels]
  26. c = target_channels
  27. return img
  28. def cut_odd_image(img):
  29. h, w, c = img.shape
  30. wm, hm = w % 2, h % 2
  31. if wm + hm != 0:
  32. img = img[0:h-hm,0:w-wm,:]
  33. return img
  34. def overlay_alpha_image(img_target, img_source, xy_offset=(0,0) ):
  35. (h,w,c) = img_source.shape
  36. if c != 4:
  37. raise ValueError("overlay_alpha_image, img_source must have 4 channels")
  38. x1, x2 = xy_offset[0], xy_offset[0] + w
  39. y1, y2 = xy_offset[1], xy_offset[1] + h
  40. alpha_s = img_source[:, :, 3] / 255.0
  41. alpha_l = 1.0 - alpha_s
  42. for c in range(0, 3):
  43. img_target[y1:y2, x1:x2, c] = (alpha_s * img_source[:, :, c] +
  44. alpha_l * img_target[y1:y2, x1:x2, c])
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...