Related
How to lower opacity to line? I would like to lower opacity to one of line in example bellow.
from PIL import Image, ImageDraw
img = Image.new('RGB', (100, 100), (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.line((100, 30, 0, 30), (0, 0, 0), 20)
draw.line((100, 70, 0, 70), (0, 0, 0), 20)
img.show()
I have seen in one example they created opacity like this...
TRANSPARENCY = .25 # Degree of transparency, 0-100%
OPACITY = int(255 * TRANSPARENCY)
But don't know how to apply to one of lines. Any ideas?
EDIT
I made some changes (based on answer of #Pedro Maia), it still doesn't work, just changes a color, it doesn't lower opacity to see background color.
from PIL import Image, ImageDraw
img = Image.new('RGBA', (500, 500), (255, 255, 255))
draw = ImageDraw.Draw(img)
TRANSPARENCY = .25 # Degree of transparency, 0-100%
draw.line((200, 0, 200, 600),(255, 0, 0), 60)
draw.line((500, 100, 0, 100), (0, 0, 0, int(255 * TRANSPARENCY)), 60)
draw.line((500, 400, 0, 400),(0, 0, 0), 60)
img
And I have to convert it to RGB to export it as 'jpg'
You would have to do something like this, which is similar to how the example code works, to do what (I think) you want to. I changed the code you added to your question in the EDIT slightly so it better demonstrates that lines of different amounts of transparency can be drawn.
from PIL import Image, ImageDraw
RED = (255, 0, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Calculate alpha given a 0-100% opacity value.
opacity = lambda transparency: (int(255 * (transparency/100.)),) # Returns a monuple.
def draw_transp_line(image, xy, color, width=1, joint=None):
""" Draw line with transparent color on the specified image. """
if len(color) < 4: # Missing alpha?
color += opacity(100) # Opaque since alpha wasn't specified.
# Make an overlay image the same size as the specified image, initialized to
# a fully transparent (0% opaque) version of the line color, then draw a
# semi-transparent line on it.
overlay = Image.new('RGBA', image.size, color[:3]+opacity(0))
draw = ImageDraw.Draw(overlay) # Create a context for drawing things on it.
draw.line(xy, color, width, joint)
# Alpha composite the overlay image onto the original.
image.alpha_composite(overlay)
# Create opaque white RGBA background image.
img = Image.new('RGBA', (500, 500), (255, 255, 255)+opacity(100))
draw_transp_line(img, ((200, 0), (200, 600)), RED+opacity(100), 60)
draw_transp_line(img, ((500, 100), (0, 100)), BLACK+opacity(25), 60)
draw_transp_line(img, ((150, 50), (600, 400)), BLACK+opacity(50), 60)
img = img.convert("RGB") # Remove alpha for saving in jpg format.
img.save('transparent_lines.jpg')
img.show()
JPG image created:
With draw.line you can pass as argument RGB or RGBA just pass the value of the transparency:
draw.line((100, 30, 0, 30), (0, 0, 0, int(255 * TRANSPARENCY)), 20)
Also when creating the image set it as RGBA:
img = Image.new('RGBA', (100, 100), (255, 255, 255))
I am currently using PIL.Image.Image.getbbox() to get the bounding box for the non-zero (non-transparent) regions of an image.
What if I have an image that has a background of a specific color? How can I get the bounding box of the image then? Same idea as getbbox() but instead of non-zero, I specify the RGB values.
I'm afraid, my comment didn't properly express, what I wanted to suggest. So, here's a full answer:
Make a copy of your image (that one, that has a specific background color).
On that copy, replace the specific background color with black.
Call getbbox on that copy.
Maybe, the following code and examples make things more clear:
import numpy as np
from PIL import Image, ImageDraw
# Black background
img = Image.new('RGB', (400, 400), (0, 0, 0))
draw = ImageDraw.Draw(img)
draw.rectangle((40, 40, 160, 160), (255, 0, 0))
draw.rectangle((280, 260, 380, 330), (0, 255, 0))
img.save('black_bg.png')
print(img.getbbox(), '\n')
# Specific color background
bg_color = (255, 255, 0)
img = Image.new('RGB', (400, 400), bg_color)
draw = ImageDraw.Draw(img)
draw.rectangle((40, 40, 160, 160), (255, 0, 0))
draw.rectangle((280, 260, 380, 330), (0, 255, 0))
img.save('color_bg.png')
print(img.getbbox(), '\n')
# Suggested color replacing (on image copy) - Pillow only, slow
img_copy = img.copy()
for y in range(img_copy.size[1]):
for x in range(img_copy.size[0]):
if img_copy.getpixel((x, y)) == bg_color:
img_copy.putpixel((x, y), (0, 0, 0))
print(img_copy.getbbox(), '\n')
# Suggested color replacing (on image copy) - NumPy, fast
img_copy = np.array(img)
img_copy[np.all(img_copy == bg_color, axis=2), :] = 0
print(Image.fromarray(img_copy).getbbox())
There's one image with black background:
The corresponding output of getbbox is:
(40, 40, 381, 331)
Also, there's an image with a specific background color (yellow):
Calling getbbox on that image – obviously – returns:
(0, 0, 400, 400)
By simply replacing yellow with black in some copy of the second image, we again get the correct results from getbbox (both proposed methods):
(40, 40, 381, 331)
(40, 40, 381, 331)
Since iterating single pixels in Pillow is kinda slow, you could also use NumPy's vectorization abilities to speed up that task.
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.16299-SP0
Python: 3.8.5
NumPy: 1.19.5
Pillow: 8.1.0
----------------------------------------
I am trying to draw multiple transparent ellipses with Pillow, and I want to draw them without outlines. I can't seem to make it work without outlines.
Here is some test code:
from PIL import Image, ImageDraw
w,h = 100,100
img = Image.new('RGB', (w,h),(255,255,255))
drw = ImageDraw.Draw(img,"RGBA")
drw.polygon([(50, 0), (100, 100), (0, 100)], (255, 0, 0, 125))
drw.polygon([(50,100), (100, 0), (0, 0)], (0, 255, 0, 125))
drw.ellipse([(40, 40), (w - 10, h - 10)], fill=(0,0,255,125), outline=None)
img.save('out.png', 'PNG')
(from here, with some modifications)
Output
Only the ellipse gets an outline. Why? How can I avoid this?
After playing around with some codes, looking at the documentation and some source codes, I'm quite sure, that most likely there's some issue with the functions like arc, chord, ellipse, that all share the same code under the hood.
I created the following example:
from matplotlib import pyplot as plt
from PIL import Image, ImageDraw
def example(outline_alpha=None, width=None):
if outline_alpha is None:
outline = None
else:
outline = (255, 255, 0, outline_alpha)
if width is None:
width = 0
img = Image.new('RGB', (100, 100), (255, 255, 255))
drw = ImageDraw.Draw(img, 'RGBA')
drw.line([(0, 40), (100, 40)], (0, 0, 0, 255))
drw.line([(50, 100), (100, 0)], (0, 0, 0, 255))
drw.polygon([(50, 100), (100, 0), (0, 0)], (0, 255, 0, 128), outline)
drw.ellipse([(40, 40), (90, 90)], (0, 0, 255, 128), outline, width)
return img
plt.figure(1, figsize=(15, 10))
plt.subplot(2, 3, 1), plt.imshow(example()), plt.title('No outlines specified, width = 0')
plt.subplot(2, 3, 2), plt.imshow(example(255)), plt.title('Opaque outlines specified, width = 0')
plt.subplot(2, 3, 3), plt.imshow(example(128)), plt.title('Semi-transparent outlines specified, width = 0')
plt.subplot(2, 3, 4), plt.imshow(example(None, 5)), plt.title('No outlines specified, width = 5')
plt.subplot(2, 3, 5), plt.imshow(example(255, 5)), plt.title('Opaque outlines specified, width = 5')
plt.subplot(2, 3, 6), plt.imshow(example(20, 5)), plt.title('Semi-transparent outlines specified, width = 5')
plt.tight_layout()
plt.show()
The output is the following:
Looking at the polygon, if no outline is specified (top left image), we see that the black line is visible, which is one of the polygon's borders. Specifying an opaque outline (top center image), the black line's no longer visible. Setting a semi-transparent outline (top right image) reveals, that the outline is identical to the polygon's border.
Now, the same for the ellipse: If no outline is set (top left), an outline is shown nevertheless, most likely the same color as used for the fill parameter, but without incorporating an alpha value. Setting an opaque outline (top center) "overwrites" the unexpected existent outline, but when setting a semi-transparent outline, we see that the unexpected outline is still there.
This effect becomes even more obvious, when setting width > 1 in ellipse, see the bottom row. The unexpected outline still seems to have width = 1, whereas the explicitly set outline has width = 5.
Again, I'm quite sure, that this behavior isn't intended – and I will open an issue in their GitHub issue tracker. EDIT: I just opened this issue. ANOTHER EDIT: It's fixed.
Hope that helps – somehow...
How can I make watermark image transparent? For example, 60% transparent. I have tried with putalpha but seems that it's doesn't work as expected
from PIL import Image
temp_image = Image.open('test1.jpg')
watermark = Image.open('watermark.png')
x, y = temp_image.size
image_with_watermark = Image.new('RGBA', (x, y), (0, 0, 0, 0))
image_with_watermark.paste(temp_image, (0, 0))
image_with_watermark.paste(watermark, (0, 0), mask=watermark)
image_with_watermark.show()
EDIT:
ok, this works, need to figure out how to set it up using %
from PIL import Image
temp_image = Image.open('test1.jpg')
watermark = Image.open('watermark.png')
x, y = temp_image.size
watermask = watermark.convert("L").point(lambda x: min(x, 50))
watermark.putalpha(watermask)
image_with_watermark = Image.new('RGBA', (x, y), (0, 0, 0, 0))
image_with_watermark.paste(temp_image, (0, 0))
image_with_watermark.paste(watermark, (0, 0), mask=watermark)
image_with_watermark.show()
meh, quality of the watermark is very low after:
watermask = watermark.convert("L").point(lambda x: min(x, 50))
watermark.putalpha(watermask)
What is the best way to achieve what I need?
Here's a solution that will work for both RGB and RGBA watermark images:
from PIL import Image
TRANSPARENCY = 65 # percentage
temp_image = Image.open('test1.jpg')
watermark = Image.open('watermark.png')
if watermark.mode!='RGBA':
alpha = Image.new('L', watermark.size, 255)
watermark.putalpha(alpha)
paste_mask = watermark.split()[3].point(lambda i: i * TRANSPARENCY / 100.)
temp_image.paste(watermark, (0,0), mask=paste_mask)
temp_image.save('res.png')
Sample image (author - Neil Howard):
Sample watermark (the background is transparent):
Sample result:
I have a png image as background and I want to add a transparent mesh to this background but this doesn't work as expected. The background image is converted to transparent on places where I apply transparent mesh.
I am doing:
from PIL import Image, ImageDraw
map_background = Image.open(MAP_BACKGROUND_FILE).convert('RGBA')
map_mesh = Image.new('RGBA', (width, height), (0, 0, 0, 0))
draw = ImageDraw.Draw(map_mesh)
# Create mesh using: draw.line([...], fill=(255, 255, 255, 50), width=1)
...
map_background.paste(map_mesh, (0, 0), map_mesh)
But the result is:
You can see a chessboard pattern if you look carefully (used in graphics programs as no background). Transparent lines makes the background layer transparent too in places where both layers met. But I only want the transparent line to be added on top of the background.
I can solve it with:
map_background.paste((255,255,255), (0, 0), map_mesh)
but as I use different colors for different lines, I would have to make for every color this process. If I had 100 colors, then I need 100 layers what is not very good solution.
What you are trying to do is to composite the grid onto the background, and for that you need to use Image.blend or Image.composite. Here's an example using the latter to composite red lines with random alpha values onto a white background:
import Image, ImageDraw, random
background = Image.new('RGB', (100, 100), (255, 255, 255))
foreground = Image.new('RGB', (100, 100), (255, 0, 0))
mask = Image.new('L', (100, 100), 0)
draw = ImageDraw.Draw(mask)
for i in range(5, 100, 10):
draw.line((i, 0, i, 100), fill=random.randrange(256))
draw.line((0, i, 100, i), fill=random.randrange(256))
result = Image.composite(background, foreground, mask)
From left to right:
[background] [mask]
[foreground]
[result]
(If you are happy to write the result back to the background image, then you can use one of the masked versions of Image.paste, as pointed out by Paulo Scardine in a deleted answer.)
I had trouble getting the above examples to work well. Instead, this worked for me:
import numpy as np
import Image
import ImageDraw
def add_craters(image, craterization=20.0, width=256, height=256):
foreground = Image.new('RGBA', (width, height), (0, 0, 0, 0))
draw = ImageDraw.Draw(foreground)
for c in range(0, craterization):
x = np.random.randint(10, width-10)
y = np.random.randint(10, height-10)
radius = np.random.randint(2, 10)
dark_color = (0, 0, 0, 128)
draw.ellipse((x-radius, y-radius, x+radius, y+radius), fill=dark_color)
image_new = Image.composite(foreground, image, foreground)
return image_new