GIF制作遇到不支持webp格式图片,需要把比较多的webp图片转换jpeg格式,自己用python学着写了代码批量转换,日常难得遇到这样的情况,做个笔记。
批量webp转jpg,反转只要修改相应代码。
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
| import os from pil import image
class proimage(): def \_\_init\_\_(self):
self.path = "/users/## #/desktop/1"
def read(self): filelist = os.listdir(self.path) return filelist def webp2jpg(self, filelist):
for item in filelist: if item.endswith('.webp'): src = os.path.join(os.path.abspath(self.path), item) print("src=", src) im = image.open(src) im.load() save\_name = src.replace('webp', 'jpg') im.save('{}'.format(save\_name), 'jpeg')
if \_\_name\_\_ == '\_\_main\_\_': newtype = proimage() filelist = newtype.read() newtype.webp2jpg(filelist)
|
4通道png图片转jpg
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
| import os from pil import image class proimage(): def \_\_init\_\_(self): self.path = "/users/## #/desktop/1"
def read(self): filelist = os.listdir(self.path) return filelist def png2jpg(self, filelist): for item in filelist: if item.endswith(".png"): src = os.path.join(os.path.abspath(self.path), item) im = image.open(src) im.load()
r, g, b, a= im.split() im = image.merge("rgb", (r, g, b))
save\_name = src.replace("png", "jpg") im.save('{}'.format(save\_name), 'jpeg') os.remove(src) if \_\_name\_\_ == '\_\_main\_\_': newtype = proimage() filelist=newtype.read() newtype.png2jpg(filelist)
|
批量图片重新编号命名
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
| import os from pil import image class proimage(): def \_\_init\_\_(self):
self.path = "/users/## #/desktop/2"
def read(self): filelist = os.listdir(self.path) return filelist def rename(self, filelist): i = 1 for item in filelist:
if item.endswith('.jpg') or item.endswith('.jpeg') or item.endswith('.jpg'): src = os.path.join(os.path.abspath(self.path), item)
dst = os.path.join(os.path.abspath(self.path), '0' + format(str(i) + '爱尔兰', '0>3s') + '.jpg') os.rename(src, dst)
i = i + 1 if \_\_name\_\_ == '\_\_main\_\_': newtype = proimage() filelist = newtype.read() newtype.rename(filelist)
|