有没有简单的方法来做到这一点? 在Python中,我创建了一个脚本来从图像中获取一个“方块”......基于中心。如何拍摄图像并获取从中心剪切的3x2比例图像?
但是,这导致我的一些脑细胞死亡。基于中心有没有简单的方法来做到这一点3×2(3宽度,2高度)?
这是我的“方框”脚本,但我不想为3x2修改它。
def letterbox(f,thumb_w=None,thumb_h=None):
try:
im = Image.open(StringIO(f))
imagex = int(im.size[0])
imagey = int(im.size[1])
thumb_size = (thumb_w,thumb_h) #what if it is too small!?? fix it.
if imagex > imagey:
setlen = imagey
left = (imagex - setlen)/2
top = 0
height = setlen
width = setlen
if imagey > imagex:
setlen = imagex
left = 0
top = (imagey - setlen)/2
heigth = setlen
width = setlen
if imagex == imagey:
left = 0
top = 0
height = imagey
width = imagex
box = (left,top,left+width,top+height)
im = im.crop(box)
#im.thumbnail(thumb_size,Image.ANTIALIAS)
new_file = StringIO()
im.save(new_file,'JPEG')
new_file.seek(0)
except Exception, e:
pass
return new_file
有没有脚本在线,可以做我需要的?
你为什么要压制异常?除非您确切知道发生异常的原因以及如何处理它,否则您应该让它冒泡,否则您将隐藏需要了解的错误或系统故障。 – jammycakes 2010-11-04 14:43:21
@jammycakes:不仅如此,它的写入方式并不能完全防止它引发异常,因为在函数结束时执行返回操作的'new_file'仍然可能还没有被定义。 – martineau 2010-11-08 16:43:27