1
0
Fork 0

Converted utils os.system commands to useing the more pythonic: wand, library. And partially converted armor os.system commands to wand too.

This commit is contained in:
James Clarke 2024-01-10 11:48:41 +00:00 committed by the-real-herowl
parent efd19bd0ee
commit 961ee13ba1
3 changed files with 117 additions and 89 deletions

View File

@ -6,6 +6,11 @@ import tempfile
import sys import sys
import argparse import argparse
import glob import glob
from wand.image import Image
from wand.color import Color
from wand.display import display
from wand.drawing import Drawing
import warnings
# Conversion of map backgrounds # Conversion of map backgrounds
def convert_map_textures( def convert_map_textures(
@ -23,21 +28,18 @@ def convert_map_textures(
# Convert map background # Convert map background
map_background_file = tex_dir + "/map/map_background.png" map_background_file = tex_dir + "/map/map_background.png"
if os.path.isfile(map_background_file): if os.path.isfile(map_background_file):
os.system( destination_path = target_dir("/mods/ITEMS/mcl_maps/textures", make_texture_pack, output_dir, output_dir_name, mineclone2_path) + "/mcl_maps_map_background.png"
"convert " +
map_background_file + with Image(filename=map_background_file) as img:
" -interpolate Integer -filter point -resize \"140x140\" " + # Resize the image with 'point' filter
target_dir( img.resize(140, 140, filter='point')
"/mods/ITEMS/mcl_maps/textures",
make_texture_pack, # Save the result
output_dir, img.save(filename=destination_path)
output_dir_name,
mineclone2_path) +
"/mcl_maps_map_background.png")
# Convert armor textures # Convert armor textures
def convert_armor_textures( def convert_armor_textures(
make_texture_pack, make_texture_pack,
dry_run, dry_run,
@ -126,6 +128,7 @@ def convert_armor_textures(
helmet = adir + "/" + a[3] helmet = adir + "/" + a[3]
chestplate = adir + "/" + a[4] chestplate = adir + "/" + a[4]
boots = adir + "/" + a[6] boots = adir + "/" + a[6]
# helmet
os.system("convert -size " + os.system("convert -size " +
str(APXSIZE * str(APXSIZE *
4) + 4) +
@ -150,59 +153,53 @@ def convert_armor_textures(
str(APXSIZE) + str(APXSIZE) +
"+0+0 \\) -composite -channel A -fx \"(a > 0.0) ? 1.0 : 0.0\" " + "+0+0 \\) -composite -channel A -fx \"(a > 0.0) ? 1.0 : 0.0\" " +
helmet) helmet)
os.system("convert -size " +
str(APXSIZE *
4) +
"x" + # chestplate
str(APXSIZE * with Image(width=APXSIZE * 4, height=APXSIZE * 2, background=Color('none')) as img:
2) + # Load layer_1 and scale
" xc:none \\( " + with Image(filename=layer_1) as layer1:
layer_1 + layer1.resize(APXSIZE * 4, APXSIZE * 2)
" -scale " +
str(APXSIZE * # Define the crop geometry
4) + crop_width = int(APXSIZE * 2.5)
"x" + crop_height = APXSIZE
str(APXSIZE * crop_x = APXSIZE
2) + crop_y = APXSIZE
" -geometry +" +
str(APXSIZE) + # Crop the image
"+" + layer1.crop(crop_x, crop_y, width=crop_width, height=crop_height)
str(APXSIZE) +
" -crop " + # Composite layer1 over the transparent image
str(APXSIZE * img.composite(layer1, APXSIZE, APXSIZE)
2.5) +
"x" + # Apply channel operation
str(APXSIZE) + img.fx("a > 0.0 ? 1.0 : 0.0", channel='alpha')
"+" +
str(APXSIZE) + # Save the result
"+" + img.save(filename=chestplate)
str(APXSIZE) + with Image(width=APXSIZE * 4, height=APXSIZE * 2, background=Color('none')) as img:
" \\) -composite -channel A -fx \"(a > 0.0) ? 1.0 : 0.0\" " + with Image(filename=layer_1) as layer1:
chestplate) # Scale the image
os.system("convert -size " + layer1.resize(APXSIZE * 4, APXSIZE * 2)
str(APXSIZE *
4) + # Crop the image
"x" + crop_x = 0
str(APXSIZE * crop_y = APXSIZE
2) + crop_width = APXSIZE
" xc:none \\( " + crop_height = APXSIZE
layer_1 + layer1.crop(crop_x, crop_y, width=crop_width, height=crop_height)
" -scale " +
str(APXSIZE * # Composite the cropped image over the transparent image
4) + img.composite(layer1, 0, APXSIZE)
"x" +
str(APXSIZE * # Apply the channel operation
2) + img.fx("a > 0.0 ? 1.0 : 0.0", channel='alpha')
" -geometry +0+" +
str(APXSIZE) + # Save the result
" -crop " + img.save(filename=boots)
str(APXSIZE) +
"x" +
str(APXSIZE) +
"+0+" +
str(APXSIZE) +
" \\) -composite -channel A -fx \"(a > 0.0) ? 1.0 : 0.0\" " +
boots)
if os.path.isfile(layer_2): if os.path.isfile(layer_2):
leggings = adir + "/" + a[5] leggings = adir + "/" + a[5]
os.system("convert -size " + os.system("convert -size " +

View File

@ -8,11 +8,15 @@ import glob
import re import re
import zipfile import zipfile
from .config import SUPPORTED_MINECRAFT_VERSION, home from .config import SUPPORTED_MINECRAFT_VERSION, home
from PIL import Image
from collections import Counter from collections import Counter
import platform import platform
from wand.image import Image
from wand.color import Color
from wand.display import display
import warnings
def detect_pixel_size(directory): def detect_pixel_size(directory):
from PIL import Image
sizes = [] sizes = []
for filename in glob.glob(directory + '/**/*.png', recursive=True): for filename in glob.glob(directory + '/**/*.png', recursive=True):
with Image.open(filename) as img: with Image.open(filename) as img:
@ -24,7 +28,6 @@ def detect_pixel_size(directory):
f"Autodetected pixel size: {most_common_size[0]}x{most_common_size[1]}") f"Autodetected pixel size: {most_common_size[0]}x{most_common_size[1]}")
return most_common_size[0] return most_common_size[0]
def target_dir( def target_dir(
directory, directory,
make_texture_pack, make_texture_pack,
@ -37,26 +40,42 @@ def target_dir(
return mineclone2_path + directory return mineclone2_path + directory
def colorize( def colorize(colormap, source, colormap_pixel, texture_size, destination, tempfile1_name):
colormap, try:
source, # Convert the colormap_pixel to integer coordinates
colormap_pixel, x, y = map(int, colormap_pixel.split('+'))
texture_size,
destination, # Define texture size as integer
tempfile1_name): texture_size = int(texture_size)
os.system(
"convert " + with Image(filename=colormap) as img:
colormap + # Crop the image
" -crop 1x1+" + img.crop(x, y, width=1, height=1)
colormap_pixel +
" -depth 8 -resize " + # Set depth (This might be ignored by Wand as it manages depth automatically)
texture_size + img.depth = 8
"x" +
texture_size + # Resize the image
" " + img.resize(texture_size, texture_size)
tempfile1_name)
os.system("composite -compose Multiply " + # Save the result
tempfile1_name + " " + source + " " + destination) img.save(filename=tempfile1_name)
except Exception as e:
warnings.warn(f"An error occurred during the first image processing operation: {e}")
try:
# Load the images
with Image(filename=tempfile1_name) as top_image:
with Image(filename=source) as bottom_image:
# Perform composite operation with Multiply blend mode
bottom_image.composite(top_image, 0, 0, operator='multiply')
# Save the result
bottom_image.save(filename=destination)
except Exception as e:
warnings.warn(f"An error occurred during the second image processing operation: {e}")
def colorize_alpha( def colorize_alpha(
@ -68,8 +87,19 @@ def colorize_alpha(
tempfile2_name): tempfile2_name):
colorize(colormap, source, colormap_pixel, colorize(colormap, source, colormap_pixel,
texture_size, destination, tempfile2_name) texture_size, destination, tempfile2_name)
os.system("composite -compose Dst_In " + source + " " + try:
tempfile2_name + " -alpha Set " + destination) with Image(filename=source) as source_image:
with Image(filename=tempfile2_name) as tempfile2_image:
# Perform composite operation with Dst_In blend mode
tempfile2_image.composite(source_image, 0, 0, operator='dst_in')
# Set alpha channel
tempfile2_image.alpha_channel = 'set'
# Save the result
tempfile2_image.save(filename=destination)
except Exception as e:
warnings.warn(f"An error occurred during the second image processing operation: {e}")
def find_highest_minecraft_version(home, supported_version): def find_highest_minecraft_version(home, supported_version):

View File

@ -1 +1,2 @@
Pillow Pillow
Wand