TeCoEd (Teaching Computing Education)
  • Home
    • Freelance
    • Book
    • Downloading
  • Python
    • Learn Python >
      • Python Modules
    • PyGame Zero
    • Python Programs >
      • Higher or Lower
      • Magic Calculator
      • Password Checker
      • Python Pit
    • What's News App
    • Pixels to Cells
    • Python Mosaics
    • Python OCR
    • L-1-AM
    • Web Scraping >
      • Scraping Trains
    • Weather App
    • Snakes and Windows
    • Python Web Server >
      • Flask
    • Python Picks
  • Raspberry Pi
    • All About the Pi
    • Getting Started
    • Remote Desktop and VNC
    • Static IP Address
    • Sonic Pi >
      • 3.14
    • Twitter Feed >
      • Tweepy
    • Android & Pi >
      • Advanced Apps
      • Odds
    • A.I on the the Pi
    • CRON
    • Pick Your Own
  • Pi Hardware
    • Pi HATS >
      • Sense Hat Hacks
      • AstroPi HAT
      • Unicorn-HAT >
        • Unicorn Alphabet Disco
        • Uni Codes / Programs
      • Skywriter
      • Piano HAT
    • STS Pi
    • Pi Camera >
      • Pi-Cam, Python & Email >
        • Time Lapse
      • Pi Noir
    • Pipsta >
      • Flask, Input & Printers
    • Raspberry Pi Power >
      • Energenie IR power
    • Pibrella
    • Distance Sensor
    • LCD Screen
    • Pi-Tooth
    • Robot Arm
    • PiGlow
    • PiFM
    • Accelerometer
    • PiFace >
      • Installing PiFace >
        • Python Commands
  • Pi-Hacks
    • Drone Hacks
    • Pi Glue Gun Hack
    • Blinkt!
    • Sonic Pixels
    • R2D2
    • Get to the chopper
    • Astro Bird
    • Twitter Translator
    • Hacking a Robot
    • Nature_Box >
      • Best Nature Photos
    • Wearable Tech >
      • Project New York
      • P.N.Y Part 2 Health
      • P.N.Y Part 3 Games
      • P.N.Y Part 4 Translation
    • Dino-Tweet
    • Other Links
  • Pi-Hacks 2
    • The Joker
    • Hologram Machine
    • Google Vision: Camera Tell
    • Yoda Tweets
    • Pi Phone
    • Darth Beats
    • Twitter Keyword Finder
    • Crimbo Lights Hack
    • Xmas Elf
    • Halloween 2016
    • Halloween Hack 2015
    • Socrative Zombie
    • Voice Translation
    • The Blue-Who Finder
    • GPIO, Twitter
    • Pi Chat Bot >
      • Dictionary Definitions
    • PiGlow & Email
    • Pibrella Alarm System
    • SMS with Python >
      • Spooking a Mobile
  • Pi-Hacks 3
    • David Bowie
    • Lamp Prank >
      • TEST
    • Yoda FM
    • Retro Player
    • LED Pixel Art
    • TARDIS
    • Battleships
    • LED Board
    • Night Vision
    • Enviro+ Weather
  • Minecraft
    • Minecraft API
    • Minecraft Sweeper
    • PiGlove: Minecraft Power Up
    • Minecraft Photo-booth
    • Rendering Pixels
    • Speed Cube
    • Lucky Dip
  • Computing
    • Why Computing?
    • Can You Compute
    • micro:bit
    • Coding Resources
    • Learn to Code >
      • Coding with iPads
      • Apps Creation Tools
      • sKratchInn
      • Sound Editing
    • Cheat Sheets
    • Theory
    • HOUR OF CODING
    • BEBRAS Computing Challange
    • Computer Facts
    • Free Software and Links
  • Contact Me

What is it?


A mosaic is defined as, a picture or decoration made of small, usually colored pieces of inlaid stone, glass.  Python can be used to create mosaics, not from glass or tile but from your images and photographs.  A single picture created from and consisting of other tiny images.  It sounds like it should be complex but, Python makes it easy with only nine lines of code!

1. Installing the Software


Installation is simple and requires a few Python Libraries.  Before you get started make sure that your Raspberry Pi is up to date.  

In the LX Terminal type:
sudo apt-get update
sudo apt-get upgrade


then type:
sudo apt-get install python-pip python-dev
sudo pip install -U pip
sudo apt-get install python-imaging

sudo pip install Pymos


2. Python Program


The Python code for creating the mosaics is also very simple and basically asks you for the image you wish to make into a mosaic, the name of the outputted file and the folder where all the images are stored and then the size and fuzz of the final image.  The real skill is in choosing the most appropriate size, zoom and fuzz to make the final image look professional and like a mosaic!  Open a new Python window, use the code below:
from pymos.core import build_mosaic

build_mosaic(

input_path="input_image_path.jpg",
output_path="mosaic_output_path.png",
collection_path="./path_to_folder_of_small_photos/",
zoom=4,
thumb_size=50,
fuzz=20,
new_colormap=False
)


It really is this simple.  Ensure that the program and the images files are stored in the same folder and then you are ready to create.
Ferran's awesome Mosic hack is here

Creating a Mosaic



3. Perfecting the Look


To create the perfect mosaic you will want to play around with the various settings in the code.  The original quality of the images you are using and the colour variations required will all impact on the what the final image looks like.  The first three elements set up which photo is the base of the mosaic, what the final image file will be called an also where the 'tile' images are stored.
  1. input_path: The path and name of the base image that you are going to convert into a mosaic.
  2. output_path: The location where the final completed image will be outputted and saved to.
  3. collection_path: The folder where all the 'mini' or tile images are stored, these are the images which will be used to create the mosaic tiles.  

The elements below can be altered and refined to make the final image look better.  Using more tiles will make the image look better but each individual tile will lack detail.  
  1. zoom: This refers to how much bigger you want the final image to be, for example a zoom of 4 would create a final image four times the size of the original input file size.
  2. thumb_size: Refers to the size of each tile, the smaller the more “realistic” your overall mosaic will look, but each individual tile will lack detail.
  3. fuzz: The larger the fuzz, the more variation you’ll have in your mosaic.  The higher the number the more variety of tiles will be used, but the more lower the colour match may be.
  4. new_colormap: Set this as true if you add or delete  photos from the folder where the tile images are stored, the collection path. 
Copyright 2021 TeCoEd @dan_aldred