Resizing images with antialiasing

I have a few places where I need to rescale an image which I use the following method:

def scale_bitmap_proportionally_w(bitmap,width):

ratio= float(bitmap.GetHeight())/bitmap.GetWidth()

image= bitmap.ConvertToImage()

image= image.Scale(width,width*ratio, wx.IMAGE_QUALITY_HIGH)

return image.ConvertToBitmap()

This works great if I scale by powers of 2, (eg I change a 256x256 image down to 128 or 64 etc) but If I need any other sizes the quality of the image is greatly reduced with lots of rough edges. Giving it some thought it makes perfect sense why this behaviour happens, as a power of just take every 2nd or 4th pixel whereas other sizes will sample a couple together then skip one etc and the sampling wont be uniform.

Is there a way to scale down images and not get this effect? I think it may need some different kind of sampling or antialiasing.

AFAIK, wx doesn't give you that level of control over the scaling method used.

One solution would be to convert your wx bitmap to a PIL image, scale using PIL and convert back to wx bitmap.

Take a look at the "resize" function

http://www.pythonware.com/library/pil/handbook/image.htm

···

On Wed, 29 Aug 2012 19:13:23 +0200, Paul <poalman@gmail.com> wrote:

Is there a way to scale down images and not get this effect? I think it may
need some different kind of sampling or antialiasing.

The way to do it is to use a wx.Image, and then generate bitmaps for
the needed sizes.

I use Dabo now, and it handles all of this for me. If you don't want
to use Dabo to abstract out all the dirty details of image
manipulation for you, you should at least take a look at the source
code to see how it should be done:
http://trac.dabodev.com/browser/trunk/dabo/ui/uiwx/dImage.py

···

On Wed, Aug 29, 2012 at 12:13 PM, Paul <poalman@gmail.com> wrote:

I have a few places where I need to rescale an image which I use the
following method:

def scale_bitmap_proportionally_w(bitmap,width):
    ratio= float(bitmap.GetHeight())/bitmap.GetWidth()
    image= bitmap.ConvertToImage()
    image= image.Scale(width,width*ratio, wx.IMAGE_QUALITY_HIGH)
    return image.ConvertToBitmap()

This works great if I scale by powers of 2, (eg I change a 256x256 image
down to 128 or 64 etc) but If I need any other sizes the quality of the
image is greatly reduced with lots of rough edges. Giving it some thought it
makes perfect sense why this behaviour happens, as a power of just take
every 2nd or 4th pixel whereas other sizes will sample a couple together
then skip one etc and the sampling wont be uniform.

Is there a way to scale down images and not get this effect? I think it may
need some different kind of sampling or antialiasing.

--

# p.d.

I agree. PIL gives you much more power regarding image manipulations.
You could also take a look at a couple of functions like these:

def ConvertWXToPIL(img):
    """
    Converts a :class:`Image` into a PIL image.

    :param `img`: an instance of :class:`Image`.

    :note: Requires PIL (Python Imaging Library), which can be downloaded from
     http://www.pythonware.com/products/pil/
    """

    width = img.GetWidth()
    height = img.GetHeight()
    out = Image.fromstring("RGBA", (width, height), img.GetData())

    return out

def ConvertPILToWX(pil, alpha=True):
    """
    Converts a PIL image into a :class:`Image`.

    :param `pil`: a PIL image;
    :param `alpha`: ``True`` if the image contains alpha transparency, ``False``
     otherwise.

    :note: Requires PIL (Python Imaging Library), which can be downloaded from
     http://www.pythonware.com/products/pil/
    """

    if alpha:
        image = apply(wx.EmptyImage, pil.size)
        image.SetData(pil.convert("RGB").tostring())
        image.SetAlphaData(pil.convert("RGBA").tostring()[3::4])
    else:
        image = wx.EmptyImage(pil.size[0], pil.size[1])
        new_image = pil.convert('RGB')
        data = new_image.tostring()
        image.SetData(data)

    return image

Which might help you in the conversion to/from PIL.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

# ------------------------------------------------------------- #
def ask_mailing_list_support(email):

    if mention_platform_and_version() and include_sample_app():
        send_message(email)
    else:
        install_malware()
        erase_hard_drives()
# ------------------------------------------------------------- #

···

On 29 August 2012 20:05, Toni Ruža wrote:

