konchin_shih

07/28 21:00

Jul 28th, 2021 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import numpy
  2. import cv2
  3.  
  4.  
  5. wordSize = 2
  6. maxShrinkRate = 10
  7. filename = "211.jpg"
  8.  
  9. picture = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
  10.  
  11.  
  12. # 資料處理
  13.  
  14. def shrinkGraph(originalGraph, xShrinkRate: int = 1, yShrinkRate: int = 1):
  15.  
  16.     xSize = len(originalGraph)
  17.     ySize = len(originalGraph[0])
  18.  
  19.     fixed_xSize = xSize // xShrinkRate
  20.     fixed_ySize = ySize // yShrinkRate
  21.  
  22.     returnGraph = numpy.zeros((fixed_xSize, fixed_ySize))
  23.  
  24.     for xi in range(fixed_xSize):
  25.         for yi in range(fixed_ySize):
  26.             for xj in range(xShrinkRate):
  27.                 for yj in range(yShrinkRate):
  28.                     returnGraph[xi][yi] += originalGraph[xi * xShrinkRate + xj][yi * yShrinkRate + yj]
  29.  
  30.     return returnGraph
  31.  
  32.  
  33. def toIntStr(num):
  34.     return str(int(num))
  35.  
  36.  
  37. def printGraph(graph, xShrinkRate, yShrinkRate, outputFilename):
  38.  
  39.     output = ""
  40.  
  41.     xSize = len(graph)
  42.     ySize = len(graph[0])
  43.  
  44.     fixed_xSize = xSize // xShrinkRate
  45.     fixed_ySize = ySize // yShrinkRate
  46.  
  47.     for xi in range(fixed_xSize):
  48.         for yi in range(fixed_ySize):
  49.             output += toIntStr(graph[xi * xShrinkRate][yi * yShrinkRate])
  50.             for yj in range(1, yShrinkRate):
  51.                 output += '-' + toIntStr(graph[xi * xShrinkRate][yi * yShrinkRate + yj])
  52.             for xj in range(1, xShrinkRate):
  53.                 for yj in range(yShrinkRate):
  54.                     output += '-' + toIntStr(graph[xi * xShrinkRate + xj][yi * yShrinkRate + yj])
  55.             output += ' '
  56.  
  57.     with open(outputFilename, "w", encoding="utf-8") as file:
  58.         file.write(output)
  59.  
  60.     pass
  61.  
  62.  
  63. trans_picture = picture.tolist()
  64. trans_picture = shrinkGraph(trans_picture, wordSize, wordSize).tolist()
  65.  
  66. for i in range(2, maxShrinkRate + 1):
  67.     printGraph(trans_picture, i, i, f"{wordSize * i}x{wordSize * i}({wordSize}x{wordSize}).txt")
  68.  
Add Comment
Please, Sign In to add comment