2018-09-10 13:59:06 8 Comments
In Python 3.x, I am using PIL to resize images, I know that we can reduce the height or width by subtraction or division by pixels. But, is it possible to resize an image to a desired size, say 200kb and remain its proportions? Assuming the image(s) is larger but the size is unknown.
Related Questions
Sponsored Content
34 Answered Questions
[SOLVED] How do I sort a dictionary by value?
- 2009-03-05 00:49:05
- Gern Blanston
- 2505195 View
- 3424 Score
- 34 Answer
- Tags: python sorting dictionary
42 Answered Questions
[SOLVED] How do I merge two dictionaries in a single expression?
- 2008-09-02 07:44:30
- Carl Meyer
- 1689614 View
- 4371 Score
- 42 Answer
- Tags: python dictionary merge
61 Answered Questions
28 Answered Questions
37 Answered Questions
[SOLVED] How do I check whether a file exists without exceptions?
- 2008-09-17 12:55:00
- spence91
- 3727147 View
- 5306 Score
- 37 Answer
- Tags: python file file-exists
21 Answered Questions
18 Answered Questions
[SOLVED] CSS Display an Image Resized and Cropped
- 2009-01-29 20:35:19
- InfoStatus
- 711122 View
- 321 Score
- 18 Answer
- Tags: html css background-image image
13 Answered Questions
43 Answered Questions
[SOLVED] Strange out of memory issue while loading an image to a Bitmap object
- 2009-01-25 11:23:37
- Chrispix
- 602829 View
- 1253 Score
- 43 Answer
- Tags: android image bitmap out-of-memory android-bitmap
25 Answered Questions
[SOLVED] How can I safely create a nested directory?
- 2008-11-07 18:56:45
- Parand
- 2459379 View
- 3928 Score
- 25 Answer
- Tags: python exception path directory operating-system
1 comments
@Mark Setchell 2018-09-11 17:28:30
I am still learning Python, so there may be better ways, but here is a function that saves a PIL/Pillow image as a JPEG and allows you to specify a maximum size.
It uses a binary search to minimise the amount of work needed and it encodes into
BytesIO
memory buffer to save writing images to disk. If anyone has any suggestions for improvements, please let me know!If I run that as is, with target size of 100,000 bytes, I get:
If I change the target size to 50,000 bytes, I get:
Keywords: Python, PIL, Pillow, JPEG, quality, quality setting, max size, maximum size, image, image processing, binary search.
@moomoochen 2018-09-12 03:21:07
I got an error from your else statement, "ERROR: No acceptble quality factor found" What did I do wrong?
@Mark Setchell 2018-09-12 06:40:57
I can't tell for sure without seeing your image and the target size you specified. If you tried to reduce, say, an 8,000x6,000 pixel image to 5,000 bytes it will have had difficulties. Please share your image and target size, or try increasing the target size to give it a better chance of success.
@Mark Setchell 2018-09-20 15:54:55
Can you clarify what size (in pixels) of image you tried to compress with this please and what target size you specified - because I have tried it successfully on many files.
@moomoochen 2018-09-25 01:39:08
sorry for my late response, may I know what IDE you're using? and what OS you're using? Thank you so much :)
@Mark Setchell 2018-09-25 06:20:18
I don't use an IDE, I just type the code into an editor (
vi
, not that it makes any difference) and save it and run it. I tested the code on a Mac and under debian. You know the formatting (indentation) is critical, don't you?