Why is it not working anymore in the 2 step process shown above? #Export/ Save: SaveAnimationFunction( ExportFrames, newfilepathname, formatname, extension, disposalID, FPS, savepath)Įven though I run through every frame stored in "ExportFrames" and convert it to palette mode using your custom function, it still creates the previous result with reduced colors. ![]() #Set Up Export Settings: newfilepathname = "GIF before import " formatname = "animation" extension = ".gif" disposalID = 2 FPS = 30 savepath =( 'GIF before import.gif',) ![]() #Convert Gif Frames into modifiable RGBA PIL images: tempimage = Img. save( newfilepathname + formatname + extension, disposal = disposalID, save_all = True, #Export and Save Gif Function: def SaveAnimationFunction( ExportFrames, newfilepathname, formatname, extension, disposalID, FPS, savepath):ĭurationFrame = 1000 / FPS if extension = ".gif":ĮxportFrames. #Showcase Bug: from PIL import Image as Img #Convert To "P" Gif 255 color Pallette mode: def convertToP( im): #Convert Gif Frames into PIL images: tempimage = Img. info = transparency for x, y in transparent_pixels: colors) < 256:Ĭolor = ( color + 1, color, color)Ĭolor = ( color, color + 1, color) # There are 256 colors or less in this image p = Img. So I still need help with this, I wasn't able to figure it out so far and understand why the loss occurs in different situations.įrom PIL import Image as Img def convertToP( im): I do modifications on the frames before saving them back as GIF, so generally I need to convert them to RGBA (for gifs with transparent backgrounds). Resulting in all the frames having reduced quality:īut sample 2 now doesn't have quality loss after saving with this code: convert( 'RGBA')) #CONVERTING TO RGBA if durationFrame = None: ![]() open( "GIF before import.gif")ĮxportFrames. RGB_AFTER_DIFFERENT_PALETTE_ONLY #Convert Gif Frames into PIL images: tempimage = Img. (I tried to convert from RGBA to RGB before saving the GIF file but it didn't change anything).įrom PIL import Image as Img from PIL import GifImagePlugin as GifPl GifPl. Now, if I convert to RGBA, and set GifImagePlugin.LOADING_STRATEGY as you mentioned in the sample code, then sample 2 does NOT lose quality and get quantized, however sample 1 DOES. Overall, I will need to convert all frames into RGBA, in no matter what case, even if I don't paste unto an empty image. Unfortunately I'm not able to recreate the issue, it didn't happen with all gifs, and it's been a few months back, when I worked on this project and solved it this way. Would you consider if more helpful to talk about this instead? If you wanted to post an image and example code? However I was not able to make it work in my main project (where Main Code, import functions, and export functions are in separate scripts), no matter if I added the line to all the scripts or only the import script. Let us know if that isn't sufficient for your situation.Īdding this loading strategy in the beginning of the sample code snippet I created worked to solve the first sample issue. GifImagePlugin.LOADING_STRATEGY = _AFTER_DIFFERENT_PALETTE_ONLYĪt the start of your code resolves the matter. How can I stop and control the quality loss in both of these cases? Why is this happening? Is this a potential bug? Running convert('P') on every PIL frame image did not solve the issue. I also attach the sample images and python scripts above here so anyone can recreate the issue: #Add Alpha Channel to all gifs in order to avoid erroneous first frames: for x in range( 0, len( ExportFrames)):ĮxportFrames = CreateFrameWithAlphaValue( ExportFrames, ExportFrames. ![]() Return edited_frame #Convert Gif Frames into PIL images: tempimage = Img. new( 'RGBA', ( width, height))Įdited_frame. This suggests that error happens during GIF export.įrom PIL import Image as Img #Correct the first frame of gif animations: def CreateFrameWithAlphaValue( CurrentImage, size): However, even with the step added, there is no quality loss when exporting the frames as single png images instead as a gif animation: This quality loss does only occur when adding the step. The original GIF image that I import as PIL formatted images:Īfter saving, there is an extreme quality loss: However with it this quality loss occurs: I added this additional step of pasting each frame unto a transparent alpha PIL image, because for some GIFs, when I imported them and then saved them, the first main frame of the GIF would not have a transparent background. save( "GIF after export.gif", disposal = 2, save_all = True,Īppend_images = ExportFrames, loop = 0,ĭuration = durationFrame, optimize = False, lossless = True)Ĭase 2: Adding Alpha Channel to PIL images, before saving as gif: open( "GIF before import.gif")ĭurationFrame = None for i in range( 0, tempimage. From PIL import Image as Img #Convert Gif Frames into PIL images: tempimage = Img.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |