mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-31 16:07:30 +03:00 
			
		
		
		
	Moved batch processing example under "More on reading images"
This commit is contained in:
		
							parent
							
								
									d455abffee
								
							
						
					
					
						commit
						946571d4a3
					
				|  | @ -406,33 +406,6 @@ Using the ImageSequence Iterator class | ||||||
|         # ...do something to frame... |         # ...do something to frame... | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| Batch processing with pathlib |  | ||||||
| ----------------------------- |  | ||||||
| 
 |  | ||||||
| This example uses Pillow together with pathlib, in order to reduce the quality of all PNG images in a folder: |  | ||||||
| 
 |  | ||||||
| :: |  | ||||||
| 
 |  | ||||||
|     from pathlib import Path |  | ||||||
|     from PIL import Image |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     def compress_image(source_path, dest_path): |  | ||||||
|         with Image.open(source_path) as img: |  | ||||||
|             if img.mode != "RGB": |  | ||||||
|                 img = img.convert("RGB") |  | ||||||
|             img.save(dest_path, "JPEG", optimize=True, quality=80) |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
|     base_directory = Path.cwd() |  | ||||||
| 
 |  | ||||||
|     for path in base_directory.iterdir(): |  | ||||||
|         if path.suffix == ".png": |  | ||||||
|             print(path) |  | ||||||
|             compress_image(path, filepath.stem + ".jpg") |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| PostScript printing | PostScript printing | ||||||
| ------------------- | ------------------- | ||||||
| 
 | 
 | ||||||
|  | @ -520,6 +493,33 @@ Reading from a tar archive | ||||||
|     fp = TarIO.TarIO("Tests/images/hopper.tar", "hopper.jpg") |     fp = TarIO.TarIO("Tests/images/hopper.tar", "hopper.jpg") | ||||||
|     im = Image.open(fp) |     im = Image.open(fp) | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  | Batch processing | ||||||
|  | ^^^^^^^^^^^^^^^^ | ||||||
|  | 
 | ||||||
|  | This example uses Pillow together with pathlib, in order to reduce the quality of all PNG images in a folder: | ||||||
|  | 
 | ||||||
|  | :: | ||||||
|  | 
 | ||||||
|  |     from pathlib import Path | ||||||
|  |     from PIL import Image | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     def compress_image(source_path, dest_path): | ||||||
|  |         with Image.open(source_path) as img: | ||||||
|  |             if img.mode != "RGB": | ||||||
|  |                 img = img.convert("RGB") | ||||||
|  |             img.save(dest_path, "JPEG", optimize=True, quality=80) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     base_directory = Path.cwd() | ||||||
|  | 
 | ||||||
|  |     for path in base_directory.iterdir(): | ||||||
|  |         if path.suffix == ".png": | ||||||
|  |             print(path) | ||||||
|  |             compress_image(path, filepath.stem + ".jpg") | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| Controlling the decoder | Controlling the decoder | ||||||
| ----------------------- | ----------------------- | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user