It happens that you go to the post office and see another collection of articles that you never read, but here the stars converged so much that I opened the article and read it, and figured out where it would be useful in practice. And now I want to share an article with Habr, so that if someone needs a simple and understandable guide to working with images in Python, please.
Pillow is a free open source imaging library ( hereinafter Imaging Library ) in Python that adds support for opening, modifying and saving images in various extensions to your code.
let's start
The most important class in the Imaging Library Python is the Image class, defined in the module of the same name. We use open () to open an image in our local directory as shown below:
from PIL import Image
sample = Image.open('pena.jpg')
It's simple! Now you can read images using Pillow , which means you can start processing the image using it. You can also check the type of image we just uploaded.
type(sample) PIL.JpegImagePlugin.JpegImageFile
You can see the properties of the image, for example:
- format
- the size
- color mode
sample.format
'JPEG'
sample.size
(640, 640)
sample.mode
'RGB'
, , show
sample.show() # , PNG
Pillow , , , jpg png .
, Python , jpg, png.
import os
import sys
from PIL import Image
jpg_images = [image for image in os.listdir() if image.endswith('.jpg')]
for jpg_image in jpg_images:
try:
new_name = jpg_image.split('.')[0] + '.png'
Image.open(jpg_image).save(new_name)
except IOError as error:
print('Couldn\'t read {} '.format(jpg_image))
, , , jpg, .png, . .
Pillow , , , .
from PIL import Image
picture = Image.open('pena.png')
cord = (10, 10, 640, 340) # , , ,
new_picture = picture.crop(cord)
new_picture.show()
, . .
(x, y) , (x2, y2) .
Pillow , .
.
from PIL import Image
image = Image.open('pena.png')
resized_image = image.resize((320, 320))
resized_image.save('resized.png')
, 320 320.
from PIL import Image
image = Image.open('pena.png')
rotated_img = image.rotate(80)
rotated_img.save('./rotation/rotated_img.png')
360 — , .
from PIL import Image
images = ['pena.jpg']
for img in images:
try:
org_img = Image.open(img)
for angle in range(1, 361):
image_name = str(angle)+'.jpg'
new_img = org_img.rotate(angle)
new_img.save('./rotation/'+image_name)
except IOError:
print('Couldn\'t read {}'.format(img))
, 360 , .
— . , , .
, , ,- , , .
Pillow , BLUR, BoxBlur, CONTOUR, FIND_EDGES, Filter, GaussianBlur, Kernel, MaxFilter, MedianFilter, SHARPEN, SMOOTH ..
, FIND_EDGES.
from PIL import Image
from PIL import Image, ImageFilter
image = Image.open('pena.jpg')
edges = image.filter(ImageFilter.FIND_EDGES)
edges.show()
Python Pillow , .
, Pillow Python,
from PIL import Image
image = Image.open(open('pena.jpg', 'rb'))
URL
Pillow . GET-request , , Pillow .
import requests
from PIL import Image
url = 'http://pena.marketing/images/Logo1.png'
raw = requests.get(url, stream=True).raw
Image.open(raw).show()
Pillow , . Image.new() .
:
new = Image.new(mode, shape, color)
:
from PIL import Image
new_img = Image.new('RGB', (500, 500), 'blue')
new_img.show()
Pillow . . , .
.
from PIL import Image, ImageDraw
new_img = Image.new('RGB', (400, 400), 'black')
pencil = ImageDraw.Draw(new_img)
pencil.rectangle((200, 50, 300, 300), fill ='green')
new_img.show()
(x, y) , (x2, y2) .
Pillow .
from PIL import Image , ImageDraw, ImageFont
new_img = Image.new('RGB', (200, 200), 'black')
font = ImageFont.load_default()
pencil = ImageDraw.Draw(new_img)
pencil.text((100,100),'Hello World', font=font, fill='blue', size = 36)
new_img.show()
:
- Pillow
- Jupyter Notebook