Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8826665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:11:41+00:00 2026-06-14T07:11:41+00:00

Say you want to scale a transparent image but do not yet know the

  • 0

Say you want to scale a transparent image but do not yet know the color(s) of the background you will composite it onto later. Unfortunately PIL seems to incorporate the color values of fully transparent pixels leading to bad results. Is there a way to tell PIL-resize to ignore fully transparent pixels?

import PIL.Image

filename = "trans.png"
size = (25,25)

im = PIL.Image.open(filename)
print im.mode  # RGBA

im = im.resize(size, PIL.Image.LINEAR)  # the same with CUBIC, ANTIALIAS, transform
# im.show()  # does not use alpha
im.save("resizelinear_"+filename)


# PIL scaled image has dark border

original image with (0,0,0,0) black but fully transparent background
output image with black halo
proper output scaled with gimp

original image with (0,0,0,0) (black but fully transparent) background (left)

output image with black halo (middle)

proper output scaled with gimp (right)

It looks like to achieve what I am looking for I would have to modify the sampling of the resize function itself such that it would ignore pixels with full transparency.

I have found a very ugly solution. It sets the color values of fully transparent pixels to the average of the surrounding non fully transparent pixels to minimize impact of fully transparent pixel colors while resizing. It is slow in the simple form but I will post it if there is no other solution. Might be possible to make it faster by using a dilate operation to only process the necessary pixels.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:11:42+00:00Added an answer on June 14, 2026 at 7:11 am

    It appears that PIL doesn’t do alpha pre-multiplication before resizing, which is necessary to get the proper results. Fortunately it’s easy to do by brute force. You must then do the reverse to the resized result.

    def premultiply(im):
        pixels = im.load()
        for y in range(im.size[1]):
            for x in range(im.size[0]):
                r, g, b, a = pixels[x, y]
                if a != 255:
                    r = r * a // 255
                    g = g * a // 255
                    b = b * a // 255
                    pixels[x, y] = (r, g, b, a)
    
    def unmultiply(im):
        pixels = im.load()
        for y in range(im.size[1]):
            for x in range(im.size[0]):
                r, g, b, a = pixels[x, y]
                if a != 255 and a != 0:
                    r = 255 if r >= a else 255 * r // a
                    g = 255 if g >= a else 255 * g // a
                    b = 255 if b >= a else 255 * b // a
                    pixels[x, y] = (r, g, b, a)
    

    Result: result of premultiply, resize, unmultiply

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a BitmapData of 600x600 and I want to scale it down
say I want to scaffold generate university but university should eventually be related to
Say I have four sub-classes of 'Car'. One for each color. I want to
Let's say I have two animations that work individually, but I want them to
Say i want to develop a library that will keep a very small core
I want to plot color scale with grid package. Something like heat.plot (10). I
I want to set a background image to my Activity. I am just using
Let's say I have an Image class and I want to provide some operations
I want to scale down my fonts for the mozilla browser by say, 10%.
Let's say you want to include a third party css stylesheet, but it comes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.