Some time ago, I saw the following picture made by the great god in Ruby on Zhihu. I thought it was very interesting, so I planned to use python to implement it.
Python has an image processing standard library PIL (Python Imaging Library), which is powerful and easy to use. Unfortunately, only Support 2.x, Pillow is a compatible version created on the basis of PIL, supports python 3.x, and adds some new features.
Install Pillow
It is very convenient to use pip to install directly on the command line
pip install Pillow
Manipulate images
First, introduce the corresponding packages as needed (Image, ImageColor, ImageFont, ImageFilter, etc.)
from PIL import Image
open image
image = Image.open(url)
Get the image size (returns a tuple, width and height respectively)
width, height = image.size
save Picture
image.save(url)
display image
image.show()
new image
newImage = Image.new(mode, (width, height), color)
Crop image
cropedImage = image.crop((0, 0, 100, 100))
Duplicate image (makes a copy of the original image, operations on it do not affect the original image)
copyImage = image.copy()
Paste (direct modification to the called image object)
copyImage.paste(image, (x, y))
adjust size
resizedIm = image.resize((width, height))
Rotate (counterclockwise, return the rotated image, the original image remains unchanged)
rotateIm = image.rotate(90)
flip
image.transpose(Image.FLIP_TOP_LEFT_RIGTH) #水平翻转
image.transpose(Image.FLIP_TOP_TOP_BOTTOM) #垂直翻转
get pixel value
image.getpixel((x,y))
Pixel
image.putpixel((x,y),(r,g,b))
get alpha
image.getalpha((x,y))
write alpha
image.putalpha((x,y), alpha)
Image composite
1. Conform img2 to img1 and return Image image (requires the same size, and the cut mode must be RGBA)
img3 = Image.alpha_composite(img1, img2)
2、out = img1*(1-alpha)+img2*alpha
img3 = Image.blend(img1, img2, alpha)
3、
img3 = Image.composite(img1, img2, mask)
Image filtering
requires the introduction of the ImageFilter package
from PIL import Image, ImageFilter
Gaussian blur
im.filter(ImageFilter.GaussianBlur)
normal blur
im.filter(ImageFilter.BLUR)
edge enhancement
im.filter(ImageFilter.EDGE_ENHANCE)
find the edge
im.filter(ImageFilter.FIND_EDGES)
relief
im.filter(ImageFilter.EMBOSS)
contour
im.filter(ImageFilter.CONTOUR)
sharpen
im.filter(ImageFilter.SHARPEN)
smooth
im.filter(ImageFilter.SMOOTH)
detail
im.filter(ImageFilter.DETAIL)
Demo
# -*- coding: utf-8 -*-
"""
将n*n张图片拼接在一起,并覆盖上一张降低透明度的图片
Copyright: 2018/01/27 by 邵明山
"""
import os
from PIL import Image
def makedir(path):
if not os.path.exists(path):
os.mkdir(path)
print("The directory \"" + path + "\" creates success.")
else:
print("The directory \"" + path + "\" has already existed.")
base_path = "base"
mask_path = "mask"
makedir(base_path)
makedir(mask_path)
n = int(input("Please enter n, n^2 is the number of the pictures."))
print("Please put n^2 pictures to the forder \"base\".")
print("Please put one picture to the forder \"mask\".")
os.system("pause")
width = int(input("Please enter the width of the final picture."))
height = int(input("Please enter the height of the final picture."))
transparency = int(input("Please enter the transparency% of the mask."))
base_pictures = []
for root, dirs, files in os.walk(os.getcwd()+"\\"+base_path):
for file in files:
base_pictures.append(os.path.join(root, file))
num = 0
base = Image.new("RGBA", (width, height))
for i in range(0, n):
for j in range(0, n):
picture = Image.open(base_pictures[num])
temp = picture.resize((int(width/n), int(height/n)))
location = (int(i * width / n), int(j * height / n))
base.paste(temp, location)
num += 1
if num >= len(base_pictures):
break
if num >= n*n:
break
for root, files in os.walk(os.getcwd()+"\\"+mask_path):
for file in files:
mask_picture = Image.open(mask_path + "\\" + file)
mask = mask_picture.resize((width, height))
mask.putalpha(int(transparency/100 * 255))
result = Image.alpha_composite(base, mask)
result.save("result.png")