Created
August 17, 2024 14:03
-
-
Save prabhatsdp/10e8ce1c06ad86e6ae868d08a2a2116a to your computer and use it in GitHub Desktop.
This Script can export Android Drawables with folders to import cumulatively at once using Android Studio's Import Drawables option. Script supports creation of drawables from PNG and SVG files currently.
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 argparse | |
| import os | |
| from PIL import Image | |
| import cairosvg | |
| # Supported extensions | |
| SUPPORTED_EXTENSIONS = ['.png', '.svg'] | |
| # Dictionary to store the multipliers for density suffixes | |
| density_multipliers = { | |
| '-ldpi': 0.75, | |
| '-mdpi': 1.0, | |
| '-hdpi': 1.5, | |
| '-xhdpi': 2.0, | |
| '-xxhdpi': 3.0, | |
| '-xxxhdpi': 4.0 | |
| } | |
| def parse_arguments(): | |
| parser = argparse.ArgumentParser(description='Resize and rename an image based on scaling factor or convert SVG to PNGs') | |
| parser.add_argument('input_file', help='Input image or SVG file name') | |
| return parser.parse_args() | |
| def resize_png(image_file): | |
| try: | |
| image = Image.open(image_file) | |
| except IOError: | |
| print(f"Unable to open image file: {image_file}") | |
| return | |
| suffixes = list(density_multipliers.keys()) | |
| base_filename, _ = os.path.splitext(image_file) | |
| new_name = os.path.basename(base_filename) | |
| for suffix in suffixes: | |
| width, height = image.size | |
| w = width * density_multipliers[suffix] | |
| h = height * density_multipliers[suffix] | |
| # Use LANCZOS for high-quality downscaling | |
| resized_image = image.resize((int(w), int(h)), Image.LANCZOS) | |
| output_dir_suffix = f'drawable{suffix}' | |
| output_dir_full = os.path.join(os.path.dirname(image_file), output_dir_suffix) | |
| if not os.path.exists(output_dir_full): | |
| os.makedirs(output_dir_full) | |
| output_path = os.path.join(output_dir_full, f'{new_name}.png') | |
| resized_image.save(output_path) | |
| print(f'Resized PNG saved to {output_path}') | |
| image.close() | |
| def convert_svg_to_png(svg_file): | |
| suffixes = list(density_multipliers.keys()) | |
| base_filename, _ = os.path.splitext(svg_file) | |
| new_name = os.path.basename(base_filename) | |
| for suffix in suffixes: | |
| output_dir_suffix = f'drawable{suffix}' | |
| output_dir_full = os.path.join(os.path.dirname(svg_file), output_dir_suffix) | |
| if not os.path.exists(output_dir_full): | |
| os.makedirs(output_dir_full) | |
| output_path = os.path.join(output_dir_full, f'{new_name}.png') | |
| cairosvg.svg2png( | |
| url=svg_file, | |
| write_to=output_path, | |
| scale=density_multipliers[suffix] | |
| ) | |
| print(f'Converted SVG to PNG at {output_path}') | |
| def handle_file(input_file): | |
| _, file_extension = os.path.splitext(input_file) | |
| if file_extension.lower() == '.png': | |
| resize_png(input_file) | |
| elif file_extension.lower() == '.svg': | |
| convert_svg_to_png(input_file) | |
| else: | |
| print(f"Unsupported file type: {file_extension}. Supported extensions are: {', '.join(SUPPORTED_EXTENSIONS)}") | |
| def main(): | |
| args = parse_arguments() | |
| handle_file(args.input_file) | |
| print('All files processed successfully.') | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment