利用python将一张图片导成横板大图和竖版大图
#!/usr/bin/env python# -*- coding:utf-8 -*-# Created by victor# 本模块的功能:<更改图片尺寸>import redisimport osimport os.pathfrom PIL import Image,ImageFilterimport urllib2import cStringIOclass MyGaussianB
·
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Created by victor
# 本模块的功能:<更改图片尺寸>
import os
import os.path
from PIL import Image,ImageFilter
import urllib2
import cStringIO
class MyGaussianBlur(ImageFilter.Filter):
name = "GaussianBlur"
def __init__(self, radius=2, bounds=None):
self.radius = radius
self.bounds = bounds
def filter(self, image):
if self.bounds:
clips = image.crop(self.bounds).gaussian_blur(self.radius)
image.paste(clips, self.bounds)
return image
else:
return image.gaussian_blur(self.radius)
#图片高斯模糊
def ResizeImageiGaosi(filein, width, height):
img = Image.open(filein)
image = img.filter(MyGaussianBlur(radius=30))
out = image.resize((width, height),Image.ANTIALIAS)
return out
#改变图片尺寸
def ResizeImage(filein, width, height):
img = Image.open(filein)
out = img.resize((width, height),Image.ANTIALIAS)
return out
#将url读取
def ImageScale(url):
file = cStringIO.StringIO(urllib2.urlopen(url).read())
return file
#合并图片
def blend_weight_images(img1,img2,fileout):
img2.paste(img1,(0,280))
img2.save(fileout)
#合并图片
def blend_height_images(img1,img2,fileout):
img2.paste(img1,(280,0))
img2.save(fileout)
if __name__ == "__main__":
filein = ImageScale("http://p6.itc.cn/q_70/images03/20201104/f21cdc7317ac49fbbe3764d6dad4db4a.jpeg")
fileouth = str(ids)+'h.jpg'
fileoutw = str(ids)+'w.jpg'
#基础图片
basefile = ResizeImage(filein, 720, 720)
#竖版高斯模糊图
filegaosih = ResizeImageiGaosi(filein, 720, 1280)
#横板高斯模糊图
filegaosiw = ResizeImageiGaosi(filein, 1280, 720)
#生成竖版大图
blend_weight_images(basefile,filegaosih,fileouth)
#生成横板大图
blend_height_images(basefile,filegaosiw,fileoutw)
更多推荐
已为社区贡献3条内容
所有评论(0)