[Bradford] Tuesday Bradlug

Bernard Czenkusz bernie.skipole at gmail.com
Sun Jul 12 20:22:38 UTC 2020


Hi guys, unless anyone else has anything, can I offer to do a demo of 
Python image manipulation capabilities?

Normally this would be best with a projector, and might not show up well 
over the video conferencing system, so I've included below the Python 
commands I'll be using if people want to try it during the demo with 
their own image.

Before the demo -

Check you have python3 with the module 'venv' available: venv allows you 
to create a 'virtual' environment so you can load Python modules without 
messing up your machine - you can delete the environment afterwards and 
the modules loaded are deleted with it.

To check - type "python3" which will open the Python interactive command 
session
then at the >>> prompt, type
import venv

if you just get a new prompt, you have venv available, leave the session 
with ctrl-D
If you get an error, you do not have venv available, leave the session 
with ctrl-D and check
if your distribution allows you to install the python3 venv

on debian this would be
apt-get install python3-venv

Then test again as above.

In a terminal, create a directory ~/bradlugdemo
copy a jpeg image of your choice into this directory and rename it 
original.jpg
we are going to manipulate the image, so any fairly detailed image would 
be useful. Having done that you're ready for the demo on Tuesday night.

## the demo #########

Here are the things I will be doing if you would like to follow along by 
copying and pasting. Open a terminal.

change into the directory ~/bradlugdemo

create a python environment with
python3 -m venv tempenv

activate the python environment with
source ~/bradlugdemo/tempenv/bin/activate

with the environment activated, you can now run python3, and load 
modules into it from
the Python package library without polluting your own system, everything 
will only be
loaded into this temporary 'environment'

note - at the end of this demo, just typing 'deactivate' will deactivate 
the environment, and
deleting ~/bradlugdemo/tempenv will remove the environment and contents

so with the environment activated, and still working in the 
~/bradlugdemo directory, type

pip install Pillow

This will load the Python Image Library from Pypi (Pillow is a fork of 
the original PIL)

Now type "python3" which will open the Python interactive command session
and we will copy and paste into the >>> prompt each of the following 
commands in
turn and view the corresponding images created.


from PIL import Image, ImageFilter, ImageEnhance, ImageOps

original = Image.open("original.jpg")

newimage = original.filter(ImageFilter.BLUR)

newimage.save("copy1.jpg")

original.filter(ImageFilter.CONTOUR).save("copy2.jpg")

original.filter(ImageFilter.EMBOSS).save("copy3.jpg")

ImageEnhance.Contrast(original).enhance(1.5).save("copy4.jpg")

ImageEnhance.Sharpness(original).enhance(2.0).save("copy5.jpg")

ImageOps.autocontrast(original, cutoff=10).save("copy6.jpg")

ImageOps.autocontrast(original, cutoff=20).save("copy7.jpg")

greypic = ImageOps.grayscale(original)

greypic.save("copy8.jpg")

ImageOps.colorize(greypic, black=(30,150,20), 
white=(240,10,99)).save("copy9.jpg")


Now leave the python session with ctrl-D, and at your terminal prompt type

deactivate

The environment and the Pillow package can now be removed by deleting 
~/bradlugdemo/tempenv

Hope this is of interest.

CheersĀ  - Bernard




More information about the Bradford mailing list