|  | 
 
| 
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  复制代码from PIL import Image as ImagePIL
import os
def transfer(infile, outfile):
    im = ImagePIL.open(infile)
    im.save(outfile,dpi=(96.0,96.0)) ##96.0,96.0分别为想要设定的dpi值
 
if __name__ == '__main__':
    for root, dirs, files in os.walk("./图片/既有建筑物/龙坪/"):  ##ori_img为需要修改的图片存储的文件夹名字
        for item in files:
            list = item.split(".")
            print(list[-1])
            if(list[-1] == "jpg"):
                new_name = list[0] + ".jpg"
                os.rename("./图片/既有建筑物/龙坪/" + item, "./图片/既有建筑物/龙坪/" + new_name)
                print(new_name)
                transfer("./图片/既有建筑物/龙坪/" + new_name, "./图片/既有建筑物/龙坪1/" + new_name)
 这是百度到的,一直运行不了,出错。错误内容如下
 jpg
 01009119cd584bc9eb0b968cc440ac8.jpg
 Traceback (most recent call last):
 File "D:\python3.8\lib\site-packages\PIL\JpegImagePlugin.py", line 633, in _save
 rawmode = RAWMODE[im.mode]
 KeyError: 'RGBA'
 
 The above exception was the direct cause of the following exception:
 
 Traceback (most recent call last):
 File "C:\Users\15125\Desktop\降低分辨率.py", line 16, in <module>
 transfer("./图片/既有建筑物/龙坪/" + new_name, "./图片/既有建筑物/龙坪1/" + new_name)
 File "C:\Users\15125\Desktop\降低分辨率.py", line 5, in transfer
 im.save(outfile,dpi=(96.0,96.0)) ##96.0,96.0分别为想要设定的dpi值
 File "D:\python3.8\lib\site-packages\PIL\Image.py", line 2172, in save
 save_handler(self, fp, filename)
 File "D:\python3.8\lib\site-packages\PIL\JpegImagePlugin.py", line 635, in _save
 raise OSError(f"cannot write mode {im.mode} as JPEG") from e
 OSError: cannot write mode RGBA as JPEG
 >>>
 | 
 |