Last active
October 15, 2023 14:05
-
-
Save mozbugbox/10cd35b2872628246140 to your computer and use it in GitHub Desktop.
Python Pillow image to/from GdkPixbuf with gobject introspection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# vim:fileencoding=utf-8:sw=4:et | |
# Convert between pygobject Pixbuf and PIL/Pillow image format | |
# Also a function to do fast gamma correction with Pillow image | |
from __future__ import print_function, unicode_literals, absolute_import | |
import sys | |
from gi.repository import GLib, GdkPixbuf | |
from PIL import Image | |
def pixbuf2image(pix): | |
"""Convert gdkpixbuf to PIL image""" | |
data = pix.get_pixels() | |
w = pix.props.width | |
h = pix.props.height | |
stride = pix.props.rowstride | |
mode = "RGB" | |
if pix.props.has_alpha == True: | |
mode = "RGBA" | |
im = Image.frombytes(mode, (w, h), data, "raw", mode, stride) | |
return im | |
def image2pixbuf(im): | |
"""Convert Pillow image to GdkPixbuf""" | |
data = im.tobytes() | |
w, h = im.size | |
data = GLib.Bytes.new(data) | |
pix = GdkPixbuf.Pixbuf.new_from_bytes(data, GdkPixbuf.Colorspace.RGB, | |
False, 8, w, h, w * 3) | |
return pix | |
def do_gamma(im, gamma): | |
"""Fast gamma correction with PIL's image.point() method""" | |
invert_gamma = 1.0/gamma | |
lut = [pow(x/255., invert_gamma) * 255 for x in range(256)] | |
lut = lut*3 # need one set of data for each band for RGB | |
im = im.point(lut) | |
return im | |
def test_i2p(infile, outfile): | |
im = Image.open(infile) | |
pix = image2pixbuf(im) | |
pix.savev(outfile, "png", (), ()) | |
def test_p2i(infile, outfile): | |
pix = GdkPixbuf.Pixbuf.new_from_file(infile) | |
im = pixbuf2image(pix) | |
# FIXME: We also test gamma correction here | |
im = do_gamma(im, 1.8) | |
im.save(outfile) | |
def main(): | |
infile = sys.argv[1] | |
outfile = "pixpil-test.png" | |
test_p2i(infile, outfile) | |
#test_i2p(infile, outfile) | |
print("result saved to {}".format(outfile)) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi mozbugbox. There seems to be an issue in GDK such that your image2pixbuf will indirectly result in a memory leak. Christopher Reiter from gitlab.gnome.org/GNOME/pygobject helped me understand the details and suggested this easy workaround, which I thought you probably want to know: replace pixbuf by pixbuf.copy() either inside or outside.
Here is a short script to highlight the problem:
Best wishes.
[details: https://gitlab.gnome.org/GNOME/pygobject/issues/225]