mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Rearranged used_palette_colors to fix get_local_header
This commit is contained in:
		
							parent
							
								
									aa1368f551
								
							
						
					
					
						commit
						f028928b5a
					
				| 
						 | 
					@ -332,8 +332,12 @@ def get_local_header(fp, im, offset=(0, 0)):
 | 
				
			||||||
        transparency = int(transparency)
 | 
					        transparency = int(transparency)
 | 
				
			||||||
        # optimize the block away if transparent color is not used
 | 
					        # optimize the block away if transparent color is not used
 | 
				
			||||||
        transparent_color_exists = True
 | 
					        transparent_color_exists = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if _get_optimize(im, im.encoderinfo):
 | 
				
			||||||
 | 
					            used_palette_colors = _get_used_palette_colors(im)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # adjust the transparency index after optimize
 | 
					            # adjust the transparency index after optimize
 | 
				
			||||||
        if used_palette_colors is not None and len(used_palette_colors) < 256:
 | 
					            if len(used_palette_colors) < 256:
 | 
				
			||||||
                for i in range(len(used_palette_colors)):
 | 
					                for i in range(len(used_palette_colors)):
 | 
				
			||||||
                    if used_palette_colors[i] == transparency:
 | 
					                    if used_palette_colors[i] == transparency:
 | 
				
			||||||
                        transparency = i
 | 
					                        transparency = i
 | 
				
			||||||
| 
						 | 
					@ -433,11 +437,26 @@ def _save_netpbm(im, fp, filename):
 | 
				
			||||||
# --------------------------------------------------------------------
 | 
					# --------------------------------------------------------------------
 | 
				
			||||||
# GIF utilities
 | 
					# GIF utilities
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _get_optimize(im, info):
 | 
				
			||||||
 | 
					    return im.mode in ("P", "L") and info and info.get("optimize", 0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def _get_used_palette_colors(im):
 | 
				
			||||||
 | 
					    used_palette_colors = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # check which colors are used
 | 
				
			||||||
 | 
					    i = 0
 | 
				
			||||||
 | 
					    for count in im.histogram():
 | 
				
			||||||
 | 
					        if count:
 | 
				
			||||||
 | 
					            used_palette_colors.append(i)
 | 
				
			||||||
 | 
					        i += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return used_palette_colors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def getheader(im, palette=None, info=None):
 | 
					def getheader(im, palette=None, info=None):
 | 
				
			||||||
    """Return a list of strings representing a GIF header"""
 | 
					    """Return a list of strings representing a GIF header"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    optimize = info and info.get("optimize", 0)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Header Block
 | 
					    # Header Block
 | 
				
			||||||
    # http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
 | 
					    # http://www.matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp
 | 
				
			||||||
    header = [
 | 
					    header = [
 | 
				
			||||||
| 
						 | 
					@ -459,15 +478,8 @@ def getheader(im, palette=None, info=None):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    used_palette_colors = palette_bytes = None
 | 
					    used_palette_colors = palette_bytes = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if im.mode in ("P", "L") and optimize:
 | 
					    if _get_optimize(im, info):
 | 
				
			||||||
        used_palette_colors = []
 | 
					        used_palette_colors = _get_used_palette_colors(im)
 | 
				
			||||||
 | 
					 | 
				
			||||||
        # check which colors are used
 | 
					 | 
				
			||||||
        i = 0
 | 
					 | 
				
			||||||
        for count in im.histogram():
 | 
					 | 
				
			||||||
            if count:
 | 
					 | 
				
			||||||
                used_palette_colors.append(i)
 | 
					 | 
				
			||||||
            i += 1
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # create the new palette if not every color is used
 | 
					        # create the new palette if not every color is used
 | 
				
			||||||
        if len(used_palette_colors) < 256:
 | 
					        if len(used_palette_colors) < 256:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user