# Pillow
---
## Introduction
----
### Pillow
* PIL 的升級版(?!
* 許多**影像處理**的工具
----
### Download
```
$ pip install pillow
```
----
### 提供了十張貓貓照片
[Download Link](https://github.com/Fireball0424/Sprout-2023py-PillowPicture.git)
----
### 檔案配置
* main.py
* image
* test0~test9
* wanted
---
## 轉檔
----
``` python=
from PIL import Image
img = Image.open('image/test2.webp')
img.save('image/test2.png', 'png')
```
----
```python=
from PIL import Image
img = Image.open('image/test0.jpeg')
img.save('image/test0.pdf', 'pdf')
```
----
常見格式
GIF, JPEG, PNG, PDF
BMP, EPS, TIFF......
----
#### 一次想把所有A格式圖片轉成B格式
```python=
from PIL import Image
from glob import glob
from os.path import splitext
def convert(formatA = 'jpeg', formatB = 'png'):
formatA = str(formatA)
formatB = str(formatB)
path = "image/*." + formatA
convertList = glob(path)
for picPath in convertList:
img = Image.open(picPath)
imgPath = splitext(picPath)[0] + '.' + formatB
img.save(imgPath, formatB)
convert()
```
---
## 修改尺寸
----
### 取得圖片目前的尺寸
```python=
from PIL import Image
img = Image.open('image/test0.jpeg')
print(img.size) # a tuple
```
----
### Resize
```python=
from PIL import Image
img = Image.open('image/test0.jpeg')
print(img.size)
# 按比例縮放
weight = 200
ratio = float(weight) / img.size[0]
height =(int)(ratio * img.size[1])
newImg = img.resize((weight, height), Image.BILINEAR)
print(newImg.size)
newImg.save('image/test0_resize.jpeg', 'jpeg')
```
----
* NEAREST 最近插值法
* BILINEAR 雙線性內插 (default)
* BICUBIC 雙立方內插
* ANTILIAS 平滑
----
### JPEG壓縮大小
```python=
from PIL import Image
img = Image.open('image/test0.jpeg')
img.save('image/test0_low.jpeg', quality=60, subsampling=0)
```
quatlity [1 ~ 95]
subsampling [0, 1, 2]
----
### 更快的縮圖
```python=
from PIL import Image
img = Image.open('image/test0.jpeg')
img.thumbnail((200, 100))
img.save('image/test0_thumbnail.jpeg', 'jpeg')
```
自動選擇長寬中比較小的值自動按比例縮放
執行速度比resize快
---
## 圖片旋轉
----
### Flip
```python=
from PIL import Image
img = Image.open('image/test1.jpeg')
imgFlipLR = img.transpose(Image.FLIP_LEFT_RIGHT)
imgFlipUD = img.transpose(Image.FLIP_TOP_BOTTOM)
imgFlipLR.save('image/test1_flipLR.jpeg', 'jpeg')
imgFlipUD.save('image/test1_flipUD.jpeg', 'jpeg')
```
----
### Rotate
```python=
from PIL import Image
img = Image.open('image/test1.jpeg')
imgR90 = img.transpose(Image.ROTATE_90)
imgR180 = img.transpose(Image.ROTATE_180)
imgR270 = img.transpose(Image.ROTATE_270)
imgR90.save('image/test1_R90.jpeg', 'jpeg')
imgR180.save('image/test1_R180.jpeg', 'jpeg')
imgR270.save('image/test1_R270.jpeg', 'jpeg')
```
---
## 濾鏡
----
### 模糊
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.BLUR)
newImg.save('image/test3_blur.jpeg', 'jpeg')
```
----
### 增強細節
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.DETAIL)
newImg.save('image/test3_detail.jpeg', 'jpeg')
```
----
### 描出輪廓
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.CONTOUR)
newImg.save('image/test3_contour.jpeg', 'jpeg')
```
----
### 找出邊緣
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.FIND_EDGES)
newImg.save('image/test3_edge.jpeg', 'jpeg')
```
----
### 增強邊緣細節
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.EDGE_ENHANCE)
newImg2 = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
newImg.save('image/test3_edgeEnhance.jpeg', 'jpeg')
newImg2.save('image/test3_edgeEnhanceMore.jpeg', 'jpeg')
```
----
### 浮雕
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.EMBOSS)
newImg.save('image/test3_emboss.jpeg', 'jpeg')
```
----
### 平滑
```python=
from PIL import Image, ImageFilter
img = Image.open('image/test3.jpeg')
newImg = img.filter(ImageFilter.SMOOTH)
newImg2 = img.filter(ImageFilter.SMOOTH_MORE)
newImg.save('image/test3_smooth.jpeg', 'jpeg')
newImg2.save('image/test3_smoothMore.jpeg', 'jpeg')
```
---
## 兩張圖片(?
----
### 裁切
```python=
from PIL import Image
img = Image.open('image/test5.webp')
print(img.size)
newImg = img.crop((200, 300, 400, 600))
newImg.save('image/test5_crop.jpeg', 'jpeg')
```
----
crop(b1, a1, b2, a2)
![](https://hackmd.io/_uploads/Sk17-hLUn.png)
[Picture Source](https://blog.csdn.net/banxia1995/article/details/85330212)
----
### 貼上
```python=
from PIL import Image
wanted = Image.open("image/wanted.jpg")
print(wanted.size)
img = Image.open("image/test5_crop.jpeg")
wanted.paste(img, (140, 220))
wanted.save("image/test5_wanted.jpeg", "jpeg")
```
貼上圖片的左上角對齊座標
---
## 族繁不及備載
[PIL document](https://pillow.readthedocs.io/en/stable/)
---
## 套用到DC bot (?
----
### Hint
* 從歷史的近n筆對話,找最近的一張圖片
* 或者在一則訊息中同時有指令+圖片
* message.attachments
----
### Example : 將訊息中的圖片存下來
```python=
@commands.command()
async def Save(self, ctx):
path = 'cmds/image.jpeg'
try:
await ctx.message.attachments[0].save(path)
except:
await ctx.send("save fail")
else:
await ctx.send("save success")
```
----
### Example : 裁切成想要的大小
```python=
@commands.command()
async def Crop(self, ctx, x1, y1, x2, y2):
img = Image.open('cmds/image.jpeg')
newImg = img.crop((x1, y1, x2, y2))
newImg.save('cmds/image_crop.jpeg', 'jpeg')
await ctx.send(file = discord.File('cmds/image_crop.jpeg'))
```
{"metaMigratedAt":"2023-06-18T05:33:45.400Z","metaMigratedFrom":"YAML","title":"Pillow","breaks":true,"contributors":"[{\"id\":\"cc9d2950-af2b-4feb-8745-4367e4091769\",\"add\":5620,\"del\":0}]"}