Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Size of the image generated by putalpha is twice as big, how can this be solved? #6287

Closed
zhenzi0322 opened this issue May 12, 2022 · 6 comments

Comments

@zhenzi0322
Copy link

Example code:

from PIL import Image

def main():
    file = 'uploads/images/4fcf8e9d4b7d41549ee260428b535f7e.jpg'
    img = Image.open(file)

    alpha = Image.open('uploads/alpha/4fcf8e9d4b7d41549ee260428b535f7e.jpg')
    img.putalpha(alpha=alpha)

    save_args = {'format': 'PNG'}
    save_args['quality'] = 85
    save_args['optimize'] = True
    img.save('demo4.png', **save_args)
@radarhere
Copy link
Member

When you say "twice as big", twice as big as what? If you mean that it is twice as big as the JPEG that you opened, PNG is a lossless image format, while JPEG is a lossy format. It should be no surprise that the the PNG equivalent of a JPEG image is larger.

If you mean something else, please specify, and also kindly upload your test image so that we can replicate your situation more exactly.

@radarhere radarhere changed the title The size of the image generated by putalpha is twice as big, how can this be solved? Size of the image generated by putalpha is twice as big, how can this be solved? May 12, 2022
@radarhere
Copy link
Member

I presume you don't mean that it is twice as big as it would be without a call to putalpha - if I test your code with a JPEG from our test suite, the resulting PNG is approximately a third larger, not twice as large.
That change seems reasonable, as you would expect it to be adding another channel, moving from RGB to RGBA.

@zhenzi0322
Copy link
Author

Yes, there is also a problem that the speed is a bit slow, how to solve this kind of

@radarhere
Copy link
Member

If you are dealing with a large JPEG, that might slow your code down.

Removing save_args['optimize'] = True would speed your code up.

@radarhere
Copy link
Member

We already have #5986 open for PNG speed and #617 for the size of PNG files. So while I can answer specific questions, there are already issues open for the general nature of this problem.

@zhenzi0322
Copy link
Author

zhenzi0322 commented May 14, 2022

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants