mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-03-17 05:40:25 +00:00
17 lines
391 B
Python
17 lines
391 B
Python
""" ImageKit utility functions """
|
|
|
|
import tempfile
|
|
|
|
|
|
def img_to_fobj(img, format, **kwargs):
|
|
tmp = tempfile.TemporaryFile()
|
|
|
|
# Preserve transparency if the image is in Pallette (P) mode.
|
|
if img.mode == 'P':
|
|
kwargs['transparency'] = len(img.split()[-1].getcolors())
|
|
else:
|
|
img.convert('RGB')
|
|
|
|
img.save(tmp, format, **kwargs)
|
|
tmp.seek(0)
|
|
return tmp
|