Skip to content

Picture in picture

In this short tutorial you will see how you can save binary data, images, in document NoSQL database (CouchDB).

Table of contents:

Problem description

The task is to create an application that generates images consisting of smaller images (mosaics) -- compare, for example, Top Three DON'Ts of Photo Mosaic Design


  1. The images used to generate the resulting image (let's call them thumbnails) should be stored in the CouchDB database (hereinafter referred to as the database).
  2. There should be at least one view in the database (view, see: Working example, 1.2 : CouchDB basics - querying ) used to search for a matching thumbnail (we do not download all data from the database and do not search them locally).
  3. The source image can be a rectangle (we do not assume it is a square).
  4. We do not assume in the application any requirements related to the dimensions of the source image (they can be any, of course within reason).
  5. The application should have a web interface (communication with the application via a web browser).
  6. Using the web interface you should be able to:
    1. add one image to the database;
    2. add multiple images in the ZIP archive (optional functionality to grade 5.0);
    3. select the source image that will be used to create the mosaic.


Python and images - short introduction

  1. Run Anaconda Navigator with command:

    and install pillow. Note: remember to do this for a correct environment.

  2. When pillow is installed activate your environment (test_dev in our case)

  3. Create directory (folder) for our image tests

  4. Download some images. You can use curl and download eight images fotka01.png, ..., fotka08.png from

    Repeat this for the next eight images.

  5. Create script file located in ~/Pulpit/code/images directory

  6. Put the following Python code in it
    NOTE: The first line in the following source code points to Python we will use and is system/user dependent - it is highly probable that you should use different path according to your settings.:

  7. Test script in terminal

CouchDB as images store - short inroduction

  1. Create database for our images
  2. Verify in Fauxton -- open web browser and use the following URL to connect with Fauxton
  3. Add user (user:password: images_db_user:pass)
  4. Verify in Fauxton if this user exists
  5. Add images_db_user to the members of images database. It can be easily done with Fauxton.

    Now only images_db_user user should hav an acess to images database
  6. Create (add) a document

    Verify in Fauxton
  7. Add an attachment
    We want to attach a file named fotka04.png, to the previously created document with id test, in the database named images. Before that, we have to fetch the data of the document with id test to get its current rev id _rev as shown below.

    Having current revision id (1-332ff5a5a0c81b2734acfe094ced3c22) we can upload an image

    We can verify this also in Fauxton

    Attachment can be easily add also with Fauxton
  8. Get image from CouchDB