Working with images

Published

April 8, 2025

There is a great library for manipulating images with Python called Pillow, which is basically THE way to manipulate image. However, it is a little heavyweight for our first forays into working with images. So, we are providing you with a module called middimage which hides most of the messy details and gives you a more familiar interface.

Installing midd-media

Before you can use middimage, you will need to install the midd-media package if you have not done so already (which you will have done if you used middsound).

In Thonny, select the “Manage Packages” item from the “Tools” menu. You will see the Package Manager

Thonny Package Manager

In the search bar, type midd-media. It should appear in the search results. Click on it.

Search results showing midd-media

You should now see the installer page. Click install.

If you have previously installed it, this page will allow you to Upgrade it or Uninstall it.

Install page

You can now close the package manager window.

Using middimage

To use middimage, you will need to import it into you Python file. We will use a syntax that we haven’t seen as much in class.

from midd_media import middimage

middimage functions and properties

There are a couple of functions available directly from the middimage module.

middimage.new(width, height, mode)
This returns a new empty image with the dimensions (widthxheight). The mode parameter is optional. The modes we will be using are “L” (greyscale) and “RGB” (RGB color, which is the default).
middimage.open(filename)
This returns an image object loaded with the contents of the specified file.filename should be a string that specifies the path relative to the directory where your Python script is. Since middimage uses Pillow, it can handle almost any image file type.

Image methods

When you usemiddimage.new() or middimage.open(filename), the return value is an Image, which you can consider to be a type like string or list. The Image is stored as a multi-dimensional list of pixels. We can use square bracket notation to access the individual pixels. The format will be img[c][r], where c refers to the column of the pixel and r refers to the row of the pixel. The value found there will either be a tuple (for three channel/RGB images) or a single value (for 1 channel/greyscale images).

There is also a collection of functions and properties we can access using dot notation (there is nothing special about the name img, it is just a place holder for whatever you called the variable holding your sound):

img.width
The number of columns in the image. Note that this is a variable, not a function, so do not include the ()
img.height
The number of rows in the image. Note that this is a variable, not a function, so do not include the ()
img.mode
The color mode of the image. Note that this is a variable, not a function, so do not include the ()
img.show()
Display the image.
img.save(filename)
Save the image as a file in the same directory as the Python file you are working on. The filename should be a string, and should end with a valid image type suffix (i.e., ‘.png’ or ‘.jpg’)
img.copy()
Returns a new image that is a duplicate of img