python产生方形图片的tip
2010-08-20 13:41 ubunoon 阅读(585) 评论(0) 编辑 收藏 举报
代码来自网友Jimmy Kuu和方石:
import Image
image = Image.open('01.jpg')
image.thumbnail((48, 48), Image.ANTIALIAS)
w, h = image.size
if w == 48 and h == 48:
image.save('thumb.jpg')
else:
image2 = Image.new(image.mode, (48, 48), (255, 255, 255))
if w < 48:
box = ((48 - w) / 2, 0, 24 + w / 2, 48)
else:
box = (0, (48 - h) / 2, 48, 24 + h / 2)
image2.paste(image, box)
image2.save('thumb.jpg')
这个更好一点
def get_Square_photo(imgname, outimgname, width):
"""
@param imgname:
@param outimgname:
@param width:
"""
img = Image.open(imgname, "r")
if img.size[0] > img.size[1]:
offset = int(img.size[0] - img.size[1]) / 2
img = img.transform((img.size[1], img.size[1]), EXTENT, (offset, 0, int(img.size[0] - offset), img.size[1]))
else:
offset = int(img.size[1] - img.size[0]) / 2
img = img.transform((img.size[0], img.size[0]), EXTENT, (0, offset, img.size[0], (img.size[1] - offset)))
img.thumbnail((width, width))
img.save(outimgname, 'png')
/*
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */
*
* Copyright (c) 2011 Ubunoon.
* All rights reserved.
*
* email: netubu#gmail.com replace '#' to '@'
* http://www.cnblogs.com/ubunoon
* 欢迎来邮件定制各类验证码识别,条码识别,图像处理等软件
* 推荐不错的珍珠饰品,欢迎订购 * 宜臣珍珠(淡水好珍珠) */