Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reduce an image's depth using PIL?

Is it possible to reduce the depth of an image using PIL? Say like going to 4bpp from a regular 8bpp.

like image 289
Geo Avatar asked Oct 05 '09 16:10

Geo


People also ask

Is PIL and Pillow the same?

What is PIL/Pillow? PIL (Python Imaging Library) adds many image processing features to Python. Pillow is a fork of PIL that adds some user-friendly features.

How do I reduce the size of an image in Python?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.

How do you compress an image using Python and PIL?

getsize(image_name) # print the size before compression/resizing print("[*] Size before compression:", get_size_format(image_size)) if new_size_ratio < 1.0: # if resizing ratio is below 1.0, then multiply width & height with this ratio to reduce image size img = img. resize((int(img. size[0] * new_size_ratio), int(img.


2 Answers

You can easily convert image modes (just call im.convert(newmode) on an image object im, it will give you a new image of the new required mode), but there's no mode for "4bpp"; the modes supported are listed here in the The Python Imaging Library Handbook.

like image 186
Alex Martelli Avatar answered Sep 21 '22 05:09

Alex Martelli


This can be done using the changeColorDepth function in ufp.image module. this function only can reduce color depth(bpp)

import ufp.image
import PIL
im = PIL.Image.open('test.png')
ufp.image.changeColorDepth(im, 16) # change to 4bpp(this function change original PIL.Image object)
im.save('changed.png')
like image 44
c2o93y50 Avatar answered Sep 23 '22 05:09

c2o93y50