python imaging library - Maintaining transparency with palette mode GIFs in Pillow -


i trying take gif palette containing 1 transparency index , use pillow create cropped sub-images. however, when using crop() method results no longer transparent.

original = image.open("filename.gif") print(original.mode) # prints "p", should transparent = original.info["transparency"] print(transparent) # prints correct index of transparent color cropped = original.crop((0, 0, 10, 10)) print(cropped.info) # transparency no longer present cropped.info["transparency"] = 255 print(cropped.info) # key entered, not transparent in drawn image 

how can maintain or restore transparent index through manipulations in pillow? shown above, if "brute-force" add transparency index "info" dictionary, not python looking index specified. documentation mentions methods crop() lazy , not transfer image information, there way re-add such information image object? documentation suggests can saving new gif file, won't need sub-images after program finished running , displaying them.

edited add below additional information:

the original image original image, made in gimp (circle red, marked transparent color using irfanview)

code output output of code, circle reverting visible red

my entire program here:

from tkinter import * tkinter import ttk pil import image pil import imagetk  class main:     def __init__(self):         self.root = tk()         self.background = canvas(self.root)         self.background.grid(column=0,row=0)         self.draw()      def draw(self):         original = image.open("transparency_test.gif")         print(original.mode) # prints "p", should         transparent = original.info["transparency"]         print(transparent) # prints correct index of transparent color         cropped = original.crop((0, 0, 50, 50))         print(cropped.info) # transparency no longer present          test_uncropped = imagetk.photoimage(image=original)         test_cropped = imagetk.photoimage(image=cropped)          self.background.create_image((0,0), image=test_uncropped, anchor=nw)         self.background.create_image((100,0), image=test_cropped, anchor=nw)          self.root.mainloop()  instance = main() 

i'm not sure efficient solution, got working making resized copy of image, , pasting pixels original image on it. think result expect.

    cropped = original.crop((0, 0, 50, 50))     cropped.load()     print(cropped.info) # transparency no longer present      copied = original.resize((50,50))     copied.paste(original, (0, 0))     print(copied.info) # transparency present      test_uncropped = imagetk.photoimage(image=original)     test_cropped = imagetk.photoimage(image=cropped)     test_copied = imagetk.photoimage(image=copied)      self.background.create_image((0,0), image=test_uncropped, anchor=nw)     self.background.create_image((100,0), image=test_cropped, anchor=nw)     self.background.create_image((200,0), image=test_copied, anchor=nw) 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -