Ellipse not transparent - python

I'm trying to crop an image (drawing an ellipse on top) and then paste it on another image.
bg_img = Image.open('bgimgs/light.png')
bg_img = bg_img.resize((600, 420))
root = tk.Tk()
root.withdraw()
image_file = filedialog.askopenfilename()
profile_pic = Image.open(image_file, mode='r')
profile_pic = profile_pic.resize((55, 55))
profile_mask = (55, 55)
mask = Image.new('L', profile_mask, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0) + profile_mask, fill=255)
mask = mask.resize(profile_pic.size, Image.ANTIALIAS)
profile_pic.putalpha(mask)
bg_img.paste(profile_pic, (25, 40))
and it produces this.
How can i make the ellipse around picture transparent?

Related

How do I Anchor Text and Shrink it to fit it on an Image

I fount this code off of the PIL API(here is the link: https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html) and I wanted to also shrink it depending on the size of the text while it is centered.
here is the anchoring code
from PIL import Image, ImageDraw, ImageFont
font = ImageFont.truetype("mont.ttf", 48)
im = Image.new("RGB", (200, 200), "white")
d = ImageDraw.Draw(im)
d.text((100, 100), "Quick", fill="black", anchor="ms", font=font)
im.save('text.png')
And the outcome looks like this:
But if you increase the word size it looks like this:
So I just want the text to be centered and shrunk to fit the image
No detail about the requirements, so here only for result image with fixed size (200, 200), so font size will be changed.
Find the size of text by ImageDraw.textsize
Draw on an image with same width as the text by ImageDraw.text
Resize image to (200-2*border, 200-2*border) by Image.resize
Paste the resized image to a 200x200 image by Image.paste
from PIL import Image, ImageDraw, ImageFont
def text_to_image(text, filename='text.png', border=20):
im = Image.new("RGB", (1, 1), "white")
font = ImageFont.truetype("calibri.ttf", 48)
draw = ImageDraw.Draw(im)
size = draw.textsize(text, font=font)
width = max(size)
im = Image.new("RGB", (width, width), "white")
draw = ImageDraw.Draw(im)
draw.text((width//2, width//2), text, anchor='mm', fill="black", font=font)
im = im.resize((200-2*border, 200-2*border), resample=Image.LANCZOS)
new_im = Image.new("RGB", (200, 200), "white")
new_im.paste(im, (border, border))
new_im.show()
# new_im.save(filename)
text_to_image("Hello World")

How Can I create an image with python PIL where there are two colors?

Here is an example of the output I want to generate. I'm able to create an image with one color, but I don't have idea of how can use two colors, and how to color only certain parts of the image .
I solved in this way. I created two image with two different colors, and then paste them in another one image.
width = 400
height = 300
img = Image.new( mode = "RGB", size = (width, height), color = (209, 123, 193) )
#First IMG
img2 = Image.new( mode = "RGB", size = (width, height + 400), color = (255, 255, 255) )
#Second IMG
img3 = Image.new('RGB', (img.width, img.height + img2.height)) img3.paste(img, (0, 0)) img3.paste(img2, (img.width, 0))
#IMG + IMG2
I got my result.

cropping an image in a circular way ang paste on another image, using python

I am trying to crop an image in python in circular shape. And I also want to paste that image on the top of another image, and then then then save the image in a desired format.
This is the image that I want to crop in circular way
This is how the image should be looked like after cropping
This is the image on which I want to paste the circular shaped image
This is my expected output
Here is the code, as far I tried
from PIL import Image, ImageDraw, ImageFilter
im1 = Image.open('rocket.jpg')
im2 = Image.open('lena.jpg')
width, height = im1.size
print(height, width)
mask_im = Image.new("L", im2.size, 0)
draw = ImageDraw.Draw(mask_im)
draw.ellipse((150, 40, 250, 100), fill=255)
mask_im.save('mask_circle.jpg', quality=95)
back_im = im1.copy()
back_im.paste(im2, (0, 0), mask_im)
back_im.save('rocket_pillow_paste_mask_circle.jpg', quality=95)
mask_im_blur = mask_im.filter(ImageFilter.GaussianBlur(10))
mask_im_blur.save('mask_circle_blur.jpg', quality=95)
back_im = im1.copy()
back_im.paste(im2, (0, 0), mask_im_blur)
back_im.save('rocket_pillow_paste_mask_circle_blur.jpg', quality=95)
from PIL import Image, ImageDraw, ImageFilter
im1 = Image.open('rocket.jpg')
im2 = Image.open('lena.jpg')
#height, width, channels = im1.shape
width, height = im1.size
print(height, width)
# ![rocket_pillow_paste_out](data/dst/rocket_pillow_paste_out.jpg)
mask_im = Image.new("L", im2.size, 0)
draw = ImageDraw.Draw(mask_im)
draw.ellipse((140, 50, 260, 170), fill=255)
mask_im.save('mask_circle.jpg', quality=95)
back_im = im1.copy()
back_im.paste(im2, (0, 0), mask_im)
back_im.save('rocket_pillow_paste_mask_circle.jpg', quality=95)
# ![rocket_pillow_paste_mask_circle](data/dst/rocket_pillow_paste_mask_circle.jpg)
mask_im_blur = mask_im.filter(ImageFilter.GaussianBlur(10))
mask_im_blur.save('mask_circle_blur.jpg', quality=95)
back_im = im1.copy()
back_im.paste(im2, (0, 0), mask_im_blur)
back_im.save('rocket_pillow_paste_mask_circle_blur.jpg', quality=95)
# ![rocket_pillow_paste_mask_circle_blur](data/dst/rocket_pillow_paste_mask_circle_blur.jpg)

How can i fix transparency in image? PIL

when i trying to run this code, my image breaks
User avatar should be transparent
I want make like this:
How can i fix this?
import io
from PIL import Image, ImageDraw
im_rgb = Image.open('cards/customcard1.png')
im_a = Image.new("RGBA", im_rgb.size, 0)
im_a.paste(im_rgb)
draw = ImageDraw.Draw(im_a)
draw.ellipse((92, 193, 403, 504), fill=255)
memberavatar = await _ctx.author.avatar_url.read()
def rounded_avatar(member):
with Image.open(io.BytesIO(member)) as im:
with Image.new("RGBA", im.size) as background:
rgb_avatar = im.convert("RGBA")
with Image.new("L", im.size, 0) as mask:
mask_draw = ImageDraw.Draw(mask)
mask_draw.ellipse([(0, 0), im.size], fill=255)
background.paste(rgb_avatar, (0, 0), mask=mask)
return background
roundedAvatar = rounded_avatar(memberavatar)
roundedAvatar = roundedAvatar.resize((311, 311), Image.ANTIALIAS)
roundedAvatar.save('cards/roundedAva.png')
r = Image.open('cards/roundedAva.png').convert('RGBA')
im_a.paste(r, (92, 193))
im_a.save('C:/Users/RAINGM/desktop/hueta.png')
im_a.show()
I fix this with Image.alpha_composite()

Additive color with Tkinter

I'm trying to reproduce additive color with Tkinter.
My function :
def synthese(red,green,blue):
win2 = Tk()
win2.title("ADDITIVE COLOR")
win2.geometry("500x500")
win2.resizable(0,0)
hred = "#%02x%02x%02x" % (red, 0, 0) #RGB to Hexadecimal
hgreen = "#%02x%02x%02x" % (0, green, 0)
hblue = "#%02x%02x%02x" % (0, 0, blue)
r = 50
Width = 450
Height = 450
win3 = Canvas(win2, width = Width, height = Height, bg = 'white')
win3.pack(padx=5,pady=5)
win3.create_oval(10,150,300,440, outline=hred, fill=hred)
win3.create_oval(150,150,440,440, outline=hblue, fill=hblue)
win3.create_oval(75,10,375,300, outline=hgreen, fill=hgreen)
win2.mainloop()
What I get :
And what I would like :
It is possible to merge the colors or I need to find the collision zones?
You can use ImageChops to add images.
So you can do something like this:
from Tkinter import Tk, Canvas, Label
import ImageDraw, ImageChops, Image, ImageTk
image1 = Image.new("RGBA", (500, 500), color=0)
image2 = Image.new("RGBA", (500, 500), color=0)
image3 = Image.new("RGBA", (500, 500), color=0)
draw1 = ImageDraw.Draw(image1)
draw2 = ImageDraw.Draw(image2)
draw3 = ImageDraw.Draw(image3)
draw1.ellipse([10, 150, 300, 440], (128,0,0))
draw2.ellipse([150, 150, 440, 440], (0,0,128))
draw3.ellipse([75, 10, 375, 300], (0,128,0))
out = ImageChops.add(image1,image2,0.5)
out = ImageChops.add(out,image3,0.5)
win2 = Tk()
photo = ImageTk.PhotoImage(out)
label = Label(win2, image=photo)
label.pack()
win2.mainloop()
output:
Here's a way to draw additive RGB circles using Numpy. It converts the Numpy data to a Tkinter PhotoImage object using PIL (Pillow), and displays the results in a Tkinter Label. I use a black background because we're doing additive color mixing.
import numpy as np
from PIL import Image, ImageTk
import tkinter as tk
width, height = 400, 360
# Make RGB colors
red, grn, blu = np.eye(3, dtype=np.uint8) * 255
class GUI:
def __init__(self, width, height):
self.root = root = tk.Tk()
root.title('Circles')
root.geometry('%dx%d' % (width, height))
self.img_label = tk.Label(self.root)
self.img_label.pack(fill='both', expand=True)
gui = GUI(width, height)
# Increase the scale for smoother circles
scale = 4
width *= scale
height *= scale
screen = np.zeros((height, width, 3), dtype=np.uint8)
def show(fname=None):
img = Image.fromarray(screen, 'RGB')
img = img.resize((width // scale, height // scale), resample=Image.BILINEAR)
gui.photo = ImageTk.PhotoImage(image=img)
gui.img_label.config(image=gui.photo)
gui.root.update()
if fname is not None:
img.save(fname)
def disc(radius):
diameter = 2 * radius
yy, xx = np.mgrid[:diameter, :diameter] - radius
c = xx * xx + yy * yy < radius * radius
return c.reshape(diameter, diameter, 1)
def get_region(cx, cy, radius):
ylo = cy - radius
yhi = cy + radius
xlo = cx - radius
xhi = cx + radius
return screen[ylo:yhi, xlo:xhi]
radius = 120 * scale
circle = disc(radius)
cx = width // 2
cy = 130 * scale
region = get_region(cx, cy, radius)
region |= circle * red
show()
cy += 97 * scale
cx -= 56 * scale
region = get_region(cx, cy, radius)
region |= circle * grn
show()
cx += 112 * scale
region = get_region(cx, cy, radius)
region |= circle * blu
show('rgb.png')
gui.root.mainloop()
output
Using PIL you can create three grayscale layers, draw circles and use them to create expected circles but on black background.
If you use inverted layers then you get white background but with wrong circles.
With PIL you can even display it or save in file.
from PIL import Image, ImageDraw
def synthese(red=255, green=255, blue=255):
background = 0 # black
# layers in greyscale
layer_R = Image.new('L', (450, 450), background)
layer_G = Image.new('L', (450, 450), background)
layer_B = Image.new('L', (450, 450), background)
# draw circle on red layer
draw_R = ImageDraw.Draw(layer_R)
draw_R.ellipse((10,150,300,440), red)
# draw circle on green layer
draw_G = ImageDraw.Draw(layer_G)
draw_G.ellipse((150,150,440,440), green)
# draw circle on blue layer
draw_B = ImageDraw.Draw(layer_B)
draw_B.ellipse((75,10,375,300), blue)
#layer_R.show()
#layer_G.show()
#layer_B.show()
#layer_R.save('layer_r.png')
#layer_G.save('layer_g.png')
#layer_B.save('layer_b.png')
# create RGB image using greyscale layers
image_RGB = Image.merge('RGB', (layer_R, layer_G, layer_B))
# show it
image_RGB.show()
#image_RGB.save('rgb.png')
synthese(255, 255, 255)

Categories

Resources