{"id":19600223,"url":"https://github.com/habedi/mosaic-art-maker","last_synced_at":"2025-08-04T14:34:42.743Z","repository":{"id":82476160,"uuid":"583000529","full_name":"habedi/mosaic-art-maker","owner":"habedi","description":"Making mosaic art using a Stable Diffusion model","archived":false,"fork":false,"pushed_at":"2025-03-17T18:01:27.000Z","size":85336,"stargazers_count":10,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T01:32:20.590Z","etag":null,"topics":["computer-vision","generative-art","generative-model","jupyter-notebook","keras","mosaic-images","python","stable-diffusion","tesnorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","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/habedi.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2022-12-28T13:21:44.000Z","updated_at":"2025-03-30T04:42:35.000Z","dependencies_parsed_at":"2025-03-17T18:42:31.137Z","dependency_job_id":null,"html_url":"https://github.com/habedi/mosaic-art-maker","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmosaic-art-maker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmosaic-art-maker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmosaic-art-maker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/habedi%2Fmosaic-art-maker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/habedi","download_url":"https://codeload.github.com/habedi/mosaic-art-maker/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251171604,"owners_count":21547119,"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":["computer-vision","generative-art","generative-model","jupyter-notebook","keras","mosaic-images","python","stable-diffusion","tesnorflow"],"created_at":"2024-11-11T09:14:03.050Z","updated_at":"2025-08-04T14:34:42.729Z","avatar_url":"https://github.com/habedi.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Creating Mosaic Art using AI\n\nThis repository includes the code (and other files) for creating [mosaic art](https://en.wikipedia.org/wiki/Mosaic)\nversions of a given image. Given an image\n(referred to as the original image), the main idea is to replace (square-shaped) patches of the original image with the\nmost similar image from a given set of images (referred to as the tile images or simply tiles).\n\nThe implementation uses the [Stable Diffusion](https://en.wikipedia.org/wiki/Stable_Diffusion) model available from\n[keras-cv](https://github.com/keras-team/keras-cv) submodule of [Keras](https://keras.io/) to create the tiles.\nIt allows limitless creativity by creating mosaic art of the same image using different tiles created by changing\nthe model's parameters and using different text prompts.\n\nIn the examples below, each row shows an (original) image and two mosaic art images created from it.\nThe left-most image is the original image, and the middle image is the mosaic art created using 2,500 tiles, and the\nright-most image is the mosaic art created using 90,000 tiles.\n\n\n\u003cp align=\"center\"\u003e\n\u003cimg alt=\"Original image 2\" title=\"Nixon Visions\" src=\"images/canvases/nixon_visions.png\" width=\"256\" height=\"256\"/\u003e \n\u003cimg alt=\"Mosaic art image 21\" title=\"The mosaic art version of Nixon Visions; made of 2.5k tiles\" src=\"images/examples/nixon_visions_mosaic_art_50.png\" width=\"256\" height=\"256\"/\u003e\n\u003cimg alt=\"Mosaic art image 22\" title=\"The mosaic art version of Nixon Visions; made of 90k tiles\" src=\"images/examples/nixon_visions_mosaic_art_300.png\" width=\"256\" height=\"256\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg alt=\"Original image 1\" title=\"A photo of Miley Cyrus\" src=\"images/canvases/miley_cyrus.png\" width=\"256\" height=\"256\"/\u003e \n\u003cimg alt=\"Mosaic art image 11\" title=\"The mosaic art version of Miley Cyrus's photo; made of 2.5k tiles\" src=\"images/examples/miley_cyrus_mosaic_art_50.png\" width=\"256\" height=\"256\"/\u003e\n\u003cimg alt=\"Mosaic art image 12\" title=\"The mosaic art version of Miley Cyrus's photo; made of 90k tiles\" src=\"images/examples/miley_cyrus_mosaic_art_300.png\" width=\"256\" height=\"256\"/\u003e\n\u003c/p\u003e\n\n### Getting started\n\n#### Project Structure\n\nThe main files and folders included in this repository are:\n\n1. [images/canvases](images/canvases): This folder includes the images. Place your image files in this folder\n   to create mosaic art versions of them. Please note that the images must have the same dimensions (width and\n   height).\n2. [images/tiles](images/tiles): This folder includes the tile images. There are already some tile images in this\n   folder. You can use an instance of MosiacArtMaker to create your own custom set of tile images and put them in\n   the `images/tiles` folder.\n3. [images/output](images/outputs): The mosaic art images are saved in this folder.\n4. [notebooks/make_mosaic_art.ipynb](notebooks/make_mosaic_art.ipynb): This notebook includes the code for creating\n   mosaic\n   art versions of the images in the `images/canvases` folder and also the code for creating the\n   tile images.\n\n#### Setting Up Python Environment\n\n```shell\n# Clone the repository\ngit clone https://github.com/habedi/mosaic-art-maker\n\n# Change the working directory\ncd mosaic-art-maker\n\n# Create a Poetry environment and install the dependencies (need Poetry installed)\npoetry install --no-root\n```\n\n#### Running the Code\n\nOpen [Making Mosaic Art using KerasCV+StableDiffusion](notebooks/make_mosaic_art.ipynb) notebook to see the code.\n\nFurthermore, you can modify the variables and parameters in the main function (shown below) to create your own mosaic\nart versions of the given images. You can place your images in the `images/canvases` folder if you want to\ncreate mosaic art versions of them.\n\n```python\ndef main(remake_tiles: bool) -\u003e None:\n    \"\"\"Main function to pack everything together and run it\"\"\"\n\n    # Extension to use for saving and loading the tile images\n    tile_file_extension = \"jpeg\"\n\n    # (Re)-make the tile images if the user wants to do so\n    if remake_tiles:\n\n        # Create a MosaicMaker object to make the tile images\n        image_maker = MosaicMaker(img_width=400, img_height=400, jit_compile=False, seed=33)\n\n        # The text prompts to be used to make the tile images\n        prompt_seq = ((\"A laughing woman\", (\"realistic\", \"white background\")),\n                      (\"A sad girl\", (\"realistic\", \"white background\")),\n                      (\"An old man\", (\"realistic\", \"white background\")),\n                      (\"Face of a sad man\", (\"realistic\", \"white background\")),\n                      (\"Drawing of rings of Saturn\", (\"abstract\", \"white background\")),\n                      (\"A watercolor painting of a puppy\", (\"detailed\",)),\n                      (\"Drawing of a red rose\", (\"elegant\", \"detailed\", \"white background\")),\n                      (\"View of a green forest with mountains in the background\", (\"elegant\", \"lush\", \"nature\")),\n                      (\"A painting of four oranges in a bowl\", (\"elegant\", \"detailed\", \"white background\")),\n                      (\"A ninja shuriken\", (\"realistic\", \"metal\", \"white background\")),)\n\n        # Make the tile images and save them\n        for index, prompt_data in enumerate(prompt_seq):\n            image_seq = image_maker.make_images(prompt_data[0], prompt_data[1], num_images=40)\n            image_maker.save_images(img_seq=image_seq, path='images/tiles', prefix=f'p{index}',\n                                    extension=tile_file_extension)\n\n    # Use the images in the images/canvases and images/tiles directories to make mosaic arts\n    for canvas_image_path in pathlib.Path(\"images/canvases\").glob(\"*.png\"):\n        # Create a MosaicArtMaker object with about sqrt_num_tiles*sqrt_num_tiles tiles!\n        art_maker = MosaicArtMaker(original_image_path=canvas_image_path, sqrt_num_tiles=300,\n                                   tile_file_extension=tile_file_extension)\n\n        # Make the mosaic art and save it in the images/outputs directory\n        output_image = art_maker.make_mosaic_art(k=40)\n        print(f\"Created a mosaic art version of '{art_maker.original_image_path}' using \"\n              f\"{art_maker.sqrt_num_tiles * art_maker.sqrt_num_tiles} smaller images created by a Stable Diffusion model\")\n        art_maker.save_images((output_image,), path='images/outputs',\n                              prefix=f'{art_maker.original_image_name}_mosaic_art')\n\n        # Display each original image and its mosaic art version\n        art_maker.display_images((art_maker.original_image, output_image),\n                                 (art_maker.original_image_name, art_maker.original_image_name + \"_mosaic_art\"))\n```\n\nRun the main function to execute the code. If you want to remake the tile images, set the `remake_tiles` parameter\nto `True`. Note that creating a new set of tile images might take a while, so it is recommended to use the current tile\nimages in the beginning by setting `remake_tiles` to `False`.\n\n```python\nmain(remake_tiles=False)\n```\n\n#### Kaggle notebook\n\nThe notebook is available on Kaggle now. You can open it\nusing [this link](https://www.kaggle.com/code/habedi/creating-mosaic-art-using-kerascv-stablediffusion).\n\n### Ideas for improvement\n\n- [x] Adding a link to a Kaggle or Colab version of the notebook.\n\n\u003cdel\u003e- [ ] Using different shapes instead of squares (of the same size) for the tiles. For example, trapezoids or\ntriangles. \u003c/del\u003e\n\n\u003cdel\u003e- [ ] Using different tile images for different parts of the original image. For example, using a set of images\ncontaining faces for the faces in the original image, and using a set of images containing landscapes for the\nbackground in the original image. \u003c/del\u003e\n\n\u003cdel\u003e- [ ] Using better similarity metrics for comparing the tiles with a patch from the original image. For example,\nusing the [SSIM](https://en.wikipedia.org/wiki/Structural_similarity) metric instead of the Euclidean distance. \u003c/del\u003e\n\n### Credits\n\n- [KerasCV](https://keras.io/keras_cv): The code for creating the tile images uses\n  the StableDiffusion model available from KerasCV.\n- The idea for creating mosaic art versions of images using a collection of tile images is inspired\n  by [this post](https://medium.com/@aarongrove/creating-image-mosaics-with-python-8e4c25dd9bf9).\n\n### License\n\nThis project is licensed under the terms of the Apache 2.0 license. See [LICENSE](LICENSE) for more details.\nPlease note that the image files in [images/canvases](images/canvases) were downloaded from the Web and are not owned by\nthe creator of this repository. Consequently, they may not be licensed under the Apache 2.0 license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabedi%2Fmosaic-art-maker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhabedi%2Fmosaic-art-maker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhabedi%2Fmosaic-art-maker/lists"}