![]() ![]() However, let's say you HAVE to bring the image size <= 100 kb, no matter what. Image.save("output_file.jpg", "JPEG", quality=95) Image = image.resize((WIDTH, HEIGHT)) #smaller width and height than the original Resizing an image, storing it as a JPEG and reducing the quality to 95 saves up a lot of bytes on the final output: image = Image.open("input_file.png") This_image.save("path\where\to_save\your_image.jpg",quality=50) This_image = Image.open("path\to\your_image.jpg") Without django foo.py: from PIL import Image This_image = this_image.resize((int(TARGET_WIDTH),int(new_height)),Image.ANTIALIAS) Image = models.ImageField(upload_to='theme_image/') ![]() In django models.py after image saved, it will be proccessed again from PIL import Image You can change TARGET_WIDTH for your required width There are two options, they are doing the same logic, first one is how i did in django project, second is on pure python This script will reduce your image's width and height, with saving it's proportion, and reducing size also I prefer quality 85 with optimize because the quality isn't affected much, and the file size is much smaller. Using a quality of 75 (default if argument is left out) would yield: Using a quality of 85 instead of 95 in this case would yield: Now to try and get it down to 5kb to 10 kb, you can change the quality value in the save options. ![]() 1.9kb might not seem like much, but over hundreds/thousands of pictures, it can add up. The optimize flag will do an extra pass on the image to find a way to reduce its size as much as possible. # downsize the image with an ANTIALIAS filter (gives the highest quality)įoo = foo.resize((160,300),Image.ANTIALIAS)įoo.save('path/to/save/image_scaled.jpg', quality=95) # The saved downsized image size is 24.8kbįoo.save('path/to/save/image_scaled_opt.jpg', optimize=True, quality=95) # The saved downsized image size is 22.9kb from PIL import Imageįoo = Image.open('path/to/image.jpg') # My image is a 200x374 jpeg that is 102kb large A built-in parameter for saving JPEGs and PNGs is optimize. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |