The Python solution ghost image appears if the background is transparent gif image composition with PNG when PIL

Recently when a PIL synthetic PNG image as a GIF, because of the need transparent background, so replace it method putpixel of a transparent background, but in the synthesis of GIF, images appear ghosting, the Internet to find a GIF relevant information: GIF related information  that has instructions for processing frames of GIF, needs to be set in the header disposal disposal GIF image

 

 Then we can see the PIL library definition files GifImagePlugin.py about the GIF

 1 disposal = int(im.encoderinfo.get("disposal", 0))
 2  
 3     if transparent_color_exists or duration != 0 or disposal:
 4         packed_flag = 1 if transparent_color_exists else 0
 5         packed_flag |= disposal << 2
 6         if not transparent_color_exists:
 7             transparency = 0
 8  
 9         fp.write(
10             b"!"
11             + o8(249)  # extension intro
12             + o8(4)  # length
13             + o8(packed_flag)  # packed fields
14             + o16(duration)  # duration
15             + o8(transparency)  # transparency index
16             + o8(0)

One of the paragraphs of the definition of Disposal, if you do not set the property disposal when you save it defaults to 0, ie without using processing method, so each one PNG files are retained become the background, that is, we see the ghost, so we in the synthesis of GIF, disposal value needs to be set

Original:

 

 The following is a transparent background PNG image code generation:

. 1  from the PIL Import Image
 2   
. 3 IMG = Image.open ( " dabai.gif " )
 . 4  the try :
 . 5      In Flag = 0
 . 6      the while True:
 . 7          # acquired per frame 
. 8          img.seek (In Flag) 
 . 9          # save 
10          img.save ( " pics / {}. PNG " .format (In Flag))
 . 11          PIC = Image.open ( " pics / {}. PNG " .format (In Flag))
 12 is          # conversion 
13         = pic.convert PIC ( " the RGBA " )
 14          # replaced with a transparent background 
15          Color = pic.getpixel ((0,0))
 16          for I in Range (pic.size [0]):
 . 17              for J in Range (PIC .size [. 1 ]):
 18 is                  DOT = (I, J)
 . 19                  RGBA = pic.getpixel (DOT)
 20 is                  IF RGBA == Color:
 21 is                      RGBA RGBA = [: -. 1] + (0,)
 22 is                      pic.putpixel ( DOT, RGBA)
 23          # save
24         pic.save("pics/{}.png".format(flag))
25         flag +=1
26 except BaseException as e:
27     pass

GIF image synthesis:

. 1  # ! / Usr / bin / Python the env 
2  # - * - Coding: UTF-. 8 - * - 
. 3   
. 4  from the PIL Import Image
 . 5  Import OS
 . 6 photo_list = []
 . 7  # gets saved PNG image 
. 8 pic_list = the os.listdir ( " pics / " )
 . 9  # sort image List, to prevent the image confusion position 
10 pic_list.sort (Key = the lambda X: int (X [: -. 4 ]))
 . 11  for K in pic_list:
 12 is      pic_p Image.open = ( " pics / {} ".format (K))
 13 is      photo_list.append (pic_p)
 14  # save image, Disposal 2 or 3 may be, but is not 0 or 1, remember, other custom untried 
15 photo_list [0] .save ( " dabai_new. GIF " , SAVE_ALL = True, append_images photo_list = [. 1:], DURATION = 40, Transparency = 0, Loop = 0, = Disposal. 3)

After the composite image, and the background has become a ghost does not appear transparent

Guess you like

Origin www.cnblogs.com/orange-wrj/p/12154032.html