main.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.pos = (params.get("posX",0), params.get("posY",0))
  15. self.size = params.get("size",100)
  16. self.resolution = params.get("resolution",300)
  17. self.stepSize = params.get("stepSize",20)
  18. self.spacer = params.get("spacer",10)
  19. self.sequence = params.get("sequence",(3,2,1,0))
  20. def draw(self, im):
  21. draw = ImageDraw.Draw(im)
  22. idx = 0
  23. end = (0,0)
  24. prevEnd = (self.pos[0]-self.spacer, self.pos[1]-self.sequence[0]*self.stepSize-self.size/2)
  25. for height in self.sequence:
  26. stepBios = height*self.stepSize
  27. idxBios = idx*self.size
  28. spacerBios = idx*self.spacer
  29. pipe = Pipe(posX=self.pos[0]+idxBios+spacerBios+self.spacer, posY=self.pos[1]-stepBios, size=self.size, resolution=self.resolution)
  30. pipe.draw(im)
  31. end = (pipe.pos[0]+pipe.size, pipe.pos[1]-pipe.size/2)
  32. shape = [(end[0]*self.resolution,end[1]*self.resolution), ((end[0]+self.spacer)*self.resolution, end[1]*self.resolution)]
  33. draw.line(shape, fill =255, width = 2)
  34. shape = [((prevEnd[0]+self.spacer)*self.resolution,prevEnd[1]*self.resolution), ((end[0]-pipe.size)*self.resolution,end[1]*self.resolution)]
  35. draw.line(shape, fill =255, width = 2)
  36. prevEnd = end
  37. idx+=1
  38. shape = [(self.pos[0]*self.resolution,(self.pos[1]+10)*self.resolution), ((end[0]+self.spacer)*self.resolution, (self.pos[1]+10)*self.resolution)]
  39. draw.line(shape, fill =255, width = 2)
  40. shape = [((end[0]+self.spacer)*self.resolution,(self.pos[1]+10)*self.resolution), ((end[0]+self.spacer)*self.resolution, end[1]*self.resolution)]
  41. draw.line(shape, fill =255, width = 2)
  42. shape = [(self.pos[0]*self.resolution,(self.pos[1]+10)*self.resolution), (self.pos[0]*self.resolution, (self.pos[1]-self.sequence[0]*self.stepSize-self.size/2)*self.resolution)]
  43. draw.line(shape, fill =255, width = 2)
  44. def crop(im, imSize, x_dpi, y_dpi, resolution=10, tileSize=(280, 180)):
  45. numTilesX = math.ceil((imSize[0] / resolution) / tileSize[0])
  46. numTilesY = math.ceil((imSize[1] / resolution) / tileSize[1])
  47. for y in range(0,numTilesY):
  48. for x in range(0,numTilesX):
  49. x0=x*tileSize[0]*resolution
  50. y0=y*tileSize[1]*resolution
  51. x1=x0+tileSize[0]*resolution
  52. y1=y0+tileSize[1]*resolution
  53. subIm = im.crop((x0, y0, x1, y1))
  54. ImageOps.invert(subIm).save("out%s-%s.png"%(x,y), dpi=(x_dpi, y_dpi))
  55. def main():
  56. imSize = (7000, 1800)
  57. resolution = 10
  58. im = Image.new('L', imSize, 0)
  59. #aSide = Series(posX=50, posY=150, sequence=(4,4,2,2,0), resolution=resolution, size=110)
  60. #aSide.draw(im)
  61. bSide = Series(posX=50, posY=160, sequence=(5,3,3,1,1), resolution=resolution, size=110)
  62. bSide.draw(im)
  63. x_dpi = resolution * 25.4
  64. y_dpi = resolution * 25.4
  65. ImageOps.invert(im).save("out.png", dpi=(x_dpi, y_dpi))
  66. crop(im, imSize, x_dpi, y_dpi)
  67. main()