Created
December 14, 2018 11:36
-
-
Save Kuurse/f42f573970e76900daf00bd2e9c3f00c to your computer and use it in GitHub Desktop.
Resizes icons for Xamarin.Forms applications
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
# run | |
# pip install python-resize-image | |
# pip install pillow | |
from PIL import Image | |
from os import path, scandir | |
from resizeimage import resizeimage, imageexceptions | |
# Resizes an icon and lays them out in the proper folders for Xamarin applications | |
home_directory = path.expanduser("~") | |
# Image sizes | |
ios = 25 | |
ios2x = ios * 2 | |
ios3x = ios * 3 | |
mdpi = 32 | |
hdpi = mdpi * 1.5 | |
xhdpi = mdpi * 2 | |
xxhdpi = mdpi * 3 | |
xxxhdpi = mdpi * 4 | |
################################################## | |
############### Configure here ################# | |
################################################## | |
desired_image_name = "decline" | |
source_image_path = home_directory + "/Desktop/temp/my_icon.png" | |
# Directory containting MyProject.Android, MyProject.iOS folders... | |
project_path = home_directory + "/MyXamarinProject/" | |
################################################# | |
desired_image_name += ".png" | |
project_name = "" | |
directories = [f.path for f in scandir(project_path) if f.is_dir()] | |
for m_dir in directories: | |
if ".Android" in m_dir: | |
project_name = m_dir.replace(project_path, "").replace(".Android", "") | |
if project_name == "": | |
raise Exception() | |
def resize(image, size): | |
try: | |
c = resizeimage.resize_cover(image, [size, size]) | |
return c | |
except imageexceptions.ImageSizeError: | |
return resize(image, size-1) | |
def resize_android(img_path, size): | |
if path.exists(img_path): | |
with open(img_path, 'r+b') as f: | |
with Image.open(f) as image: | |
cover = resize(image, size) | |
resource_folder = project_path + project_name + ".Android/Resources/drawable-" | |
output_path = "" | |
if size == mdpi: | |
mdpi_folder = resource_folder + "mdpi/" | |
output_path = mdpi_folder | |
elif size == hdpi: | |
hdpi_folder = resource_folder + "hdpi/" | |
output_path = hdpi_folder | |
elif size == xhdpi: | |
xhdpi_folder = resource_folder + "xhdpi/" | |
output_path = xhdpi_folder | |
elif size == xxhdpi: | |
xxhdpi_folder = resource_folder + "xxhdpi/" | |
output_path = xxhdpi_folder | |
elif size == xxxhdpi: | |
xxxhdpi_folder = resource_folder + "xxxhdpi/" | |
output_path = xxxhdpi_folder | |
final_path = output_path + desired_image_name | |
cover.save(final_path) | |
print("Generated android {}".format(size)) | |
def resize_ios(img_path, size): | |
if path.exists(img_path): | |
with open(img_path, 'r+b') as f: | |
with Image.open(f) as image: | |
cover = resizeimage.resize_cover(image, [size, size]) | |
output_path = project_path + project_name + ".iOS/Resources/" | |
suffix = "" | |
if size == ios2x: | |
suffix = "@2x" | |
elif size == ios3x: | |
suffix = "@3x" | |
temp_name = desired_image_name.replace(".png", "") | |
full_name_debug = output_path + temp_name + suffix + ".png" | |
cover.save(full_name_debug) | |
print("Generated ios {}".format(size)) | |
directories = [f.path for f in scandir(project_path) if f.is_dir()] | |
for m_dir in directories: | |
if ".Android" in m_dir: | |
project_name = m_dir.replace(project_path, "").replace(".Android", "") | |
# Resize for iOS then Android | |
resize_ios(source_image_path, ios) | |
resize_ios(source_image_path, ios2x) | |
resize_ios(source_image_path, ios3x) | |
resize_android(source_image_path, mdpi) | |
resize_android(source_image_path, hdpi) | |
resize_android(source_image_path, xhdpi) | |
resize_android(source_image_path, xxhdpi) | |
resize_android(source_image_path, xxxhdpi) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment