Overwriting a border on an image in Python PIL

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Overwriting a border on an image in Python PIL



I have a gallery application where the users upload photos and my code gives it a border, writes some of the photo attributes on the border and stores it.


image2 = Image.open('media/' + str(image.file))
width, height = image2.size;
image2 = ImageOps.expand(image2, border=(int(width/25),int(height/20),int(width/25),int(height/10)), fill='rgb(0,0,0)')



(Note that here my bottom border is longer than the top because I am writing attributes on the bottom border.)
Now I'm building an edit feature for the uploaded images where the user can change the attributes of the uploaded images. But the attributes that are already written on the border have to be overwritten.



So here, my approach is to put a black patch on the bottom border and re-write the new attributes without changes the top and side borders and without changing the aspect ratio. All of this has to be done using PIL.



Question is how do I put a black box on the bottom border?



I tried ImageOps.fit() as mentioned here https://pillow.readthedocs.io/en/3.3.x/reference/ImageOps.html#PIL.ImageOps.fit, but the aspect ratio doesn't seem to be right and I want to overwrite on the black border a black box and not crop the photo.





I recommend you to always keep original image in your DB in a separate field say original and when you are creating a new image with the border, save it in a different column say edited. so this way, when someone request with new attributes, use original image and edit it and update new image to edited column
– Satendra
21 hours ago




original


edited


original


edited





Yeah, should have done that.
– Keerthan Bhat
21 hours ago





Apart from this, what else can I do now?
– Keerthan Bhat
21 hours ago




2 Answers
2



The simplest way in my opinion is to create a new black image and paste onto your existing image -


from PIL import Image
im = Image.open('test.png')
blackBox = Image.new(im.mode, (im.width, 50), '#000')
im.paste(blackBox, (0, im.height - blackBox.height))



Alternatively, you could use ImageDraw - http://pillow.readthedocs.io/en/5.2.x/reference/ImageDraw.html - which you could use to draw rectangles and other shapes.


from PIL import Image, ImageDraw
im = Image.open('test.png')
d = ImageDraw.Draw(im)
d.rectangle((0, im.height - 50, im.width, im.height), fill='#000')



To me it seems like the easiest solution is just quickly draw the black pixels in the area that you want using a couple loops and Image.putpixel


Image.putpixel



enter image description here


from PIL import Image

img = Image.open('red.png')

for x in range(img.width):
for y in range(img.height - 40, img.height):
img.putpixel((x, y), (0, 0, 0))

img.save('red2.png')



enter image description here






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Dv 3hKf9qnBeYRYV,wvgd,dIT2Wstdz6Su6tvaFg,0
Fb89H,XkZfFWaBk WGCl22T,W,1ElYfoBSEx1PDLye5Dbto1g85HK9eIYNonPKMi,eoYwBZylINkzbqTtqM

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results