├── README.md ├── gimpfu_android_xdpi.py └── screenshot.png /README.md: -------------------------------------------------------------------------------- 1 | gimp-android-xdpi 2 | ================= 3 | 4 | Gimp plugin to export images or icons for any android density : 5 | [gimpfu_android_xdpi.py](https://github.com/ncornette/gimp-android-xdpi/raw/master/gimpfu_android_xdpi.py) 6 | 7 | ![Plugin Screenshot](https://github.com/ncornette/gimp-android-xdpi/raw/master/screenshot.png) 8 | 9 | ### Installation 10 | 11 | - Download the script [gimpfu_android_xdpi.py](https://github.com/ncornette/gimp-android-xdpi/raw/master/gimpfu_android_xdpi.py), 12 | save into your gimp plugin directory, ie: `~/.gimp-2.6/plug-ins/gimpfu_android_xdpi.py` 13 | - On Linux or MacOSX you need to set the script file to be executable. (`chmod +x gimpfu_android_xdpi.py`) 14 | - Restart Gimp 15 | - Run script via Filters -> Android -> Write Android XDPIs... 16 | 17 | ### Usage 18 | 19 | - Select area to export. 20 | - Run the plugin from Filters -> Android -> Write Android XDPIs... 21 | - Enter the target DP width. 22 | - Choose density folders to export to : ldpi / mdpi / hdpi / xhdpi / xxhdpi / xxxhdpi. 23 | 24 | ### Example 25 | 26 | To write you app icon directly from you full size picture, select area for your icon : 27 | 28 | 29 | [![icon selection](https://lh5.googleusercontent.com/9ovI_ry3awmPs0tWjt2b08c5ykQxwFz7GQgltRxs3BOV5LREYr4pWPrMNunrZDeP5zCqig1kjiUmnd5-CmXzCNk_oKPTRC5i_qpbZBI_cazA29VC4dw)](https://lh5.googleusercontent.com/9ovI_ry3awmPs0tWjt2b08c5ykQxwFz7GQgltRxs3BOV5LREYr4pWPrMNunrZDeP5zCqig1kjiUmnd5-CmXzCNk_oKPTRC5i_qpbZBI_cazA29VC4dw) 30 | 31 | Use the reference values for an app icon, 48px on mdpi : 32 | 33 | ![Plugin Screenshot](https://github.com/ncornette/gimp-android-xdpi/raw/master/screenshot.png) 34 | 35 | 36 | 1. select your app res folder 37 | 2. Pick drawable or mipmap as your export folder prefix 38 | 3. type `icon` as the image base name 39 | 4. select image DP width of `48` 40 | 5. select densities to export 41 | 6. select image format `png` 42 | 43 | 44 | Icon or Image resources for all densities will be scaled and written 45 | accordingly, except that by default upscaled images won't be upscaled. You 46 | can force creating upscaled images by using the appropiate option. 47 | 48 | [![export result](https://lh6.googleusercontent.com/LT7vn7uo2jmjul4ejuu59iM4elDto1TsjagX1Zp5wdgzPghQ_TBsUKGOF65y7m6XwW2DaTpJlxS2GxU9Xi3jklrxj2bR8c6d8blc6dgi8Iwnri56SlM)](https://lh6.googleusercontent.com/LT7vn7uo2jmjul4ejuu59iM4elDto1TsjagX1Zp5wdgzPghQ_TBsUKGOF65y7m6XwW2DaTpJlxS2GxU9Xi3jklrxj2bR8c6d8blc6dgi8Iwnri56SlM) 49 | 50 | -------------------------------------------------------------------------------- /gimpfu_android_xdpi.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | ''' 3 | Created on 2012/04/20 4 | 5 | @author: nic 6 | 7 | This is a Gimp plugin 8 | 9 | Actions: 10 | - Save Visible Selection to android drawables into 11 | - res/drawable-ldpi 12 | - res/drawable-mdpi 13 | - res/drawable-hdpi 14 | - res/drawable-xhdpi 15 | - You can select a new width for the drawable and select the target density. 16 | - Drawables for other densities will be scaled accordingly 17 | 18 | Installation: 19 | - Put this file into your gimp plugin directory, ie: ~/.gimp-2.6/plug-ins/gimpfu_android_xdpi.py 20 | - Restart Gimp 21 | - Run script via Filters/Android/Write Android XDPIs... 22 | ''' 23 | 24 | import gimpfu 25 | import gimp 26 | import os 27 | 28 | DEFAULT_OUTPUT_DIR = os.getcwd() 29 | DEFAULT_OUTPUT_EXT = 'png' 30 | DEFAULT_FOLDER_PREFIX = 'drawable' 31 | 32 | UPSCALE_WARN_MESSAGE = '\nQuality of your application could be seriously affected when using upscaled bitmaps !' 33 | 34 | def write_xdpi(img, layer, res_folder, folder_prefix, image_basename, target_width, x_ldpi, x_mdpi, x_hdpi, x_xhdpi, x_xxhdpi, x_xxxhdpi, allow_upscale, image_extension): 35 | ''' 36 | Resize and write images for all android density folders 37 | 38 | @param img: gimp image 39 | @param layer: gimp layer (or drawable) 40 | @param res_folder: output directory : basically res folder of your android project 41 | @param folder_prefix: android mipmap or drawable folder 42 | @param image_basename: basename of your image, ex: icon 43 | @param target_width: new width for your image 44 | @param target_dpi: reference density for your target width 45 | @param allow_upscale: whether to create upscaled images 46 | @param image_extension: output format 47 | ''' 48 | 49 | warnings = list() 50 | 51 | gimpfu.pdb.gimp_edit_copy_visible(img); #@UndefinedVariable 52 | 53 | dpi_ratios = (('ldpi', 0.75 ,x_ldpi), 54 | ('mdpi', 1 ,x_mdpi), 55 | ('tvdpi', 1.33 ,False), 56 | ('hdpi', 1.5 ,x_hdpi), 57 | ('xhdpi', 2 ,x_xhdpi), 58 | ('xxhdpi', 3 ,x_xxhdpi), 59 | ('xxxhdpi', 4 ,x_xxxhdpi)) 60 | 61 | for folder, ratio, export in dpi_ratios: 62 | if not export: 63 | continue 64 | 65 | new_img = gimpfu.pdb.gimp_edit_paste_as_new(); #@UndefinedVariable 66 | 67 | resize_ratio = float(target_width) / new_img.width 68 | target_dp_width = target_width 69 | target_dp_height = round(new_img.height * resize_ratio) 70 | 71 | # Compute new dimensions for the image 72 | new_width = target_dp_width * ratio 73 | new_height = target_dp_height * ratio 74 | 75 | print('%s : %dx%d' % (folder, new_width, new_height)) 76 | 77 | if (new_width>new_img.width): 78 | if not allow_upscale: 79 | warnings.append('Not creating resource for %s upscaled by %0.2f' % 80 | (folder, new_width/new_img.width)) 81 | continue 82 | else: 83 | warnings.append('Resource for %s has been upscaled by %0.2f' % 84 | (folder, new_width/new_img.width)) 85 | 86 | target_res_folder = os.path.join(res_folder, folder_prefix + '-' + folder) 87 | if (os.path.exists(res_folder) and not os.path.exists(target_res_folder)): 88 | os.makedirs(target_res_folder) 89 | 90 | target_res_filename = os.path.join(target_res_folder, image_basename + '.' + image_extension) 91 | 92 | # Save the new Image 93 | gimpfu.pdb.gimp_image_scale_full( #@UndefinedVariable 94 | new_img, new_width, new_height, gimpfu.INTERPOLATION_CUBIC) 95 | 96 | gimpfu.pdb.gimp_file_save( #@UndefinedVariable 97 | new_img, new_img.layers[0], target_res_filename, target_res_filename) 98 | 99 | gimpfu.pdb.gimp_image_delete(new_img) #@UndefinedVariable 100 | 101 | # Show warning message 102 | if warnings: 103 | warnings.append(UPSCALE_WARN_MESSAGE) 104 | gimp.message('\n'.join(warnings)) 105 | 106 | gimpfu.register("python_fu_android_xdpi", 107 | "Write Android drawables for all DPI folders", 108 | "Write images for all android densities", 109 | "Nic", "Nicolas CORNETTE", "2012", 110 | "/Filters/Android/Write Android XDPIs...", 111 | "*", [ 112 | (gimpfu.PF_DIRNAME, "res-folder", "Project res Folder", DEFAULT_OUTPUT_DIR), #os.getcwd()), 113 | (gimpfu.PF_RADIO, "folder-prefix", "Android Folder Prefix", DEFAULT_FOLDER_PREFIX, (("drawable", "drawable"), ("mipmap", "mipmap"))), 114 | (gimpfu.PF_STRING, "image-basename", "Image Base Name", 'icon'), 115 | (gimpfu.PF_SPINNER, "target-width", "Target DP Width", 48, (1, 8000, 2)), 116 | (gimpfu.PF_BOOL, "x_ldpi", " Export ldpi", False), 117 | (gimpfu.PF_BOOL, "x_mdpi", " Export mdpi", True), 118 | (gimpfu.PF_BOOL, "x_hdpi", " Export hdpi", True), 119 | (gimpfu.PF_BOOL, "x_xhdpi", " Export xhdpi", True), 120 | (gimpfu.PF_BOOL, "x_xxhdpi", " Export xxhdpi", True), 121 | (gimpfu.PF_BOOL, "x_xxxhdpi", " Export xxxhdpi",False), 122 | #(gimpfu.PF_BOOL, "x_tvdpi", " Export tvdpi", False), 123 | (gimpfu.PF_BOOL, "allow_upscale", " Create upscaled images", False), 124 | (gimpfu.PF_RADIO, "image-extension", "Image Format", DEFAULT_OUTPUT_EXT, (("gif", "gif"), ("png", "png"), ("jpg", "jpg"))), 125 | ], 126 | [], 127 | write_xdpi) #, menu, domain, on_query, on_run) 128 | 129 | gimpfu.main() 130 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ncornette/gimp-android-xdpi/0977fada4f232319a856b11385d6bfb6917b7579/screenshot.png --------------------------------------------------------------------------------