Advertisement
Guest User

Untitled

a guest
Jun 21st, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.61 KB | None | 0 0
  1. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
  2. # Written by Zitzabis
  3. # https://www.planetminecraft.com/member/zitzabis/
  4. # 6/20/2022
  5. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
  6.  
  7. from os import listdir
  8. from os.path import isfile, join
  9. from PIL import Image
  10. import random
  11.  
  12.  
  13. ########################
  14. # Global Vars
  15. ########################
  16. # Change this value to generate a new skin based on a folder's contents.
  17. target_folders = ["Prelim", "Week 1", "Week 2", "Week 3", "Week 4", "Week 5"]
  18. # This will cause the script to select a random color if no more than 1 is found at a pixel position. This setting overrides toggle_random_base_skin.
  19. toggle_random_pixels = 1                                                        
  20. # This will cause the script to randomly choose a base skin instead of defaulting to the first one loaded as it's reference for when no more than 1 unique color is found at a pixel position.
  21. toggle_random_base_skin = 1
  22. # This will cause the script to average colors instead of selecting the dominant color.
  23. toggle_average_color = 0
  24. toggle_alpha_rounding = 0
  25. # Verbose Logging
  26. toggle_verbose = 0
  27.  
  28. ########################
  29. # Find sum of array of values
  30. ########################
  31. def _sum(_list):
  32.     sum=0
  33.     for item in _list:
  34.         sum = sum + item
  35.          
  36.     return(sum)
  37.  
  38. ########################
  39. # Find common color
  40. ########################
  41. def common_color(colorList, items):
  42.     R = []
  43.     G = []
  44.     B = []
  45.     A = []
  46.     for pixel in colorList:
  47.         R.append(pixel[0])
  48.         G.append(pixel[1])
  49.         B.append(pixel[2])
  50.         A.append(pixel[3])
  51.  
  52.     R = round(_sum(R) / items)
  53.     G = round(_sum(G) / items)
  54.     B = round(_sum(B) / items)
  55.     A = round(_sum(A) / items)
  56.     if toggle_alpha_rounding == 1:
  57.         if A > 75:
  58.             A = 255
  59.         else:
  60.             A = 0
  61.  
  62.     average = [R, G, B, A]
  63.  
  64.     return average
  65.  
  66. ########################
  67. # Find unique values in list
  68. ########################
  69. def unique(list1):
  70.     if toggle_verbose == 1:
  71.         print("Find unique colors...")
  72.     # initialize a null list
  73.     unique_list = []
  74.      
  75.     # traverse for all elements
  76.     for x in list1:
  77.         # check if exists in unique_list or not
  78.         if x not in unique_list:
  79.             unique_list.append(x)
  80.     if toggle_verbose == 1:
  81.         print("Unique colors found.")
  82.     return unique_list
  83.  
  84. ########################
  85. # Count occurances of a value in a list
  86. ########################
  87. def countX(lst, x):
  88.     count = 0
  89.     for ele in lst:
  90.         if (ele == x):
  91.             count = count + 1
  92.     return count
  93.  
  94. ##############################
  95. # Interate through all target folders
  96. for target_folder in target_folders:
  97.    
  98.     # Build file list
  99.     file_list = [f for f in listdir(target_folder) if isfile(join(target_folder, f))]
  100.     print("Target Files:", file_list)
  101.  
  102.     imagesList = [] # Hold image data
  103.     totalImages = 0 # Track image count
  104.     # Iterate through file list
  105.     if toggle_random_base_skin == 1:
  106.         random.shuffle(file_list)
  107.    
  108.     for file in file_list:
  109.         print("Loading...", target_folder + "/" + file)
  110.         image = Image.open(target_folder + "/" + file, 'r').convert("RGBA") # Open image in RGBA
  111.         width, height = image.size # Get image size
  112.         pixel_values = list(image.getdata()) # Get list of pixels
  113.         imagesList.append(pixel_values) # Store image set of pixels
  114.         totalImages = totalImages + 1 # Increment counter
  115.  
  116.     # Build empty array
  117.     print("Building color list...")
  118.     colorList = []
  119.     i = 0
  120.     while i < 999999:
  121.         colorList.append([])
  122.         i = i + 1
  123.     print("Color list built.")
  124.  
  125.     # Insert pixel values into array built above
  126.     print("Collecting pixels...")
  127.     for image in imagesList:
  128.         i = 0
  129.         while i < len(image):
  130.             colorList[i].append(image[i])
  131.             i = i + 1
  132.     print("Pixels collected.")
  133.  
  134.     # Calculate new image colors
  135.     newImage = []
  136.     for pixel in colorList:
  137.         # Check if pixel is valid
  138.         if len(pixel) == 0:
  139.             continue
  140.         if toggle_average_color == 1:
  141.             newImage.append(common_color(pixel, len(pixel)))
  142.         else:
  143.             # Get list of unique values
  144.             uniq_colors = unique(pixel)
  145.             # Init array for holding color counts
  146.             color_occur = []
  147.             # Count color occurances
  148.             for color in uniq_colors:
  149.                 color_occur.append(countX(pixel, color))
  150.  
  151.             # Calculate index of the most common color
  152.             maxValueIndex = 0
  153.             max_value = 0
  154.             for i in color_occur:
  155.                 if i > max_value:
  156.                     max_value = i
  157.             for i in range(len(color_occur)):
  158.                 if color_occur[i] == max_value:
  159.                     maxValueIndex = i
  160.  
  161.             if toggle_verbose == 1:
  162.                 print(str(uniq_colors[maxValueIndex]) + " was found " + str(max_value) + " times.")
  163.             if max_value == 1:
  164.                 if toggle_random_pixels == 1:
  165.                     newImage.append(random.choice(uniq_colors))
  166.                 else:
  167.                     newImage.append(uniq_colors[maxValueIndex])
  168.             else:
  169.                 newImage.append(uniq_colors[maxValueIndex])
  170.  
  171.     # Save Image
  172.     im = Image.new(mode="RGBA", size=(64, 64))
  173.     pixels = im.load()
  174.     p = 0
  175.     for x in range(0, 64):
  176.         for y in range(0, 64):
  177.             im.putpixel((y, x), (int(newImage[p][0]), int(newImage[p][1]), int(newImage[p][2]), int(newImage[p][3])))
  178.             p = p + 1
  179.     im.save(target_folder + ".png")
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement