librelist archives

« back to archive

Convert PNG with Transparency to JPG

Convert PNG with Transparency to JPG

From:
Sergei G
Date:
2013-09-08 @ 04:31
I am trying to resize a PNG image into a smaller one. It works.
If I keep PNG extension, then image quality is great. If I use JPEG, then
what used to be a transparent background becomes black. I can't seem to
figure out how to switch that black into the color of my choice like white.

Here is the code I've got. Some of the lines don't seem to have much of
impact. Like enabling alpha_channel or setting background_color. I am sure
it simply because I don't really know what I am doing in this case.


    with Image(filename=fileName) as original:
        original.alpha_channel = True
        with Color('white') as white:
            original.background_color = white
        original.transform(resize="350")
        original.save(filename=destinationOrig)
        with original.convert('jpeg') as target:
            target.save(filename=destinationJpg)


Thank you

Re: [wand] Convert PNG with Transparency to JPG

From:
Hong Minhee
Date:
2013-09-08 @ 05:54
Hi Sergei,

You can use Image.composite() method to do that:

  http://docs.wand-py.org/en/0.3.3/wand/image.html#wand.image.BaseImage.composite

Here’s an example:

  from wand.color import Color
  from wand.image import Image


  with Image(filename='tests/assets/trimtest.png') as png:
      with Image(width=png.width,
                 height=png.height,
                 background=Color('white')) as jpeg:
          jpeg.composite(png, 0, 0)
          jpeg.save(filename='result.jpg')

(You can see the same code in gist: https://gist.github.com/dahlia/6482216
as well.)


Thanks,
Hong Minhee

On Sep 8, 2013, at 1:31 PM, Sergei G <sergeig.public@gmail.com> wrote:

> I am trying to resize a PNG image into a smaller one. It works.
> If I keep PNG extension, then image quality is great. If I use JPEG, 
then what used to be a transparent background becomes black. I can't seem 
to figure out how to switch that black into the color of my choice like 
white.
> 
> Here is the code I've got. Some of the lines don't seem to have much of 
impact. Like enabling alpha_channel or setting background_color. I am sure
it simply because I don't really know what I am doing in this case.
> 
> 
>     with Image(filename=fileName) as original:
>         original.alpha_channel = True
>         with Color('white') as white:
>             original.background_color = white
>         original.transform(resize="350")
>         original.save(filename=destinationOrig)
>         with original.convert('jpeg') as target:
>             target.save(filename=destinationJpg)
> 
> 
> Thank you

Re: [wand] Convert PNG with Transparency to JPG

From:
Sergei G
Date:
2013-09-08 @ 06:25
Thanks. It worked perfectly. I thought of using composite, but was not sure
how to do it.


On Sat, Sep 7, 2013 at 10:54 PM, Hong Minhee <minhee@dahlia.kr> wrote:

> Hi Sergei,
>
> You can use Image.composite() method to do that:
>
>
> http://docs.wand-py.org/en/0.3.3/wand/image.html#wand.image.BaseImage.composite
>
> Here’s an example:
>
>   from wand.color import Color
>   from wand.image import Image
>
>
>   with Image(filename='tests/assets/trimtest.png') as png:
>       with Image(width=png.width,
>                  height=png.height,
>                  background=Color('white')) as jpeg:
>           jpeg.composite(png, 0, 0)
>           jpeg.save(filename='result.jpg')
>
> (You can see the same code in gist: 
https://gist.github.com/dahlia/6482216as well.)
>
>
> Thanks,
> Hong Minhee
>
> On Sep 8, 2013, at 1:31 PM, Sergei G <sergeig.public@gmail.com> wrote:
>
> > I am trying to resize a PNG image into a smaller one. It works.
> > If I keep PNG extension, then image quality is great. If I use JPEG,
> then what used to be a transparent background becomes black. I can't seem
> to figure out how to switch that black into the color of my choice like
> white.
> >
> > Here is the code I've got. Some of the lines don't seem to have much of
> impact. Like enabling alpha_channel or setting background_color. I am sure
> it simply because I don't really know what I am doing in this case.
> >
> >
> >     with Image(filename=fileName) as original:
> >         original.alpha_channel = True
> >         with Color('white') as white:
> >             original.background_color = white
> >         original.transform(resize="350")
> >         original.save(filename=destinationOrig)
> >         with original.convert('jpeg') as target:
> >             target.save(filename=destinationJpg)
> >
> >
> > Thank you
>
>