mirror of
				https://github.com/LonamiWebs/Telethon.git
				synced 2025-10-30 23:47:33 +03:00 
			
		
		
		
	Prevent pillow from closing non-exclusive fps (#1121)
This commit is contained in:
		
							parent
							
								
									ae8f1fed05
								
							
						
					
					
						commit
						fcfebf75a3
					
				|  | @ -44,29 +44,31 @@ def _resize_photo_if_needed( | ||||||
|         file = io.BytesIO(file) |         file = io.BytesIO(file) | ||||||
| 
 | 
 | ||||||
|     try: |     try: | ||||||
|         with PIL.Image.open(file) as image: |         # Don't use a `with` block for `image`, or `file` would be closed. | ||||||
|             if image.width <= width and image.height <= height: |         # See https://github.com/LonamiWebs/Telethon/issues/1121 for more. | ||||||
|                 return file |         image = PIL.Image.open(file) | ||||||
|  |         if image.width <= width and image.height <= height: | ||||||
|  |             return file | ||||||
| 
 | 
 | ||||||
|             image.thumbnail((width, height), PIL.Image.ANTIALIAS) |         image.thumbnail((width, height), PIL.Image.ANTIALIAS) | ||||||
| 
 | 
 | ||||||
|             alpha_index = image.mode.find('A') |         alpha_index = image.mode.find('A') | ||||||
|             if alpha_index == -1: |         if alpha_index == -1: | ||||||
|                 # If the image mode doesn't have alpha |             # If the image mode doesn't have alpha | ||||||
|                 # channel then don't bother masking it away. |             # channel then don't bother masking it away. | ||||||
|                 result = image |             result = image | ||||||
|             else: |         else: | ||||||
|                 # We could save the resized image with the original format, but |             # We could save the resized image with the original format, but | ||||||
|                 # JPEG often compresses better -> smaller size -> faster upload |             # JPEG often compresses better -> smaller size -> faster upload | ||||||
|                 # We need to mask away the alpha channel ([3]), since otherwise |             # We need to mask away the alpha channel ([3]), since otherwise | ||||||
|                 # IOError is raised when trying to save alpha channels in JPEG. |             # IOError is raised when trying to save alpha channels in JPEG. | ||||||
|                 result = PIL.Image.new('RGB', image.size, background) |             result = PIL.Image.new('RGB', image.size, background) | ||||||
|                 result.paste(image, mask=image.split()[alpha_index]) |             result.paste(image, mask=image.split()[alpha_index]) | ||||||
| 
 | 
 | ||||||
|             buffer = io.BytesIO() |         buffer = io.BytesIO() | ||||||
|             result.save(buffer, 'JPEG') |         result.save(buffer, 'JPEG') | ||||||
|             buffer.seek(0) |         buffer.seek(0) | ||||||
|             return buffer |         return buffer | ||||||
| 
 | 
 | ||||||
|     except IOError: |     except IOError: | ||||||
|         return file |         return file | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user