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

Resize the width and height to 90% of original, but the image file size(bytes) become larger #6771

Closed
chucklu opened this issue Dec 1, 2022 · 6 comments

Comments

@chucklu
Copy link

chucklu commented Dec 1, 2022

What did you do?

resize the width and height of a image to 90%

What did you expect to happen?

I expect the file size(bytes) become smaller,

What actually happened?

The file size(bytes) become larger, the original file size is 80KB,
and the resized image file size become 700KB 89KB

What are your OS, Python and Pillow versions?

  • OS: win10 21H2
  • Python: Python 3.10.0
  • Pillow: '9.3.0' (PIL.version)
    im = Image.open(sourceFilePath)
    width, height = im.size
    width, height = int(width*0.9), int(height*0.9)
    im = im.resize((width, height))
    im.save(targetFilePath)

cat

@chucklu
Copy link
Author

chucklu commented Dec 1, 2022

My bad, my previous test case, open a jpg file, and resize it then save as png(different image type). Which make it from 80KB to 700KB

@chucklu
Copy link
Author

chucklu commented Dec 1, 2022

I also tried to open the jpg and save it as another jpg(same image type) with 90% width and height, and the file size changed from 80KB to 89KB. It's weird too.

@radarhere
Copy link
Member

radarhere commented Dec 1, 2022

You may already know this, but regarding saving it as a PNG,

#5069 (comment)

JPEGs are a lossy format, which means that perceptually, the image will be close to the original, but individual pixels and values may differ depending on implementations of the underlying jpeg library. If you need pixel for pixel identical results, you need to use a lossless compression method, like PNGs. Note that lossless compression usually results in images that are significantly larger than those using lossless compression.

#617 is an open issue that mentions PNG file size.

Regarding saving it as a JPG,
if I use im.save(targetFilePath, optimize=True) instead, the result is slightly smaller (but still larger than the original).
If I use im.save(targetFilePath, quality=72, optimize=True), then the result becomes a bit smaller than the original.

@chucklu
Copy link
Author

chucklu commented Dec 2, 2022

@radarhere Thanks for the information about the jpg is a lossy format, I don't know about this part.

I did the following test

 im.save(targetFilePath)  #from 80KB to 89KB
 im.save(targetFilePath,optimize=True)  #from 80KB to 85KB
 im.save(targetFilePath,quality=90,optimize=True) #from 80KB to 149KB
 im.save(targetFilePath,quality=72,optimize=True) #from 80KB to 79KB

By the way, how do you know that the quality should set to 72?
As I tried with quality 90, then the size became larger.
However quality value 72 works.

@radarhere
Copy link
Member

I just manually decreased the quality value until I found one that gave a smaller file size.

@chucklu
Copy link
Author

chucklu commented Dec 2, 2022

@radarhere Thanks very much, I appreciate your effort.

@chucklu chucklu closed this as completed Dec 2, 2022
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