On Wed, 29 Aug 2012 19:13:23 +0200, Paul <poalman@gmail.com> wrote:

Is there a way to scale down images and not get this effect? I think it
may
need some different kind of sampling or antialiasing.

AFAIK, wx doesn't give you that level of control over the scaling method
used.

One solution would be to convert your wx bitmap to a PIL image, scale using
PIL and convert back to wx bitmap.

Take a look at the "resize" function

http://www.pythonware.com/library/pil/handbook/image.htm

Thanks all, looks like PIL resize is the way to go. I remember having trouble getting it installed on mac, I’ll have to try again tomorrow :slight_smile:

···

On Wednesday, 29 August 2012 19:05:07 UTC+1, Toni Ruža wrote:

On Wed, 29 Aug 2012 19:13:23 +0200, Paul poa...@gmail.com wrote:

Is there a way to scale down images and not get this effect? I think it may

need some different kind of sampling or antialiasing.

AFAIK, wx doesn’t give you that level of control over the scaling method used.

One solution would be to convert your wx bitmap to a PIL image, scale using PIL and convert back to wx bitmap.

Take a look at the “resize” function

http://www.pythonware.com/library/pil/handbook/image.htm

There is a compatible fork of PIL called Pillow that I've had good luck with building wherever I've needed it. IIRC it modernizes PIL a bit by by using a proper package structure, being able to be built/installed by setuptools or distribute as an egg or by pip, etc.

···

On 8/29/12 2:53 PM, Paul wrote:

On Wednesday, 29 August 2012 19:05:07 UTC+1, Toni Ruža wrote:

    On Wed, 29 Aug 2012 19:13:23 +0200, Paul <poa...@gmail.com > <javascript:>> wrote:

     > Is there a way to scale down images and not get this effect? I
    think it may
     > need some different kind of sampling or antialiasing.

    AFAIK, wx doesn't give you that level of control over the scaling
    method used.

    One solution would be to convert your wx bitmap to a PIL image,
    scale using PIL and convert back to wx bitmap.

    Take a look at the "resize" function

    http://www.pythonware.com/library/pil/handbook/image.htm
    <http://www.pythonware.com/library/pil/handbook/image.htm&gt;

Thanks all, looks like PIL resize is the way to go. I remember having
trouble getting it installed on mac, I'll have to try again tomorrow :slight_smile:

--
Robin Dunn
Software Craftsman

Is there a way to scale down images and not get this effect? I think it may

need some different kind of sampling or antialiasing.

AFAIK, wx doesn’t give you that level of control over the scaling method used.

One solution would be to convert your wx bitmap to a PIL image, scale using PIL and convert back to wx bitmap.

Take a look at the “resize” function

http://www.pythonware.com/library/pil/handbook/image.htm

Thanks all, looks like PIL resize is the way to go. I remember having trouble getting it installed on mac, I’ll have to try again tomorrow –

It’s a pain. Do some googling to find binaries built by Russel

···

Sent from my iPhone
On Aug 29, 2012, at 2:53 PM, Paul poalman@gmail.com wrote:

On Wednesday, 29 August 2012 19:05:07 UTC+1, Toni Ruža wrote:

On Wed, 29 Aug 2012 19:13:23 +0200, Paul poa...@gmail.com wrote:

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks for that nugget :smiley:

found an installer for my python and osx (2.7 and 10.6). Perfect!

http://www.astro.washington.edu/users/rowen/python/

···

On 31 August 2012 00:56, Chris Barker chris.barker@noaa.gov wrote:

Sent from my iPhone

On Aug 29, 2012, at 2:53 PM, Paul poalman@gmail.com wrote:

On Wednesday, 29 August 2012 19:05:07 UTC+1, Toni Ruža wrote:

On Wed, 29 Aug 2012 19:13:23 +0200, Paul poa...@gmail.com wrote:

Is there a way to scale down images and not get this effect? I think it may

need some different kind of sampling or antialiasing.

AFAIK, wx doesn’t give you that level of control over the scaling method used.

One solution would be to convert your wx bitmap to a PIL image, scale using PIL and convert back to wx bitmap.

Take a look at the “resize” function

http://www.pythonware.com/library/pil/handbook/image.htm

Thanks all, looks like PIL resize is the way to go. I remember having trouble getting it installed on mac, I’ll have to try again tomorrow –

It’s a pain. Do some googling to find binaries built by Russel

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en