使用python完成抠图,调整大小,设置背景
虽然当时也做了相关的记录,并且在第二次 完成相关的操作时,也有参照之前的记录,但是最后居然花了 两天多的时间才完成。这次的记录如下,经过几天摸索, 发现完成抠图, 的方法很多, 比如使用opencv ,但这里使用的是paddle来完成。这里抠的图也只是人像相关的。(纯python 代码,加上若干注释,第二次操作时,我是没有完成的。第二步 调整处理后图片的大小为自己需要的大小, 我这里需要的是 48
·
之前只花了几个小时就搞出来了, 也是 一时 兴起, 这一块并未涉足。虽然当时也做了相关的记录,并且在第二次 完成相关的操作时,也有参照之前的记录,但是最后居然花了 两天多的时间才完成。
以下为第一次完成后所作的操作。(纯python 代码,加上若干注释,第二次操作时,我是没有完成的。)
#抠图
import paddlehub as hub
output_path = 'output'
module = hub.Module(name="deeplabv3p_xception65_humanseg")
input_dict = {"image": photo_path}
results = module.segmentation(data=input_dict,visualization=True, output_dir=output_path)
from PIL import Image, ImageDraw
WIDTH_1IN = 295
HEIGHT_1IN = 413
#WIDTH_2IN = 413
#HEIGHT_2IN = 626
WIDTH_2IN = 480
HEIGHT_2IN = 640
WIDTH_5IN = 1500
HEIGHT_5IN = 1050
### 非全景6寸照片
WIDTH_6IN = 1950
HEIGHT_6IN = 1300
def cut_photo(photo, choice):
width = photo.size[0] # 宽
height = photo.size[1] # 高
rate = height / width
if choice == 1:
if rate < (HEIGHT_1IN / WIDTH_1IN):
x = (width - int(height / HEIGHT_1IN * WIDTH_1IN)) / 2
y = 0
cutted_photo = photo.crop((x, y, x + (int(height / HEIGHT_1IN * WIDTH_1IN)), y + height))
else:
x = 0
y = (height - int(width / WIDTH_1IN * HEIGHT_1IN)) / 2
cutted_photo = photo.crop((x, y, x + width, y + (int(width / WIDTH_1IN * HEIGHT_1IN))))
return cutted_photo
if choice == 2:
if rate < (HEIGHT_2IN / WIDTH_2IN):
x = (width - int(height / HEIGHT_2IN * WIDTH_2IN)) / 2
y = 0
cutted_photo = im.crop((x, y, x + (int(height / HEIGHT_2IN * WIDTH_2IN)), y + height))
else:
x = 0
y = (height - int(width / WIDTH_2IN * HEIGHT_2IN)) / 2
cutted_photo = im.crop((x, y, x + width, y + (int(width / WIDTH_2IN * HEIGHT_2IN))))
return cutted_photo
def resize_photo(photo, choice):
'''
缩放照片
:param photo: 待处理的照片
:param choice: <int> 1代表1寸,2代表2寸
:return: 处理后的照片
'''
if choice == 1:
resized_photo = photo.resize((WIDTH_1IN, HEIGHT_1IN))
return resized_photo
if choice == 2:
resized_photo = photo.resize((WIDTH_2IN, HEIGHT_2IN))
return resized_photo
path='C:\\Users\lws\Desktop'
import os
os.chdir("C:\\Users\lws\Desktop")
im = Image.open("lws.jpg")
width = im.size[0]
height = im.size[1]
rate = height / width
im=resize_photo(cut_photo(im, 2), 2)
im.save('lws2.png')
#抠图 ok
#换底
from PIL import Image
import os
path='C:\\Users\lws\Desktop'
os.chdir(path)
#定义证件照底色RGB值
blue = 0,191,243
red = 255,0,0
white =255,255,255
#底色默认值
color = 0,0,0
your_choice=input("你想要的证件照底色")
if your_choice =="red":
color = red
elif your_choice == "white":
color = white
elif your_choice == "blue":
color = blue
else:
color=white
print("颜色不正确,默认白")
#填充图片A通道,即透明部分
img_path=['lws2.png']
img =Image.open(img_path[0])
finish_path = img_path[0]+"_finish2.png"
x, y = img.size
p = Image.new('RGBA', img.size, (color))
p.paste(img, (0, 0, x, y), img)
p.save(finish_path)
这次的记录如下,经过几天摸索, 发现完成抠图, 的方法很多, 比如使用opencv ,但这里使用的是paddle 来完成。这里抠的图也只是人像相关的。毕竟我只是需要一张蓝底的照片。
第一步 抠图(比如抠出人像)
import paddlehub as hub
import os
output_path = 'C:/Users/lws/Desktop/output' # 设定抠图完成后存放路径
path="C:/Users/lws/Desktop/in_put/" # 设定要处理的图片目录
files= [path + i for i in os.listdir(path)] # 获取要处理的图片的绝对路径
module = hub.Module(name="deeplabv3p_xception65_humanseg") # 加载人像识别库(我是这么理解的,所以类似的库很多)
results = module.segmentation(data={'image':files},visualization=True, output_dir=output_path) # 调用人像识别对象的方法来处理图片列表,并将处理后的图片输出到指定目录
以下为完成抠图的软件环境,python版本也许不那么重要,paddlehub 1.8.0 paddlepaddle 1.8.5 必须得这两个版本。
第二步 调整处理后图片的大小为自己需要的大小, 我这里需要的是 480 X 640
代码如下
from PIL import Image, ImageDraw
WIDTH_1IN = 295
HEIGHT_1IN = 413
#WIDTH_2IN = 413
#HEIGHT_2IN = 626
WIDTH_2IN = 480
HEIGHT_2IN = 640
WIDTH_5IN = 1500
HEIGHT_5IN = 1050
### 非全景6寸照片
WIDTH_6IN = 1950
HEIGHT_6IN = 1300
def cut_photo(photo, choice):
width = photo.size[0] # 宽
height = photo.size[1] # 高
rate = height / width
if choice == 1:
if rate < (HEIGHT_1IN / WIDTH_1IN):
x = (width - int(height / HEIGHT_1IN * WIDTH_1IN)) / 2
y = 0
cutted_photo = photo.crop((x, y, x + (int(height / HEIGHT_1IN * WIDTH_1IN)), y + height))
else:
x = 0
y = (height - int(width / WIDTH_1IN * HEIGHT_1IN)) / 2
cutted_photo = photo.crop((x, y, x + width, y + (int(width / WIDTH_1IN * HEIGHT_1IN))))
return cutted_photo
if choice == 2:
if rate < (HEIGHT_2IN / WIDTH_2IN):
x = (width - int(height / HEIGHT_2IN * WIDTH_2IN)) / 2
y = 0
cutted_photo = im.crop((x, y, x + (int(height / HEIGHT_2IN * WIDTH_2IN)), y + height))
else:
x = 0
y = (height - int(width / WIDTH_2IN * HEIGHT_2IN)) / 2
cutted_photo = im.crop((x, y, x + width, y + (int(width / WIDTH_2IN * HEIGHT_2IN))))
return cutted_photo
def resize_photo(photo, choice):
'''
缩放照片
:param photo: 待处理的照片
:param choice: <int> 1代表1寸,2代表2寸
:return: 处理后的照片
'''
if choice == 1:
resized_photo = photo.resize((WIDTH_1IN, HEIGHT_1IN))
return resized_photo
if choice == 2:
resized_photo = photo.resize((WIDTH_2IN, HEIGHT_2IN))
return resized_photo
path='C:\\Users\lws\Desktop\output'
import os
os.chdir("C:\\Users\lws\Desktop\output")
im = Image.open("lws2.png")
width = im.size[0]
height = im.size[1]
rate = height / width
im=resize_photo(cut_photo(im, 2), 2)
im.save('lws23.png')
#抠图 ok
#需要安装 Pillow 包
第三步 将调整后的图片 和设置的 背景粘贴在一起,
代码如下
from PIL import Image
import os
path='C:\\Users\lws\Desktop\output'
os.chdir(path)
#定义证件照底色RGB值
blue = 0,191,243
red = 255,0,0
white =255,255,255
#底色默认值
color = 0,0,0
your_choice=input("你想要的证件照底色")
if your_choice =="red":
color = red
elif your_choice == "white":
color = white
elif your_choice == "blue":
color = blue
else:
color=white
print("颜色不正确,默认白")
#填充图片A通道,即透明部分
img_path=['lws23.png'] # 抠出来的图像,并已经调整了大小
img =Image.open(img_path[0]) # 打开这个图片对象
finish_path = img_path[0]+"_finish2.png"
x, y = img.size
p = Image.new('RGBA', img.size, (color)) # 创建新的特定大小背景,
p.paste(img, (0, 0, x, y), img) # 使用 新创建的 背景对象,完成粘贴, 这里为什么 两次用img 对象 我也不太明白。
p.save(finish_path) # 调用图片对象的保存方法。
更多推荐
所有评论(0)