Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import cv2
  2. import numpy
  3. import glob
  4. import os
  5.  
  6. dir = "." # current directory
  7. ext = ".jpg" # whatever extension you want
  8.  
  9. pathname = os.path.join(dir, "*" + ext)
  10. images = [cv2.imread(img) for img in glob.glob(pathname)]
  11.  
  12. height = sum(image.shape[0] for image in images)
  13. width = max(image.shape[1] for image in images)
  14. output = numpy.zeros((height,width,3))
  15.  
  16. y = 0
  17. for image in images:
  18. h,w,d = image.shape
  19. output[y:y+h,0:w] = image
  20. y += h
  21.  
  22. cv2.imwrite("test.jpg", output)
  23.  
  24. pathname = os.path.join(dir, "*" + ext)
  25. images = [cv2.imread(img) for img in glob.glob(pathname)]
  26. names = [img for img in glob.glob(pathname)]
  27. #Get filenames from paths
  28. files = [re.sub("/.*/", "", x) for x in names]
  29. #Get positions
  30. position = [re.sub(".jpeg", "", x) for x in files]
  31. #Get hight position and tranform to int
  32. height = [re.sub("_.*", "", x) for x in position]
  33. height2 = [int(i) for i in height]
  34. #Get width position and tranform to int
  35. width = [re.sub(".*_", "", x) for x in position]
  36. width2 = [int(i) for i in width]
  37. #Generate background image
  38. height_tot = 280*max(height2) #All images are 280*280
  39. width_tot = 280*max(width2)
  40. output = numpy.zeros((height_tot,width_tot,3))
  41. #Locate images
  42. for name, image in zip(names,images):
  43. files = re.sub("/.*/", "", name)
  44. position = re.sub(".jpeg", "", files)
  45. height = re.sub("_.*", "", position)
  46. width = re.sub(".*_", "", position)
  47. output[height:height+280,width:width+280] = image
  48. cv2.imwrite("test.jpg", output)
  49.  
  50. ---------------------------------------------------------------------------
  51. TypeError Traceback (most recent call last)
  52. <ipython-input-35-f789f37a8386> in <module>()
  53. 5 height = re.sub("_.*", "", position)
  54. 6 width = re.sub(".*_", "", position)
  55. ----> 7 output[height:height+280,width:width+280] = image
  56. 8
  57. 9 cv2.imwrite("test.jpg", output)
  58.  
  59. TypeError: must be str, not int
  60.  
  61. ---------------------------------------------------------------------------
  62. TypeError Traceback (most recent call last)
  63. <ipython-input-40-a7771bda9221> in <module>()
  64. 5 height = re.sub("_.*", "", position)
  65. 6 width = re.sub(".*_", "", position)
  66. ----> 7 output[str(height):str(height+280),str(width):str(width+280)] = image
  67. 8
  68. 9 cv2.imwrite("test.jpg", output)
  69.  
  70. TypeError: must be str, not int
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement