depix.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from depixlib.LoadedImage import *
  2. from depixlib.Rectangle import *
  3. from depixlib.functions import *
  4. import argparse
  5. import logging
  6. logging.basicConfig(level=logging.INFO)
  7. usage = '''
  8. The pixelated rectangle must be cut out to only include the pixelated rectangles.
  9. The pattern search image is generally a screenshot of a De Bruijn sequence of expected characters,
  10. made on a machine with the same editor and text size as the original screenshot that was pixelated.
  11. '''
  12. parser = argparse.ArgumentParser(description = usage)
  13. parser.add_argument('-p', '--pixelimage', help = 'Path to image with pixelated rectangle', required=True)
  14. parser.add_argument('-s', '--searchimage', help = 'Path to image with patterns to search', required=True)
  15. parser.add_argument('-o', '--outputimage', help = 'Path to output image', nargs='?', default='output.png')
  16. args = parser.parse_args()
  17. pixelatedImagePath = args.pixelimage
  18. searchImagePath = args.searchimage
  19. logging.info("Loading pixelated image from %s" % pixelatedImagePath)
  20. pixelatedImage = LoadedImage(pixelatedImagePath)
  21. unpixelatedOutputImage = pixelatedImage.getCopyOfLoadedPILImage()
  22. logging.info("Loading search image from %s" % searchImagePath)
  23. searchImage = LoadedImage(searchImagePath)
  24. logging.info("Finding color rectangles from pixelated space")
  25. # fill coordinates here if not cut out
  26. pixelatedRectange = Rectangle((0, 0), (pixelatedImage.width-1, pixelatedImage.height-1))
  27. pixelatedSubRectanges = findSameColorSubRectangles(pixelatedImage, pixelatedRectange)
  28. logging.info("Found %s same color rectangles" % len(pixelatedSubRectanges))
  29. pixelatedSubRectanges = removeMootColorRectangles(pixelatedSubRectanges)
  30. logging.info("%s rectangles left after moot filter" % len(pixelatedSubRectanges))
  31. rectangeSizeOccurences = findRectangleSizeOccurences(pixelatedSubRectanges)
  32. logging.info("Found %s different rectangle sizes" % len(rectangeSizeOccurences))
  33. if len(rectangeSizeOccurences) > max(10, pixelatedRectange.width * pixelatedRectange.height * 0.01):
  34. logging.warning("Too many variants on block size. Re-pixelating the image might help.")
  35. logging.info("Finding matches in search image")
  36. rectangleMatches = findRectangleMatches(rectangeSizeOccurences, pixelatedSubRectanges, searchImage)
  37. logging.info("Removing blocks with no matches")
  38. pixelatedSubRectanges = dropEmptyRectangleMatches(rectangleMatches, pixelatedSubRectanges)
  39. logging.info("Splitting single matches and multiple matches")
  40. singleResults, pixelatedSubRectanges = splitSingleMatchAndMultipleMatches(pixelatedSubRectanges, rectangleMatches)
  41. logging.info("[%s straight matches | %s multiple matches]" % (len(singleResults), len(pixelatedSubRectanges)))
  42. logging.info("Trying geometrical matches on single-match squares")
  43. singleResults, pixelatedSubRectanges = findGeometricMatchesForSingleResults(singleResults, pixelatedSubRectanges, rectangleMatches)
  44. logging.info("[%s straight matches | %s multiple matches]" % (len(singleResults), len(pixelatedSubRectanges)))
  45. logging.info("Trying another pass on geometrical matches")
  46. singleResults, pixelatedSubRectanges = findGeometricMatchesForSingleResults(singleResults, pixelatedSubRectanges, rectangleMatches)
  47. logging.info("[%s straight matches | %s multiple matches]" % (len(singleResults), len(pixelatedSubRectanges)))
  48. logging.info("Writing single match results to output")
  49. writeFirstMatchToImage(singleResults, rectangleMatches, searchImage, unpixelatedOutputImage)
  50. logging.info("Writing average results for multiple matches to output")
  51. writeAverageMatchToImage(pixelatedSubRectanges, rectangleMatches, searchImage, unpixelatedOutputImage)
  52. # writeRandomMatchesToImage(pixelatedSubRectanges, rectangleMatches, searchImage, unpixelatedOutputImage)
  53. logging.info("Saving output image to: %s" % args.outputimage)
  54. unpixelatedOutputImage.save(args.outputimage)