-
Notifications
You must be signed in to change notification settings - Fork 0
/
photoresize.py
44 lines (29 loc) · 1.38 KB
/
photoresize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from PIL import Image
import os.path
import glob
import cv2
import time
target = r"C:\Users\OMEN\Downloads\horse2zebra\trainA\*.jpg" #待转换的文件路径中所有.jpg文件
save_path = r"C:\Users\OMEN\Downloads\horse2zebra\32_trainA" #转换格式后的文件存储路径
def convertjpg_scale(jpgfile,outdir): #设置图像缩放规格
try:
img = cv2.imread(jpgfile)
new_img = cv2.resize(img,None,fx=0.2, fy=0.2, interpolation = cv2.INTER_CUBIC)
cv2.imwrite(os.path.join(outdir,os.path.basename(jpgfile)), new_img)
except Exception as e:
print(e)
def convertjpg(jpgfile,outdir,width=32,height=32): #设置图像尺寸
img=Image.open(jpgfile).convert("RGB")
# print(type(img)) #<class 'PIL.JpegImagePlugin.JpegImageFile'>
# print(img.size) #(256,256)
# time.sleep(1000)
try:
new_img=img.resize((width,height),Image.Resampling.BILINEAR)
new_img.save(os.path.join(outdir,os.path.basename(jpgfile)))
except Exception as e:
print(e)
# test
# for jpgfile in glob.glob(target):
# convertjpg_scale(jpgfile,save_path)
for jpgfile in glob.glob(target): #待转换的文件路径中所有.jpg文件
convertjpg(jpgfile,save_path) #转换格式后的文件存储路径