Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
- # Written by Zitzabis
- # https://www.planetminecraft.com/member/zitzabis/
- # 6/20/2022
- #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
- from os import listdir
- from os.path import isfile, join
- from PIL import Image
- import random
- ########################
- # Global Vars
- ########################
- # Change this value to generate a new skin based on a folder's contents.
- target_folders = ["Prelim", "Week 1", "Week 2", "Week 3", "Week 4", "Week 5"]
- # 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.
- toggle_random_pixels = 1
- # 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.
- toggle_random_base_skin = 1
- # This will cause the script to average colors instead of selecting the dominant color.
- toggle_average_color = 0
- toggle_alpha_rounding = 0
- # Verbose Logging
- toggle_verbose = 0
- ########################
- # Find sum of array of values
- ########################
- def _sum(_list):
- sum=0
- for item in _list:
- sum = sum + item
- return(sum)
- ########################
- # Find common color
- ########################
- def common_color(colorList, items):
- R = []
- G = []
- B = []
- A = []
- for pixel in colorList:
- R.append(pixel[0])
- G.append(pixel[1])
- B.append(pixel[2])
- A.append(pixel[3])
- R = round(_sum(R) / items)
- G = round(_sum(G) / items)
- B = round(_sum(B) / items)
- A = round(_sum(A) / items)
- if toggle_alpha_rounding == 1:
- if A > 75:
- A = 255
- else:
- A = 0
- average = [R, G, B, A]
- return average
- ########################
- # Find unique values in list
- ########################
- def unique(list1):
- if toggle_verbose == 1:
- print("Find unique colors...")
- # initialize a null list
- unique_list = []
- # traverse for all elements
- for x in list1:
- # check if exists in unique_list or not
- if x not in unique_list:
- unique_list.append(x)
- if toggle_verbose == 1:
- print("Unique colors found.")
- return unique_list
- ########################
- # Count occurances of a value in a list
- ########################
- def countX(lst, x):
- count = 0
- for ele in lst:
- if (ele == x):
- count = count + 1
- return count
- ##############################
- # Interate through all target folders
- for target_folder in target_folders:
- # Build file list
- file_list = [f for f in listdir(target_folder) if isfile(join(target_folder, f))]
- print("Target Files:", file_list)
- imagesList = [] # Hold image data
- totalImages = 0 # Track image count
- # Iterate through file list
- if toggle_random_base_skin == 1:
- random.shuffle(file_list)
- for file in file_list:
- print("Loading...", target_folder + "/" + file)
- image = Image.open(target_folder + "/" + file, 'r').convert("RGBA") # Open image in RGBA
- width, height = image.size # Get image size
- pixel_values = list(image.getdata()) # Get list of pixels
- imagesList.append(pixel_values) # Store image set of pixels
- totalImages = totalImages + 1 # Increment counter
- # Build empty array
- print("Building color list...")
- colorList = []
- i = 0
- while i < 999999:
- colorList.append([])
- i = i + 1
- print("Color list built.")
- # Insert pixel values into array built above
- print("Collecting pixels...")
- for image in imagesList:
- i = 0
- while i < len(image):
- colorList[i].append(image[i])
- i = i + 1
- print("Pixels collected.")
- # Calculate new image colors
- newImage = []
- for pixel in colorList:
- # Check if pixel is valid
- if len(pixel) == 0:
- continue
- if toggle_average_color == 1:
- newImage.append(common_color(pixel, len(pixel)))
- else:
- # Get list of unique values
- uniq_colors = unique(pixel)
- # Init array for holding color counts
- color_occur = []
- # Count color occurances
- for color in uniq_colors:
- color_occur.append(countX(pixel, color))
- # Calculate index of the most common color
- maxValueIndex = 0
- max_value = 0
- for i in color_occur:
- if i > max_value:
- max_value = i
- for i in range(len(color_occur)):
- if color_occur[i] == max_value:
- maxValueIndex = i
- if toggle_verbose == 1:
- print(str(uniq_colors[maxValueIndex]) + " was found " + str(max_value) + " times.")
- if max_value == 1:
- if toggle_random_pixels == 1:
- newImage.append(random.choice(uniq_colors))
- else:
- newImage.append(uniq_colors[maxValueIndex])
- else:
- newImage.append(uniq_colors[maxValueIndex])
- # Save Image
- im = Image.new(mode="RGBA", size=(64, 64))
- pixels = im.load()
- p = 0
- for x in range(0, 64):
- for y in range(0, 64):
- im.putpixel((y, x), (int(newImage[p][0]), int(newImage[p][1]), int(newImage[p][2]), int(newImage[p][3])))
- p = p + 1
- im.save(target_folder + ".png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement