{"id":21513242,"url":"https://github.com/patrickfav/density-converter","last_synced_at":"2025-04-12T18:50:11.885Z","repository":{"id":4323845,"uuid":"52564417","full_name":"patrickfav/density-converter","owner":"patrickfav","description":"A multi platform image density converting tool converting single or batches of images to Android, iOS, Windows or CSS specific formats and density versions given the source scale factor or width/height in dp. It has a graphical and command line interface and supports many image types (svg, psd, 9-patch, etc.) aswell as some lossless compressors like pngcrush.","archived":false,"fork":false,"pushed_at":"2024-10-14T10:16:53.000Z","size":3061,"stargazers_count":247,"open_issues_count":6,"forks_count":30,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-04-03T21:14:04.812Z","etag":null,"topics":["android","cli","converter","density","dpi","drawable","gui","ios","jpeg","mozjpeg","nine-patch","png","pngcrush","post-processor","psd","resized-images","svg"],"latest_commit_sha":null,"homepage":"https://favr.dev/opensource/density-converter","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patrickfav.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-25T23:31:35.000Z","updated_at":"2025-01-11T16:17:11.000Z","dependencies_parsed_at":"2023-07-06T20:01:47.631Z","dependency_job_id":"e04de7c7-8af2-43b0-b4e9-3af68435816f","html_url":"https://github.com/patrickfav/density-converter","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickfav%2Fdensity-converter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickfav%2Fdensity-converter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickfav%2Fdensity-converter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickfav%2Fdensity-converter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrickfav","download_url":"https://codeload.github.com/patrickfav/density-converter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248618219,"owners_count":21134199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","cli","converter","density","dpi","drawable","gui","ios","jpeg","mozjpeg","nine-patch","png","pngcrush","post-processor","psd","resized-images","svg"],"created_at":"2024-11-23T22:55:07.776Z","updated_at":"2025-04-12T18:50:11.861Z","avatar_url":"https://github.com/patrickfav.png","language":"Java","readme":"# ![logo](src/main/resources/img/density_converter_icon_24.png) Density Image Converter Tool for Android, iOS, Windows and CSS\n\nThis is a powerful little tool that helps **converting single or batches of images** to **Android**, **iOS**, **Windows** and **CSS** specific formats and density\nversions given the source scale factor or target width/height in [dp](https://developer.android.com/guide/practices/screens_support.html#density-independence).\nIt has a **graphical** and **command line** interface and supports a wide array of image types for reading and conversion\nincluding PNG, JPEG, SVG, PSD and Android 9-patches. Using sophisticated scaling algorithms, it is designed to make conversion of images easy and\nfast while keeping the image quality high (comparable to PS). To further optimize the output post processors like **pngcrush** and **mozJpeg** can be used (see section below).\n\n[![GitHub release](https://img.shields.io/github/release/patrickfav/density-converter.svg)](https://github.com/patrickfav/density-converter/releases/latest)\n[![Github Actions](https://github.com/patrickfav/density-converter/actions/workflows/build_deploy.yml/badge.svg)](https://github.com/patrickfav/density-converter/actions)\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=patrickfav_density-converter\u0026metric=coverage)](https://sonarcloud.io/summary/new_code?id=patrickfav_density-converter)\n[![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=patrickfav_density-converter\u0026metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=patrickfav_density-converter)\n\nUsage:\n\n```\njava -jar ./dconvert.jar -src \"C:/your-folder/image-folder\" -scale 4\n```\n\nwhile\n\n```\njava -jar ./dconvert.jar\n```\n\nwill start the UI (_or double click the jar file_).\n\n_To use the post processor ([pngcrush](https://pmt.sourceforge.net/pngcrush/), [mozJpeg](https://mozjpeg.codelove.de/binaries.html) and [webp](https://developers.google.com/speed/webp/download)) the respective cmd tool must be set in PATH._\n\n\n## Download\n\n**[Grab jar from latest release.](https://github.com/patrickfav/density-converter/releases/latest)**\n\n### Requirements\n\n* JDK 11 with a [JavaFX packaged](https://stackoverflow.com/questions/58354622/javafx-is-missing-in-java-openjdk), like [Azul](https://www.azul.com/downloads/?package=jdk-fx#zulu) or [Liberica](https://bell-sw.com/pages/downloads/)\n\n# Usage\n\n## GUI\n\nStart with\n\n```\njava -jar ./dconvert.jar\n```\n\nprovides the same features as the command line tool so see the help page of command line or tooltips. Additionally the UI\nwill save its settings persistently making it easier for power users.\n\n![gui-screenshot](src/main/resources/img/screenshot1.jpg)\n\n## Command Line\n\nE.g. with `xxxhdpi` (x4) source files:\n\n```\njava -jar ./dconvert.jar -src \"C:/master-image/ic_my_icon.png\" -scale 4 -platform android\n```\n\nWill generate `mdpi`, `hdpi`, etc. folders in \"C:/master-image/\" containing the resized images\n\nFull list of arguments:\n\n    -androidIncludeLdpiTvdpi              Android only: creates mipmap sub-folders instead of drawable.\n    -androidMipmapInsteadOfDrawable       Android only: If set will include additional densities (ldpi and\n                                          tvdpi)\n    -antiAliasing                         Anti-aliases images creating a little more blurred result; a light\n                                          3x3 convolve matrix is used; useful for very small images\n    -clean                                Deletes all file and folders in out dir that would be used in\n                                          current configuration before converting.\n    -compressionQuality \u003c0.0-1.0\u003e         Only used with compression 'jpg' sets the quality [0-1.0] where 1.0\n                                          is the highest quality. Default is 0.9\n    -dryRun                               Will not create any images or folder. Useful as fast preview in log\n                                          what images in what resolutions would be created.\n    -dst \u003cpath\u003e                           The directory in which the converted files will be written. Will use\n                                          the source folder if this argument is omitted.\n    -gui                                  Starts graphical user interface\n    -h,--help                             This help page\n    -haltOnError                          If set will stop the process if an error occurred during conversion\n    -iosCreateImagesetFolders             iOS only: Will create .imageset folders and Content.json for every\n                                          source image n iOS converter. Default is just all images in the root\n                                          folder.\n    -keepOriginalPostProcessedFiles       If a post processor is run on a file, this flag will ensure that the\n                                          original will be kept (renamed _orig), otherwise only the optimized\n                                          image will be kept\n    -outCompression \u003cpng|jpg|gif|bmp\u003e     Sets the compression of the converted images. Can be 'png', 'jpg',\n                                          'gif', 'bmp', 'png+jpg' or 'strict' which tries to use same\n                                          compression as source. By default will convert to png except if\n                                          source compression is jpeg.\n    -platform \u003call|android|ios|win|web\u003e   Can be 'all', 'android', 'ios', 'win' or 'web'. Sets what formats\n                                          the converted images will be generated for. E.g. set 'android' if\n                                          you only want to convert to android format. Default is [IOS,\n                                          ANDROID]\n    -postProcessorMozJpeg                 Will post-process all jpegs with mozJpegs lossless compressor\n                                          'jpegtran'. Mozjpeg (specifically 'jpegtran') must be set in PATH,\n                                          tested with mozJpeg 3. Binaries for mozJpeg can be found on the\n                                          internet.\n    -postProcessorPngCrush                Will post-process all pngs with pngcrush, a lossless compressor. The\n                                          executable must be set in the system PATH as 'pngcrush' i.e\n                                          executable from every path. Pngcrush is a tool to compress pngs.\n                                          Requires v1.7.22+\n    -postProcessorWebp                    Will additionally convert all png/gif to lossless wepb and all jpg\n                                          to lossy webp with cwebp. Does not delete source files. The\n                                          executable must be set in the system path as 'cwebp' i.e executable\n                                          from every path. cwebp is the official converter from Google.\n    -roundingMode \u003cround|ceil|floor\u003e      Defines the rounding mode when scaling the dimensions. Possible\n                                          options are 'round' (rounds up of \u003e= 0.5), 'floor' (rounds down) and\n                                          'ceil' (rounds up). Default is ROUND_HALF_UP\n    -scale \u003c[float]|[int]dp\u003e              The source scale. This can either be a factor (1,1.5,2,3,4,etc.)\n                                          used if the images already have the correct resolution for one scale\n                                          factor and up- and downscaling for all other densities are needed.\n                                          Ie. if you have the src file in density xxxhdpi you pass '4'. You\n                                          could also pass a value in dp (density independent pixels) which\n                                          denotes the output pixel width (or height if the flag is set) in\n                                          mdpi/x1. In this mode all output images will have the same width\n                                          (height). This argument is mandatory.\n    -scaleIsHeightDp                      If set and scale is in dp it will be interpreted as fixed height not\n                                          width\n    -skipExisting                         If set will not overwrite a already existing file\n    -skipUpscaling                        If set will only scale down, but not up to prevent image quality\n                                          loss\n    -src \u003cpath to file or folder\u003e         The source. Can be an image file or a folder containing image files\n                                          to be converted. This argument is mandatory.\n    -threads \u003c1-8\u003e                        Sets the count of max parallel threads (more is faster but uses more\n                                          memory). Possible values are 1-8. Default is 4\n    -v,--version                          Gets current version\n    -verbose                              If set will log to console more verbose\n\n## Using the .exe Launcher\n\n[Launch4J](https://launch4j.sourceforge.net) is used to create Windows executables. Two types are available: `dconvert.exe` and `dconvert-gui.exe`. The former is for command line, the later\nas a simple GUI starter. Launch4J will automatically download the required JRE.\n\n# Details\n\n## Scale Factor vs. Fixed DP Width/Height\n\nThere are 2 modes available for the user:\n\n### Scale Factor Mode\n\nThis mode is based on the idea that a developer gets the 'master' images in the highest needed resolution for his platform,\ne.g. `xxxhdpi` (x4) in Android. He then proceeds to generate every other density with it.\n\nTo better understand, here is a practical example for Android: Source file is `ic_my_icon.png` in density xxxhdpi with resolution 144x144 (this will be 36dp x 36dp).\nThe tool will generate the following images in the following resolutions:\n\n```\n* mdpi 36x36 (x1)\n* hdpi 54x54 (x1.5)\n* xhdpi 72x72 (x2)\n* xxhdpi 108x108 (x3)\n* xxxhdpi 144x144 (x4)\n```\n\n### Fixed Width/Height in DP Mode\n\nThis mode is suitable if the source image is in an arbitrary resolution (but usually greater resolution than needed) and\nthe needed density independent pixel (dp) dimensions are known to the developer. Either the width or height can be fixed\nwith a dp value, while calculating the other dimension accordingly (keeping aspect ratio). In this mode all images will\nhave the same pixel width (or height) for the same density bucket. Here is a practical example with fixed width:\n\nYou get an icon in 512x512 and want to display it in 48x48dp. The tool will generate the following resolutions:\n\n```\n* mdpi 48x48 (x1)\n* hdpi 72x72 (x1.5)\n* xhdpi 108x108 (x2)\n* xxhdpi 144x144 (x3)\n* xxxhdpi 192x192 (x4)\n```\n\n## Supported Platforms\n\nThis tool can convert to formats used in Android, iOS and Windows:\n\n * **Android**: The Android converter will create `drawable-ldpi`, `drawable-mdpi`, etc. folders and saves images with the original name and respective scale factor in there. 9-Patches are specifically supported for Android - must have `.9.png` extension and only size correctly if out-compression is PNG.\n * **iOS**: The iOS converter will either create a folder for each image with the `.imageset` postfix and 3 images in them with no, `@2x` and `@3x` postfix also including is the `Content.json`; or creates a single folder `AssetCatalog` with all the images in it.\n * **Windows**: The Windows converter will create an `Assets` folder containing all the images with `.scale-100`, `.scale-140` etc. postfixes. This should be compatible with Windows universal apps.\n * **Web**: The web converter will create an `img` folder containing all the images with a `1x` and a `2x` scale factor which is compatible with [the css image-set](https://www.hongkiat.com/blog/css-image-set/) definition for multiple densities.\n\nThere are some options that might change the format of the output. See cmd help or tooltips for more info.\n\n## Supported File Types\n\nThis tool can read \u0026 write the following file types:\n\n* PNG - read and write support (including 9-patches format)\n* JPEG - read and write support with quality parameter\n* GIF - read and write support (no animation support)\n* SVG - can read svg, will compress with png per default\n* PSD - can read some Photoshop files, will compress with png per default\n* TIFF - read and write support\n* BMP - read and write support\n\nDetails on the [supported features of each format see here](https://github.com/haraldk/TwelveMonkeys).\n\nOut-compression has the following modes:\n\n* as input (prefer png): jpegs will output jpeg, all other types png\n* as input (strict): except PSD \u0026 SVG which will use png, every file will be compressed with the source compression\n* [force compression]: jpeg, png, gif and bmp can be forced\n\nIf an image will be re-compressed with a file type that does not support alpha, alpha will be replaced with white (e.g. when converting png -\u003e jpeg)\n\n## Quality Comparison\n_Note this will be available in future versions._\n\nOne of the main features of this converter is downscaling. Unfortunately without using specialized algorithms, down scaled images may look jagged or too blurred. I tested the algorithms of [Thumbnailator](https://github.com/coobird/thumbnailator) and [imgscalr](https://github.com/thebuzzmedia/imgscalr) which basically uses [Chris Campbell's proposed progressive scaling](https://community.oracle.com/docs/DOC-983611) algorithm. Upon further analysis [Lanczos3 filtering](https://en.wikipedia.org/wiki/Lanczos_resampling) seems to be recommended which produces results preserving more detail without being too jagged. The downside is, it is considerably slower than the former. This tool uses the implementation of [Morten Nobel](https://github.com/mortennobel/java-image-scaling).\n\nThe tool provides 3 quality profiles:\n\n * **High Quality**: uses `Lanczos3` filtering (8 times slower than **Balanced**)\n * **Balanced**: uses progressive bilinear algorithm for downscaling and bicubic filtering for upscaling (6 times slower than speed)\n * **Speed**: uses nearest neighbor algorithm\n\nIn most cases `Lanczos3` creates best results, although in some cases progressive bilinear scaling may be superior for very sharp edges of e.g. simple icons where the former creates some softness. Here are some example to see the difference yourself. The icons are down-scaled from 96x96 to 72x72 and 48x48, the photos from different source from 500 to 2000 px.\n\n![comparison](src/main/resources/img/comparison_icons_72.png)\n![comparison](src/main/resources/img/comparison_icons_48.png)\n\n![comparison](src/main/resources/img/comparison_photo_144.png)\n![comparison](src/main/resources/img/comparison_photo_72.png)\n\nAs reference, here are the results of other methods:\n\n![comparison-ldpi](src/main/resources/img/mdpi_comparison.png)\n\n1. Thumbnailator with default settings (_progressive bilinear_)\n2. Photoshop CS5 with bicubic algorithm\n3. imgscalr with ULTRA_QUALITY setting (_progressive bilinear_)\n4. simple Graphics2d with render hints `VALUE_INTERPOLATION_BICUBIC`, `VALUE_RENDER_QUALITY`, `VALUE_ANTIALIAS_ON`\n\n## Post Processors\n\nThe converter tool supports some post processors which run after the main conversation task to manipulate the resulting files.\nPost processors can be easily implemented by implementing the `PostProcessor` interface.\n\n### pngcrush\n\nPngcrush is a brute force png lossless compression tool. This may be already in your build chain (Android does this automatically),\nbut may be useful if not. Will run pngcrush over the all converted *.png files. See code or output for specific used options.\n\nThe converter tool requires `pngcrush` to be in PATH. Minimal required version is 1.7.22; currently tested with v1.7.87 \u0026 1.8.0.\n[More info on the pngcrush wesbite](https://pmt.sourceforge.net/pngcrush/)\n\n### webp\n\nWebP is an image format employing both lossy and lossless compression developed by Google. It is natively supported in Android\nand claims to be 25-35% smaller in file size than png and jpeg (in their respective categories).\n\nIf the flag is set the converter tool will compress jpegs to lossy webp and png/gif to lossless version of it. The program\n `cwebp`, which can be downloaded from [Google's page](https://developers.google.com/speed/webp/docs/cwebp), must be set in PATH variable.\n\n[More info on webp](https://developers.google.com/speed/webp/)\n\n### mozJpeg\nMozJpeg is a Jpeg encoder that also features some lossless compression techniques developed by the Mozilla foundation. It can be\n used to further shrink jpegs in size. Unfortunately they do not provide binaries on github,\n [but they can be found e.g. here](https://mozjpeg.codelove.de/binaries.html). The tool requires `jpegtran` to be set in PATH. Tested with mozJpeg 3.1\n\n[More info on mozJpeg](https://github.com/mozilla/mozjpeg)\n\n## Implementation Details\n\nJava 11, JavaFx and Apache CLI\n\nFor support of various image types TwelveMonkey lib is used which claims to have wider support of even basic file types (jpeg, etc.)\n\nFor parallel execution, every source file spawns his own thread for each platform converter e.g. 3 files 2 converters -\u003e 6 threads needed.\n\nThe tool can easily be extended by using the `IPlatformConvert` and `PostProcessor` interfaces.\n\nBoth user interfaces use the same underlying logic.\n\n## Limitations \u0026 Known Issues\n\n* You tell me\n\n## Possible Future Features\n\n* change SVG source resolution to max needed resolution so no upscaling is needed\n\n## How to build\n\nUse the Maven wrapper to create a jar including all dependencies\n\n```\nmvnw clean package\n```\n\nThe .jar file will be in the generated `/target` folder\n\n### Checkstyle Config File\n\nThis project uses my [`common-parent`](https://github.com/patrickfav/mvn-common-parent) which centralized a lot of\nthe plugin versions aswell as providing the checkstyle config rules. Specifically they are maintained in [`checkstyle-config`](https://github.com/patrickfav/checkstyle-config). Locally the files will be copied after you `mvnw install` into your `target` folder and is called\n`target/checkstyle-checker.xml`. So if you use a plugin for your IDE, use this file as your local configuration.\n\n# About\n\n## Credits\n\n* TIFF, PSD, SVG image type support [TwelveMonkeys](https://github.com/haraldk/TwelveMonkeys)\n* HQ image resizing with [Morten Nobel's Lib](https://github.com/mortennobel/java-image-scaling)\n* JavaFx theme based on Flatter by [hendrikebbers](https://github.com/guigarage/javafx-collection/tree/master/flatter)\n* Image file icon: Icon made by [Freepik](https://www.freepik.com/)\n* 9-patch resizing routine from [Redwarp](https://github.com/redwarp/9-Patch-Resizer)\n\n# License\n\nCopyright 2016 Patrick Favre-Bulle\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","funding_links":[],"categories":["Libs"],"sub_categories":["\u003cA NAME=\"Tools\"\u003e\u003c/A\u003eTools"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickfav%2Fdensity-converter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickfav%2Fdensity-converter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickfav%2Fdensity-converter/lists"}