main.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from PIL import Image, ImageDraw, ImageOps
  2. import math
  3. class Pipe:
  4. def __init__(self, **params):
  5. self.pos = (params.get("posX",0), params.get("posY",0))
  6. self.size = params.get("size",100)
  7. self.resolution = params.get("resolution",300)
  8. def draw(self, im):
  9. draw = ImageDraw.Draw(im)
  10. offset = (0,-self.size)
  11. draw.arc(((self.pos[0]+offset[0])*self.resolution, (self.pos[1]+offset[1])*self.resolution, (self.pos[0]+self.size+offset[0])*self.resolution, (self.pos[1]+self.size+offset[1])*self.resolution), start=0, end=180, fill=255, width=2)
  12. class Series:
  13. def __init__(self, **params):
  14. self.name = params.get("name", "out")
  15. self.pos = (params.get("posX",0), params.get("posY",0))
  16. self.size = params.get("size",100)
  17. self.resolution = params.get("resolution",300)
  18. self.x_dpi = self.resolution * 25.4
  19. self.y_dpi = self.resolution * 25.4
  20. self.stepSize = params.get("stepSize",30)
  21. self.tilt = params.get("tilt",20)
  22. self.spacer = params.get("spacer",20)
  23. self.sequence = params.get("sequence",(3,2,1,0))
  24. def draw(self, im):
  25. draw = ImageDraw.Draw(im)
  26. idx = 0
  27. end = (0,0)
  28. prevEnd = (self.pos[0]-self.spacer, self.pos[1]-self.sequence[0]*self.tilt-self.size/2)
  29. for height in self.sequence:
  30. tiltBios = height*self.tilt
  31. stepBios = idx*self.stepSize
  32. idxBios = idx*self.size
  33. spacerBios = idx*self.spacer
  34. pipe = Pipe(posX=self.pos[0]+idxBios+spacerBios+self.spacer, posY=self.pos[1]-tiltBios-stepBios, size=self.size, resolution=self.resolution)
  35. pipe.draw(im)
  36. end = (pipe.pos[0]+pipe.size, pipe.pos[1]-pipe.size/2)
  37. if prevEnd[1] < end[1]:
  38. shape = [(end[0]*self.resolution,end[1]*self.resolution), ((end[0]+self.spacer)*self.resolution, end[1]*self.resolution)]
  39. draw.line(shape, fill =255, width = 2)
  40. shape = [((prevEnd[0]+self.spacer)*self.resolution,prevEnd[1]*self.resolution), ((end[0]-pipe.size)*self.resolution,end[1]*self.resolution)]
  41. draw.line(shape, fill =255, width = 2)
  42. else:
  43. shape = [((prevEnd[0])*self.resolution,prevEnd[1]*self.resolution), ((prevEnd[0])*self.resolution,end[1]*self.resolution)]
  44. draw.line(shape, fill =255, width = 2)
  45. shape = [((prevEnd[0])*self.resolution,end[1]*self.resolution), ((end[0]-pipe.size)*self.resolution, end[1]*self.resolution)]
  46. draw.line(shape, fill =255, width = 2)
  47. prevEnd = end
  48. idx+=1
  49. shape = [(self.pos[0]*self.resolution,(self.pos[1]+10)*self.resolution), ((end[0]+self.spacer)*self.resolution, (self.pos[1]+10)*self.resolution)]
  50. draw.line(shape, fill =255, width = 2)
  51. shape = [((end[0])*self.resolution, end[1]*self.resolution), ((end[0]+self.spacer)*self.resolution, end[1]*self.resolution)]
  52. draw.line(shape, fill =255, width = 2)
  53. shape = [((end[0]+self.spacer)*self.resolution,(self.pos[1]+10)*self.resolution), ((end[0]+self.spacer)*self.resolution, end[1]*self.resolution)]
  54. draw.line(shape, fill =255, width = 2)
  55. shape = [(self.pos[0]*self.resolution,(self.pos[1]+10)*self.resolution), (self.pos[0]*self.resolution, (self.pos[1]-self.sequence[0]*self.tilt-self.size/2)*self.resolution)]
  56. draw.line(shape, fill =255, width = 2)
  57. def cropNsave(self, im, imSize, tileSize=(180, 280)):
  58. numTilesX = math.ceil((imSize[0] / self.resolution) / tileSize[0])
  59. numTilesY = math.ceil((imSize[1] / self.resolution) / tileSize[1])
  60. for y in range(0,numTilesY):
  61. for x in range(0,numTilesX):
  62. x0=x*tileSize[0]*self.resolution
  63. y0=y*tileSize[1]*self.resolution
  64. x1=x0+tileSize[0]*self.resolution
  65. y1=y0+tileSize[1]*self.resolution
  66. subIm = im.crop((x0, y0, x1, y1))
  67. self.drawMarker(subIm, tileSize)
  68. ImageOps.invert(subIm).save("%s-%s-%s.png"%(self.name,x,y), dpi=(self.x_dpi, self.y_dpi))
  69. def drawMarker(self, subIm, tileSize):
  70. x0 = 0
  71. y0 = 0
  72. x1 = tileSize[0]*self.resolution
  73. y1 = tileSize[1]*self.resolution
  74. length = 40
  75. draw = ImageDraw.Draw(subIm)
  76. shapes = [
  77. [(x0,y0), (x0+length,y0)],
  78. [(x0,y0), (x0,y0+length)],
  79. [(x0,y1), (x0+length,y1)],
  80. [(x0,y1), (x0,y1-length)],
  81. [(x1,y0), (x1-length,y0)],
  82. [(x1,y0), (x1,y0+length)],
  83. [(x1,y1), (x1-length,y1)],
  84. [(x1,y1), (x1,y1-length)]
  85. ]
  86. for shape in shapes:
  87. draw.line(shape, fill =200, width = 20)
  88. def main():
  89. imSize = (7000, 5599)
  90. resolution = 10
  91. im = Image.new('L', imSize, 0)
  92. aSide = Series(name="aSide", posX=10, posY=300, sequence=(0,2,2,4,4), resolution=resolution, size=110)
  93. aSide.draw(im)
  94. ImageOps.invert(im).save("aSide.png", dpi=(aSide.x_dpi, aSide.y_dpi))
  95. aSide.cropNsave(im, imSize)
  96. im = Image.new('L', imSize, 0)
  97. bSide = Series(name="bSide",posX=10, posY=300, sequence=(1,1,3,3,5), resolution=resolution, size=110)
  98. bSide.draw(im)
  99. ImageOps.invert(im).save("bSide.png", dpi=(bSide.x_dpi, bSide.y_dpi))
  100. bSide.cropNsave(im, imSize)
  101. main()