Specify image filling color when rotating in python with PIL and setting expand argument to true
This video explains
Specify image filling color when rotating in python with PIL and setting expand argument to true
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Question
00:49 Accepted answer (Score 35)
01:19 Answer 2 (Score 10)
01:39 Answer 3 (Score 0)
02:29 Thank you
--
Full question
https://stackoverflow.com/questions/5252...
Answer 1 links:
[https://pillow.readthedocs.io/en/stable/...]: https://pillow.readthedocs.io/en/stable/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #image #colors #pythonimaginglibrary #rotation
#avk47
Specify image filling color when rotating in python with PIL and setting expand argument to true
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping
--
Chapters
00:00 Question
00:49 Accepted answer (Score 35)
01:19 Answer 2 (Score 10)
01:39 Answer 3 (Score 0)
02:29 Thank you
--
Full question
https://stackoverflow.com/questions/5252...
Answer 1 links:
[https://pillow.readthedocs.io/en/stable/...]: https://pillow.readthedocs.io/en/stable/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #image #colors #pythonimaginglibrary #rotation
#avk47
ACCEPTED ANSWER
Score 38
If your original image has no alpha layer, you can use an alpha layer as a mask to convert the background to white. When rotate creates the "background", it makes it fully transparent.
# original image
img = Image.open('test.png')
# converted to have an alpha layer
im2 = img.convert('RGBA')
# rotated image
rot = im2.rotate(22.2, expand=1)
# a white image same size as rotated image
fff = Image.new('RGBA', rot.size, (255,)*4)
# create a composite image using the alpha layer of rot as a mask
out = Image.composite(rot, fff, rot)
# save your work (converting back to mode='1' or whatever..)
out.convert(img.mode).save('test2.bmp')
ANSWER 2
Score 12
There is a parameter fillcolor in a rotate method to specify color which will be use for expanded area:
white = (255,255,255)
pil_image.rotate(angle, PIL.Image.NEAREST, expand = 1, fillcolor = white)
https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.rotate
ANSWER 3
Score 0
Here is a working version, inspired by the answer, but it works without opening or saving images and shows how to rotate a text.
The two images have colored background and alpha channel different from zero to show what's going on. Changing the two alpha channels from 92 to 0 will make them completely transparent.
from PIL import Image, ImageFont, ImageDraw
text = 'TEST'
font = ImageFont.truetype(r'C:\Windows\Fonts\Arial.ttf', 50)
width, height = font.getsize(text)
image1 = Image.new('RGBA', (200, 150), (0, 128, 0, 92))
draw1 = ImageDraw.Draw(image1)
draw1.text((0, 0), text=text, font=font, fill=(255, 128, 0))
image2 = Image.new('RGBA', (width, height), (0, 0, 128, 92))
draw2 = ImageDraw.Draw(image2)
draw2.text((0, 0), text=text, font=font, fill=(0, 255, 128))
image2 = image2.rotate(30, expand=1)
px, py = 10, 10
sx, sy = image2.size
image1.paste(image2, (px, py, px + sx, py + sy), image2)
image1.show()