Python批量修改图片格式和尺寸

2023-02-27,,,,

Python批量修改图片格式尺寸

备注:

1.导入了PIL库,是处理图片用的,很强大;

2.导入了的win32库,是判断隐藏文件用的,我们的项目需要删除隐藏文件,不需要的可以直接找到删除。

3.导入send2trash库,是把删除的文件放进垃圾箱,而不是永久删除,这个我只是防止删除有用的文件而搞得,有点严谨了是吧,不需要的可以删掉啊。

4.我这个脚本是Python2.7编写的,但是在处理中文编码的时候非常恶心,尽管最后被我解决了,这个解决的方法,我随后会再单独写一篇,但是此刻我是建议大家不要用2.x版本的python了。据说3.x的版本的已经解决了编码的问题。希望大家听我的建议。

    #编码= utf-8的
    导入 系统
    导入 os,glob
    导入 平台
    导入 win32file,win32con
    从 PIL  导入 图像
    从 send2trash  导入 send2trash
    重装(SYS)
    sys.setdefaultencoding('utf-8' )
    #new_width = 2048
    #width = int(raw_input(“width U want:”))
    #imgslist = glob.glob(路径+'/ *。*')
    ShuiPing = “水平”
    ShiZhuang = “矢状”
    GuanZhuang = “冠状”
    def  Py_Log(_string):
    打印“----” + _string.decode('utf-8' )+ “----”
    def  is_windows_system():
    返回“窗口” 在 platform.system()
    def  is_hiden_file(file_Path):
    如果 is_windows_system():
    fileAttr = win32file.GetFileAttributes(file_Path)
    如果 fileAttr&win32con.FILE_ATTRIBUTE_HIDDEN:
    返回True
    返回False
    返回False
    def  remove_hidden_​​file(file_path):
    send2trash(FILE_PATH)
    打印“删除隐藏的文件路径:” + file_path
    def  astrcmp(str1,str2):
    返回 str1.lower()== str2.lower()
    def  resize_image(img_path):
    尝试:
    mPath,ext = os.path.splitext(img_path)
    如果 (astrcmp(ext,“.  png ” )  或astrcmp(ext,“.jpg” )):
    img = Image.open(img_path)
    (宽度,高度)= img.size
    if (width!= new_width):
    new_height = int(height * new_width / width)
    out = img.resize((new_width,new_height),Image.ANTIALIAS)
    new_file_name =  '%s%s'  %(mPath,ext)
    out.save(new_file_name,质量= 100 )
    Py_Log(“图片尺寸修改为:” + str(new_width))
    其他:
    Py_Log(“图片尺寸正确,未修改” )
    其他:
    Py_Log(“非图片格式” )
    除了 例外,e:
    打印 e
    #改变图片类型
    def  change_img_type(img_path):
    尝试:
    img = Image.open(img_path)
    img.save('new_type.png' )
    除了 例外,e:
    打印 e
    #处理远程图片
    def  handle_remote_img(img_url):
    尝试:
    request = urllib2.Request(img_url)
    img_data = urllib2.urlopen(request).read()
    img_buffer = StringIO.StringIO(img_data)
    img = Image.open(img_buffer)
    img.save('remote.jpg' )
    (宽度,高度)= img.size
    out = img.resize((200 ,height *  200  / width),Image.ANTIALIAS)
    out.save('remote_small.jpg' )
    除了 例外,e:
    打印 e
    def  rename_forder(forder_path):
    Py_Log(“------------ rename_forder --------------------------” )
    names = os.path.split(forder_path)
    尝试:
    如果(UNICODE(热水瓶)  中 的Unicode(名称[ 1 ],'GBK' )):
    os.rename(forder_path,名称[ 0 ] + “\\”+“ 01 ”)
    Py_Log(名称[ 1 ] + “ - >” + “01” )
    如果(unicode(石庄)  in  unicode(names [ 1 ],'gbk' )):
    os.rename(forder_path,名称[ 0 ] + “\\”+“ 02 ”)
    Py_Log(名称[ 1 ] + “ - >” + “02” )
    如果(unicode(管庄)  in  unicode(names [ 1 ],'gbk' )):
    os.rename(forder_path,名称[ 0 ] + “\\”+“ 03 ”)
    Py_Log(名称[ 1 ] + “ - >” + “03” )
    除了 例外,e:
    打印 e
    def  BFS_Dir(dirPath,dirCallback =  None ,fileCallback =  None ):
    队列= []
    ret = []
    queue.append(dirPath);
    而 len(队列)>  0 :
    tmp = queue.pop(0 )
    如果(os.path.isdir(tmp)):
    ret.append(TMP)
    对于 项目  在 os.listdir(TMP):
    queue.append(os.path.join(tmp,item))
    如果 dirCallback:
    dirCallback(TMP)
    elif (os.path.isfile(tmp)):
    ret.append(TMP)
    如果 fileCallback:
    fileCallback(TMP)
    返回 RET
    def  DFS_Dir(dirPath,dirCallback =  None ,fileCallback =  None ):
    stack = []
    ret = []
    stack.append(dirPath);
    而 len(stack)>  0 :
    tmp = stack.pop(len(stack) -  1 )
    如果(os.path.isdir(tmp)):
    ret.append(TMP)
    对于 项目  在 os.listdir(TMP):
    stack.append(os.path.join(tmp,item))
    如果 dirCallback:
    dirCallback(TMP)
    elif (os.path.isfile(tmp)):
    ret.append(TMP)
    如果 fileCallback:
    fileCallback(TMP)
    返回 RET
    def  printDir(dirPath):
    打印“dir:”  + dirPath
    if (is_hiden_file(dirPath)):
    remove_hidden_​​file(dirPath)
    其他:
    rename_forder(dirPath)
    def  printFile(dirPath):
    打印“文件:”  + dirPath
    resize_image(dirPath)
    返回True
    如果 __name__ == ' __main__  ' :
    而真:
    path = raw_input(“Path:” )
    new_width = int(raw_input(“width U want:” ))
    尝试:
    b = BFS_Dir(path,printDir,printFile)
    Py_Log(“\ r \ n ********** \ r \ n” + “*********图片处理完毕*********” + “\ r \ n ********** \ r \ n“ )
    除了:
    打印“意外的错误:” ,sys.exc_info()
    raw_input('按回车键重新处理' )

 

Python批量修改图片格式和尺寸的相关教程结束。