在Python的Python图片库1.1.6中,如何在不调整大小的情况下扩展画布? [英] In Python, Python Image Library 1.1.6, how can I expand the canvas without resizing?

查看:108
本文介绍了在Python的Python图片库1.1.6中,如何在不调整大小的情况下扩展画布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能正在寻找手册中的错误内容,但是我正在寻找一个图像对象并对其进行扩展,而无需调整原始图像的大小(拉伸/压缩).

I am probably looking for the wrong thing in the handbook, but I am looking to take an image object and expand it without resizing (stretching/squishing) the original image.

玩具示例:想象一个200 x 100的蓝色矩形,然后执行一些操作,然后有一个新的图像对象400 x 300,该图像对象由一个白色背景组成,上面有一个200 x 100的蓝色矩形.如果我可以控制扩展的方向或新的背景颜色等,则可以得到奖励.

Toy example: imagine a blue rectangle, 200 x 100, then I perform some operation and I have a new image object, 400 x 300, consisting of a white background upon which a 200 x 100 blue rectangle rests. Bonus if I can control in which direction this expands, or the new background color, etc.

从本质上来说,我有一个要迭代添加的图像,但我不知道图像一开始的大小.

Essentially, I have an image to which I will be adding iteratively, and I do not know what size it will be at the outset.

我想我可能会抓住原始对象,制作一个新的,稍大的对象,将原始对象粘贴在该对象上,再画一点,然后重复.看来它在计算上可能会很昂贵.但是,我认为这将是一个功能,因为我认为这是一个常见的操作.也许我认为错了.

I suppose it would be possible for me to grab the original object, make a new, slightly larger object, paste the original on there, draw a little more, then repeat. It seems like it might be computationally expensive. However, I thought there would be a function for this, as I assume it is a common operation. Perhaps I assumed wrong.

推荐答案

ImageOps.expand函数将扩展图像,但在每个方向上添加相同数量的像素.

The ImageOps.expand function will expand the image, but it adds the same amount of pixels in each direction.

最好的方法是简单地制作新图像并粘贴:

The best way is simply to make a new image and paste:

newImage = Image.new(mode, (newWidth,newHeight))
newImage.paste(srcImage, (x1,y1,x1+oldWidth,y1+oldHeight))

如果性能是一个问题,请使原始图像超出所需的大小,并在绘制完成后对其进行裁剪.

If performance is an issue, make your original image bigger than needed and crop it after the drawing is done.

这篇关于在Python的Python图片库1.1.6中,如何在不调整大小的情况下扩展画布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