Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have you tried something like <code>image.putalpha(mask)</code> to replace the image's alpha channel with the mask? This seems like it should do what you want. <code>mask</code> might have to be in 'L' mode for this to work correctly, and <code>image</code> should probaby be 'RGBA', but might be automatically converted for you.</p> <p>The top answer here provides some good examples: <a href="https://stackoverflow.com/questions/890051/how-do-i-generate-circuar-thumbnails-with-pil">How do I generate circular thumbnails with PIL?</a></p> <p>Your mask image looks fine, but I think you want to swap 'white' and 'black', so you have a white rounded rectangle exactly the shape you want your final image to be, on a black background. You will probably also need to use the 'L' mode (greyscale) one.</p> <p>Once you have this image, you can replace the <code>return ImageChops.add(mask, image)</code> by <code>image.putalpha(mask); return image</code> and this should cause the image to be transparent in only the black areas of the mask.</p> <p>You might need to convert the image first with <code>image.convert('RGBA')</code> but I think this is unnecessary in later versions of PIL, it does it automatically.</p> <p>Something like: (sorry can't test this right now)</p> <pre> def round_corner_jpg(image, radius): """generate round corner for image""" mask = Image.new('L', image.size) # filled with black by default draw = aggdraw.Draw(mask) brush = aggdraw.Brush('white') width, height = mask.size #upper-left corner draw.pieslice((0,0,radius*2, radius*2), 90, 180, None, brush) #upper-right corner draw.pieslice((width - radius*2, 0, width, radius*2), 0, 90, None, brush) #bottom-left corner draw.pieslice((0, height - radius * 2, radius*2, height),180, 270, None, brush) #bottom-right corner draw.pieslice((width - radius * 2, height - radius * 2, width, height), 270, 360, None, brush) #center rectangle draw.rectangle((radius, radius, width - radius, height - radius), brush) #four edge rectangle draw.rectangle((radius, 0, width - radius, radius), brush) draw.rectangle((0, radius, radius, height-radius), brush) draw.rectangle((radius, height-radius, width-radius, height), brush) draw.rectangle((width-radius, radius, width, height-radius), brush) draw.flush() image = image.convert('RGBA') image.putalpha(mask) return image </pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload