Open In Colab

Project 2 Notes on Converting and Cropping All Images in a Folder with Python PIL

Mounting Google Drive

  • Upload breccia.zip or move breccia.zip to Google Drive.
In [1]:
from google.colab import drive
drive.mount('/content/drive')
Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).

Uninstall PIL and install the current version.

In [2]:
import PIL
PIL.__version__
Out[2]:
'8.1.0'
In [3]:
pip uninstall PIL
WARNING: Skipping PIL as it is not installed.
In [4]:
pip install pillow==8.1.0
Requirement already satisfied: pillow==8.1.0 in /usr/local/lib/python3.6/dist-packages (8.1.0)
In [5]:
import PIL
PIL.__version__
Out[5]:
'8.1.0'

The range( ) Function

  • range(start, stop, step)
In [6]:
for x in range(7):
    print(x)
0
1
2
3
4
5
6
In [7]:
for y in range(2, 7, 2):
    print(y)
2
4
6

Functions

  • A function is a block of code which only runs when it is called.
  • Creating a function: def function_name(parameters):
  • indentation
In [8]:
def fibonacci(m):
    a, b = 0, 1
    for i in range(m):
        print(a)
        a, b = b, a+b
In [9]:
fibonacci(11)
0
1
1
2
3
5
8
13
21
34
55
In [10]:
def fi(n):
    L = [0]
    a, b = 0, 1
    for i in range(n-1):
        a, b = b, a+b
        L.append(a)
    return L
In [11]:
fi(11)
Out[11]:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Create a fibonacci.py

Modules

  • A module is a file containing a set of functions you want to include in your application.
  • A module is the same as a code library.
  • Create a file with .py.
  • import
  • from... import...
In [12]:
import os, sys, shutil
os.chdir('/content/drive/My Drive')
In [13]:
pwd
Out[13]:
'/content/drive/My Drive'
In [14]:
os.listdir()
Out[14]:
['Colab Notebooks',
 'Data',
 'breccia.zip',
 'w_2020',
 'fibonacci.py',
 '__pycache__',
 'breccia_jpg',
 'breccia_crop',
 'breccia',
 '__MACOSX']
In [15]:
import fibonacci
In [16]:
fibonacci.fibo(11)
0
1
1
2
3
5
8
13
21
34
55
In [17]:
fibonacci.fi(11)
Out[17]:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
In [18]:
from fibonacci import fi
fi(7)
Out[18]:
[0, 1, 1, 2, 3, 5, 8]
In [19]:
from fibonacci import fibo
fibo(7)
0
1
1
2
3
5
8

Working with Files in Python

  • os.chir( )
  • pwd
  • mkdir ( )
  • shutil.unpack_archive( )
  • path = 'a/b/c'
  • os.listdir( )
  • os.path.join( )
  • os.path.isfile( )
  • os.path.splitext ( )

Converting and cropping images with Python PIL

In [20]:
os.chdir('/content/drive/My Drive')
In [21]:
pwd
Out[21]:
'/content/drive/My Drive'
In [22]:
mkdir breccia_jpg
mkdir: cannot create directory ‘breccia_jpg’: File exists
In [23]:
mkdir breccia_crop
mkdir: cannot create directory ‘breccia_crop’: File exists
In [24]:
shutil.unpack_archive('breccia.zip', '/content/drive/My Drive')
In [25]:
import PIL
from PIL import Image
In [26]:
def convert():
    path1 = '/content/drive/My Drive/breccia'
    savedpath1 = '/content/drive/My Drive/breccia_jpg'
    for item in os.listdir(path1):
        fullpath1 = os.path.join(path1, item)
        if os.path.isfile(fullpath1):
            im = Image.open(fullpath1)
            im_convert = im.convert('RGB')
            fullsavedpath1 = os.path.join(savedpath1, item)
            f, e = os.path.splitext(fullsavedpath1)
            im_convert.save(f + '.jpg', 'JPEG', quality=95)
convert()
In [27]:
def crop():
    path2 = '/content/drive/My Drive/breccia'
    savedpath2 = '/content/drive/My Drive/breccia_crop'
    for item in os.listdir(path2):
        fullpath2 = os.path.join(path2,item)
        if os.path.isfile(fullpath2):
            im = Image.open(fullpath2)
            im_crop = im.crop((100,100,400,400))
            fullsavedpath2 = os.path.join(savedpath2, item)
            f, e = os.path.splitext(fullsavedpath2)
            im_crop.save(f + '_cropped.png', 'png', quality=100)
crop()