Python Exercise Questions: Application of PiL Image Processing Dependency Library

Keywords: Python

The exercises included in this paper are mainly PIL dependency libraries, i.e. pillow-related applications.

 

Exercise 1: Use python to add numbers to pictures

Ideas for realization:

  1. Import images using PIL's Image.open.
  2. Get the size of the picture.
  3. Call ImageDraw and write numbers at the specified location of the image.
#coding=utf-8
#Auther by Alice
#Add a number to the top right corner of the picture

from PIL import Image,ImageFont,ImageDraw
image = Image.open('/Users/alice/Documents/Photo/IMG_8379.JPG')
#Open original

wight, hight = image.size
text = "015"
color = (255,255,0)
fontsize = wight//10
font = ImageFont.truetype('Apple Symbols',fontsize)
#Set the parameters of the added number, the number content, the number color and the number

draw = ImageDraw.Draw(image)
draw.text((fontsize*6,0), text, color, font)
image.save('/Users/alice/Documents/Photo/IMG_7997.JPG', 'jpeg')
#Save the image after adding the number

Before implementation:

After implementation:

After modifying the font and color of two lines of code as follows,

color = (105,200,45)
font = ImageFont.truetype('Palatino.ttc',fontsize)

The results of the operation are as follows:

 

  • Exercise 2: Use python to zoom in and out an image

Ideas for realization:

  1. Use PIL, the Python image standard dependency library.
  2. Open the local image with open.
  3. Use image.thumbnail to zoom in and out
#coding by alice
#coding=utf-8

from PIL import Image

im = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# Open the specified jpg image file in a path
w,h = im.size
# Obtain image size
im.thumbnail((w//10, h//10))
# Zoom to 10%
im.save('/Users/alice/Documents/Develop/PythonCode/test2.JPG', 'jpeg')
# Save the scaled image in jpeg format:

Equivalent to code:

#coding by alice
#coding=utf-8

from PIL import Image

image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# Open the specified jpg image file in a path
wight,hight = image.size
# Obtain image size
image.thumbnail((w//10, h//10))
# Zoom to 10%
image.save('/Users/alice/Documents/Develop/PythonCode/test2.JPG', 'jpeg')
# Save the scaled image in jpg format:

The results after operation are as follows:

 

  • Exercise 3: Using python to blur a picture

Ideas for realization:

  1. Use PIL, the Python image standard dependency library.
  2. Open the local image with open.
  3. Use image.thumbnail to zoom in and out
#coding by alice
#coding=utf-8

from PIL import Image
from PIL import ImageFilter

image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
# Open a jpg image file in a path
image = image.filter(ImageFilter.BLUR)
# Application of Fuzzy Filter
image.save('/Users/alice/Documents/Develop/PythonCode/test3.JPG', 'jpeg')
#Save pictures

The results after operation are as follows:

If it is still life or face, the blurring effect will be more obvious after enlargement.

 

  • Exercise 4: Use python to get the coordinates of elements in a picture

Ideas for realization:

  1. Use PIL, the Python image standard dependency library.
  2. Open the local image with open.
  3. Using imshow to display images
  4. Get the picture by clicking on the cursor, output coordinates
#coding by alice
#coding=utf-8

from PIL import Image
import matplotlib.pyplot as plt

image = Image.open('/Users/alice/Documents/Develop/PythonCode/test.JPG')
#Open the location and the name of the image
plt.figure('image')
#Image window name
plt.imshow(image)
plt.show()

 

Posted by Randomizer on Sat, 13 Apr 2019 21:27:33 -0700