pillow_example.py
· 410 B · Python
Bruto
from PIL import Image
# 開啟影像
image = Image.open("example.jpg")
# 顯示影像
image.show()
# 轉換為灰階
gray_image = image.convert("L")
gray_image.save("example_gray.jpg")
# 調整大小
resized_image = image.resize((200, 200))
resized_image.save("example_resized.jpg")
# 旋轉影像
rotated_image = image.rotate(45)
rotated_image.save("example_rotated.jpg")
print("影像處理完成!")
| 1 | from PIL import Image |
| 2 | |
| 3 | # 開啟影像 |
| 4 | image = Image.open("example.jpg") |
| 5 | |
| 6 | # 顯示影像 |
| 7 | image.show() |
| 8 | |
| 9 | # 轉換為灰階 |
| 10 | gray_image = image.convert("L") |
| 11 | gray_image.save("example_gray.jpg") |
| 12 | |
| 13 | # 調整大小 |
| 14 | resized_image = image.resize((200, 200)) |
| 15 | resized_image.save("example_resized.jpg") |
| 16 | |
| 17 | # 旋轉影像 |
| 18 | rotated_image = image.rotate(45) |
| 19 | rotated_image.save("example_rotated.jpg") |
| 20 | |
| 21 | print("影像處理完成!") |
| 22 |