How to "crop" along a mask?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to "crop" along a mask?

pontissimo
I'm trying to take a group of pictures and give them rounded edges. I have a source image (a rectangle with rounded edges, transparent PNG) and I'd like to use this as a template. All the target images would become transparent wherever this source image is transparent.

In other words, I'd be making the same region of the target image transparent as is transparent in the source image.

Any tips for accomplishing this?



_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to "crop" along a mask?

Roger André-2
I would think that you could take the Alpha band from the source image and apply it to the target.  Not sure how exactly this would be done with PIL, but you might try copying the source image's "transparency" setting over to the target.

Roger
--

On Sun, Jan 24, 2010 at 2:56 PM, Alec Bennett <[hidden email]> wrote:
I'm trying to take a group of pictures and give them rounded edges. I have a source image (a rectangle with rounded edges, transparent PNG) and I'd like to use this as a template. All the target images would become transparent wherever this source image is transparent.

In other words, I'd be making the same region of the target image transparent as is transparent in the source image.

Any tips for accomplishing this?



_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig



_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to "crop" along a mask?

Edward Cannon-2
In reply to this post by pontissimo
Using imagechops, multipy the alpha channels. If your source image in
white everywhere you could just multiply the images. Alternately you
could add the mask from the one image to the other image using the
addalpha method which takes a L or 1 band image as the alpha chanel)

On Mon, Jan 25, 2010 at 11:15 AM, Laura & Edward Cannon
<[hidden email]> wrote:

> Using imagechops, multipy the alpha channels. If your source image in
> white everywhere you could just multiply the images. Alternately you
> could add the mask from the one image to the other image using the
> addalpha method which takes a L or 1 band image as the alpha chanel)
>
> On Sun, Jan 24, 2010 at 2:56 PM, Alec Bennett <[hidden email]> wrote:
>> I'm trying to take a group of pictures and give them rounded edges. I have a
>> source image (a rectangle with rounded edges, transparent PNG) and I'd like
>> to use this as a template. All the target images would become transparent
>> wherever this source image is transparent.
>>
>> In other words, I'd be making the same region of the target image
>> transparent as is transparent in the source image.
>>
>> Any tips for accomplishing this?
>>
>>
>>
>> _______________________________________________
>> Image-SIG maillist  -  [hidden email]
>> http://mail.python.org/mailman/listinfo/image-sig
>>
>>
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: How to "crop" along a mask?

Nils de Reus
In reply to this post by pontissimo

What you describe is not cropping, but rather the setting of an alpha mask. Here is the simple case for if all your images are the same size as your template, and only the template has transparancy information:

# Get the Alpha band from the template

tmplt = Image.open('template.png')
A = tmplt.split()[3]

# Get the R, G, B bands from the image to apply the alpha to

im = Image.open('myimage.png')
[R, G, B] = im.split()

# Combine the RGB from the image with the A from the template

newimg = Image.merge('RGBA', (R, G, B, A))

# Write the result back to disk under another name

newimg.save('myimage_rgba.png')


To get from the simple case to more general uses:
- If your images are not all the same size, you need to think about how you want to scale the template to match.
- If your other images do have some other transparency of their own, combine the A band from the image with the A band from the template before you merge everything back together.

Hope this helps, kind regards,
   Nils

________________________________________
I'm trying to take a group of pictures and give them rounded edges. I have a source image (a rectangle with rounded edges, transparent PNG) and I'd like to use this as a template. All the target images would become transparent wherever this source image is transparent.

In other words, I'd be making the same region of the target image transparent as is transparent in the source image.

Any tips for accomplishing this?


_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig