Advertisement
Guest User

Batch Blur Rescaler.py

a guest
Nov 3rd, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. # Description: Scales up blur (and distance) from multiple SWF XML outputted by JPEXS
  2. # Author: Hey.youcp
  3. # Date: November 3, 2016
  4. # Note: Modified from original blur rescaler (http://pastebin.com/HwY4w90K)
  5.  
  6. # Import glob (for finding files in directory) and regex (for finding certain strings in XML)
  7. import glob, re
  8.  
  9. # This should be the same as your content scale in Kurst (you can change it back to a set value if you want)
  10. scale = float(input("Enter scale value: "))
  11.  
  12. # For keeping count of the amount of XML files found
  13. filesFound = 0
  14.  
  15. # Go through each XML file in the present working directory
  16. for inputFile in glob.glob("*.xml"):
  17.  
  18.     # Set name of output file ("<filename>-blur-rescaled.xml")
  19.     outputFile = inputFile[:-4] + "-blur-rescaled.xml"
  20.  
  21.     # Open "<filename>.xml" as input file, "<filename>-blur-rescaled.xml" as output file
  22.     with open(inputFile,"r",encoding="utf-8") as XML, open(outputFile,"w",encoding="utf-8") as output:
  23.  
  24.         print("\nOpened " + inputFile)
  25.  
  26.         # For keeping count of the amount of lines modified and processed
  27.         linesModified, linesProcessed = 0, 0
  28.  
  29.         # Regex search pattern for the following formats: 5.0, 15.5, 100.15, etc.
  30.         pattern = "\d+\.\d+"
  31.  
  32.         # Go through each line in the XML file
  33.         for line in XML:
  34.             if "blurX=" in line:
  35.  
  36.                 # Get blur values from line                             Example with scale = 2:
  37.                 blurX = re.findall('blurX="' + pattern + '"', line)[0]  # blurX="5.0"
  38.                 blurY = re.findall('blurY="' + pattern + '"', line)[0]  # blurY="5.0"
  39.                 valueX = re.findall(pattern, blurX)[0]                  # 5.0
  40.                 valueY = re.findall(pattern, blurY)[0]                  # 5.0
  41.  
  42.                 # Multiply blur values
  43.                 newX = str(float(valueX) * scale)                       # 5.0 * 2 = 10.0
  44.                 newY = str(float(valueY) * scale)                       # 5.0 * 2 = 10.0
  45.  
  46.                 # Substitute old blur values with new values
  47.                 line = re.sub('blurX="' + pattern + '"', 'blurX="' + newX + '"', line)
  48.                 line = re.sub('blurY="' + pattern + '"', 'blurY="' + newY + '"', line)
  49.  
  50.                 # Increment amount of lines modified
  51.                 linesModified += 1
  52.  
  53.             # Multiply distance value (same process as commented above)
  54.             if "distance=" in line:
  55.                 distance = re.findall('distance="' + pattern + '"', line)[0]
  56.                 value = re.findall(pattern, distance)[0]
  57.                 newDistance = str(float(value) * scale)
  58.                 line = re.sub('distance="' + pattern + '"', 'distance="' + newDistance + '"', line)
  59.                 linesModified += 1
  60.  
  61.             # Increment amount of lines processed
  62.             linesProcessed += 1
  63.  
  64.             # Write line to output file
  65.             output.write(line)
  66.  
  67.         print(str(linesModified) + " lines were modified out of " + str(linesProcessed) + " lines in total.")
  68.  
  69.     # Increment amount of XML files found
  70.     filesFound += 1
  71.  
  72. # If files were found, print the amount found; else print no files found
  73. if filesFound != 0:
  74.     print("\n" + str(filesFound) + " XML files were processed.")
  75. else:
  76.     print("No XML files were found in the directory.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement