Python adding extra area to image
Asked Answered
E

3

8

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)
Economic answered 9/3, 2012 at 9:31 Comment(4)
What library/class are you using to represent your images? If your image is a NumPy array, it should be no problem to create a new, all-white image and copy the orginal pixels to the new one.Anaximenes
I am using Python Image Library. But i already found out. But if there is a better solution, I would be happy to listen :)Economic
Please post the solution as an answer to your own question. Then you can close it yourself (after 24h).Ptolemaeus
ok, i will, but i only can after 8 hours.Economic
A
17

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

Anacreontic answered 9/3, 2012 at 10:12 Comment(3)
You may look at this answer too for another solution #11664538Halftruth
Nice short solution! In your lambda, you might want to add a int(); otherwise it fails sometimes.Brittaney
in case one of the sizes is not even use: layer.paste(img, tuple(map(lambda x:int((x[0]-x[1])/2), zip(size, img.size)))) in the functionFlew
E
1

My 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)
Economic answered 9/3, 2012 at 17:50 Comment(0)
R
1

If we use opencv to process the image.

import cv2
import numpy as np

def make_square(self, image_in):
   size = image_in.shape[:2]
   max_dim = max(size)
   delta_w = max_dim - size[1]
   delta_h = max_dim - size[0]
   top, bottom = delta_h//2, delta_h-(delta_h//2)
   left, right = delta_w//2, delta_w-(delta_w//2)
   color = [255, 255, 255]
   #image_out = cv2.copyMakeBorder(image_in, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
   image_out = cv2.copyMakeBorder(image_in, top, bottom, left, right, cv2.BORDER_REPLICATE, value=color)
   return image_out

image_in = cv2.imread(image_path)
Rodenhouse answered 21/4, 2019 at 17:39 Comment(1)
thanks for the great solution. I would like to add the differences between cv2.BORDER_REPLICATE and cv2.BORDER_CONSTANT. The first creates a border "replicating" the pixels. the second creates a border in the chosen color.Maramarabel

© 2022 - 2024 — McMap. All rights reserved.