Python向图像添加额外区域 [英] Python adding extra area to image

查看:54
本文介绍了Python向图像添加额外区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个带有图像大小的表格.有多个不同大小的图像(66x66、400x400 等).我有一个图像(原始)示例,其大小始终为 600x532,在此图像上是产品(电视、PC 等).

我必须调整这张图片的大小,这不是问题.但是,如果我按比例执行此操作,则会得到类似 66x55 的结果.如果我不按比例执行此操作,则图像看起来不太好.

所以原件的背景总是白色的.有没有办法扩展图像的区域并用白色填充其余部分?所以像这样:600x532 -> 600x600 -> 66x66 等等

它应该像一个反作物.

我发现如果我使用 PIL 中的crop() 而不是最小化"使用高于实际图像大小的值,它会创建我的额外区域.但它会变黑.知道如何将这个区域填充为白色吗?

我猜它与 ImageDraw 有关系.

在发现 ImageDraw 是解决方案后,我的问题就解决了.请关闭这个.

这是我的解决方案:

导入图片,ImageDrawimg1 = Image.open("img.jpg")img2 = img1.crop((0,0,600,600))绘制 = ImageDraw.Draw(img2)draw.rectangle( (0,532,600,600), fill='white' )德尔画img2.save("img2.jpg","JPEG", quality=75)

接下来我要做的是在上面和下面做额外的裁剪.所以图片停留在中间.

最终解决方案

img1 = Image.open("img1.jpg")img2 = img1.crop( (0,-34,600,566) )绘制 = ImageDraw.Draw(img2)draw.rectangle( (0,0,600,34), fill="white" )draw.rectangle( (0,566,600,600), fill="white" )德尔画img2.save("img2.jpg", "JPEG", quality=75)

解决方案

假设我们使用PIL来处理图像

from PIL 导入图片def white_bg_square(img):返回一个白色背景颜色的图像,img 位于精确的中心"大小 = (max(img.size),)*2layer = Image.new('RGB', size, (255,255,255))layer.paste(img, tuple(map(lambda x:(x[0]-x[1])/2, zip(size, img.size))))返回层

您可以调整 PIL Image 对象的大小,例如 img

img.resize((width, height), resample=Image.ANTIALIAS)

因此在python shell中,它看起来像

<预><代码>>>>从 PIL 导入图像>>>img = Image.open('path/to/image')>>>square_one = white_bg_square(img)>>>square_one.resize((100, 100), Image.ANTIALIAS)>>>square_one.save('路径/到/结果')

PIL 文档和 sorl-thumbnail 3.2.5 里面有很好的例子

So I have a table with image sizes. There are multiple images of different sizes (66x66, 400x400, etc.). I have one example of image (the original) that always has a size of 600x532, and on this image is a product (a TV, a PC, etc.).

I have to resize this image, which isn't a problem. But if I do this with proportion I get something like 66x55. If I don't do this with proportion the image doesn't look good.

So the background of the original is always white. Is there a way to extend the area of the image and filling the rest with white? So like this: 600x532 -> 600x600 -> 66x66 etc etc.

It should be like a anti-crop.

EDIT: I found out that if I use crop() from PIL and instead of "minimizing" using a value above the actual image-size it creates my extra area. but it is going to be black. Any idea how I could fill this area white?

EDIT2: I guess it has something to do with ImageDraw.

EDIT3: After finding out that ImageDraw was the solution, my problem was solved. Please close this.

Here my solution:

import Image, ImageDraw
img1 = Image.open("img.jpg")
img2 = img1.crop((0,0,600,600))
draw = ImageDraw.Draw(img2)
draw.rectangle( (0,532,600,600), fill='white' )
del draw
img2.save("img2.jpg","JPEG", quality=75)

The next thing I will do is to make the extra crop above and under. So the picture stays in the middle.

EDIT4: final solution

img1 = Image.open("img1.jpg")
img2 = img1.crop( (0,-34,600,566) )  
draw = ImageDraw.Draw(img2)
draw.rectangle( (0,0,600,34), fill="white" )
draw.rectangle( (0,566,600,600), fill="white" )
del draw
img2.save("img2.jpg", "JPEG", quality=75)

解决方案

Supposing we use PIL to process the image

from PIL import Image

def white_bg_square(img):
    "return a white-background-color image having the img in exact center"
    size = (max(img.size),)*2
    layer = Image.new('RGB', size, (255,255,255))
    layer.paste(img, tuple(map(lambda x:(x[0]-x[1])/2, zip(size, img.size))))
    return layer

You could resize a PIL Image object, img for example

img.resize((width, height), resample=Image.ANTIALIAS)

Thus in the python shell, it looks like

>>> from PIL import Image
>>> img = Image.open('path/to/image')
>>> square_one = white_bg_square(img)
>>> square_one.resize((100, 100), Image.ANTIALIAS)
>>> square_one.save('path/to/result')

There are nice examples inside PIL document and sorl-thumbnail 3.2.5

这篇关于Python向图像添加额外区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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