Created
September 9, 2024 21:29
-
-
Save SwiftyAlex/8259994b6f4f70dc5f6ab09f70d40af3 to your computer and use it in GitHub Desktop.
Make images from appiconsets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import shutil | |
def process_xcassets_folder(xcassets_path): | |
for item in os.listdir(xcassets_path): | |
item_path = os.path.join(xcassets_path, item) | |
if os.path.isdir(item_path): | |
process_child_folder(item_path) | |
def process_child_folder(folder_path): | |
for item in os.listdir(folder_path): | |
if item.endswith('.appiconset'): | |
appiconset_path = os.path.join(folder_path, item) | |
process_appiconset(appiconset_path) | |
def process_appiconset(appiconset_path): | |
contents_file = os.path.join(appiconset_path, 'Contents.json') | |
try: | |
with open(contents_file, 'r') as f: | |
contents = json.load(f) | |
# Find the largest image in the appiconset | |
largest_image = find_largest_image(contents['images']) | |
if not largest_image: | |
print(f"No suitable image found in {appiconset_path}") | |
return | |
# Create new imageset name | |
appiconset_name = os.path.basename(appiconset_path) | |
new_imageset_name = appiconset_name.replace('.appiconset', '.imageset') | |
new_imageset_path = os.path.join(os.path.dirname(appiconset_path), new_imageset_name) | |
# Create new imageset folder | |
os.makedirs(new_imageset_path, exist_ok=True) | |
# Copy image to new imageset | |
source_image_path = os.path.join(appiconset_path, largest_image['filename']) | |
if os.path.exists(source_image_path): | |
shutil.copy(source_image_path, os.path.join(new_imageset_path, largest_image['filename'])) | |
else: | |
print(f"Image file not found: {source_image_path}") | |
return | |
# Create Contents.json for new imageset | |
new_contents = { | |
"images" : [ | |
{ | |
"filename" : largest_image['filename'], | |
"idiom" : "universal", | |
"scale" : get_scale(largest_image) | |
} | |
], | |
"info" : { | |
"author" : "xcode", | |
"version" : 1 | |
} | |
} | |
with open(os.path.join(new_imageset_path, 'Contents.json'), 'w') as f: | |
json.dump(new_contents, f, indent=2) | |
print(f"Created new imageset: {new_imageset_path}") | |
except Exception as e: | |
print(f"Error processing {appiconset_path}: {str(e)}") | |
def find_largest_image(images): | |
largest_image = None | |
largest_size = 0 | |
for image in images: | |
if 'size' in image: | |
try: | |
size = int(image['size'].split('x')[0]) | |
if size > largest_size: | |
largest_size = size | |
largest_image = image | |
except ValueError: | |
continue | |
elif 'width' in image: | |
try: | |
size = int(image['width']) | |
if size > largest_size: | |
largest_size = size | |
largest_image = image | |
except ValueError: | |
continue | |
return largest_image | |
def get_scale(image): | |
if 'scale' in image: | |
return image['scale'] | |
elif 'filename' in image: | |
# Try to infer scale from filename (e.g., "[email protected]") | |
if '@2x' in image['filename']: | |
return "2x" | |
elif '@3x' in image['filename']: | |
return "3x" | |
return "1x" # Default to 1x if scale can't be determined | |
# Usage | |
xcassets_path = '/Users/alex/Developer/Apps/Coffee-Book/Coffee Book/Icons/AlternateIcons.xcassets' | |
process_xcassets_folder(xcassets_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment