Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- import cv2
- wordSize = 2
- maxShrinkRate = 10
- filename = "211.jpg"
- picture = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
- # 資料處理
- def shrinkGraph(originalGraph, xShrinkRate: int = 1, yShrinkRate: int = 1):
- xSize = len(originalGraph)
- ySize = len(originalGraph[0])
- fixed_xSize = xSize // xShrinkRate
- fixed_ySize = ySize // yShrinkRate
- returnGraph = numpy.zeros((fixed_xSize, fixed_ySize))
- for xi in range(fixed_xSize):
- for yi in range(fixed_ySize):
- for xj in range(xShrinkRate):
- for yj in range(yShrinkRate):
- returnGraph[xi][yi] += originalGraph[xi * xShrinkRate + xj][yi * yShrinkRate + yj]
- return returnGraph
- def toIntStr(num):
- return str(int(num))
- def printGraph(graph, xShrinkRate, yShrinkRate, outputFilename):
- output = ""
- xSize = len(graph)
- ySize = len(graph[0])
- fixed_xSize = xSize // xShrinkRate
- fixed_ySize = ySize // yShrinkRate
- for xi in range(fixed_xSize):
- for yi in range(fixed_ySize):
- output += toIntStr(graph[xi * xShrinkRate][yi * yShrinkRate])
- for yj in range(1, yShrinkRate):
- output += '-' + toIntStr(graph[xi * xShrinkRate][yi * yShrinkRate + yj])
- for xj in range(1, xShrinkRate):
- for yj in range(yShrinkRate):
- output += '-' + toIntStr(graph[xi * xShrinkRate + xj][yi * yShrinkRate + yj])
- output += ' '
- with open(outputFilename, "w", encoding="utf-8") as file:
- file.write(output)
- pass
- trans_picture = picture.tolist()
- trans_picture = shrinkGraph(trans_picture, wordSize, wordSize).tolist()
- for i in range(2, maxShrinkRate + 1):
- printGraph(trans_picture, i, i, f"{wordSize * i}x{wordSize * i}({wordSize}x{wordSize}).txt")
Add Comment
Please, Sign In to add comment