본문 바로가기
Programming/iOS

iPhone 4용 이미지 라시이징 스크립트 #2

by 신규하 2010. 12. 13.



iPhone 4용 이미지 리사이징 스크립트...





위 글을 포스팅하고 나서, 쓰다가 불편해서 약간 수정한 버전도 올립니다.

첫번째는 이전 스크립트를 개선한 버전으로 하위 폴더까지 뒤져서 파일이름을 변경해 줍니다.

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################################################
# iPhone 4 용 이미지 파일 리사이징
# 실행 전에 이미지 라이브러리 설치 필요
#     http://www.pythonware.com/products/pil/index.htm
# 이미지 라이브러리 설치 방법
#    python setup.py install
#####################################################################
import sys
import string
import os
import Image

def convertFile(dirpath, filename):
    path = dirpath+'/'+filename
    s =  os.path.splitext(path)
    if s[1] == '.png' and  path.find('@2x') < 0 :
        rename = s[0]+'@2x'+s[1]
        if os.path.exists(rename):
            return 0
        print path+'\t=>\t'+rename
        img = Image.open(path)
        img.save(rename, options='optimize')
        #img = img.resize((img.size[0] / 2, img.size[1] / 2), Image.BILINEAR)
        img = img.resize((img.size[0] / 2, img.size[1] / 2), Image.ANTIALIAS)
        os.remove(path)
        img.save(path, options='optimize')
        return 1

convertCount = 0;
totalFileCount = 0;
for dirpath, dirnames, filenames in os.walk("."):
    if dirpath.find('.svn') > 0:
        continue
    for filename in filenames:
        if convertFile(dirpath, filename):
            convertCount = convertCount + 1
        totalFileCount = totalFileCount + 1
print '========================================'
print '   Total File(s) :'+str(totalFileCount)
print ' Convert File(s) :'+str(convertCount)
print '========================================'

25, 26행에 보시면, 이미지 리사이징 중 축소 방법인데.. 마음에 드시는 것으로 쓰시면 됩니다.

아래는 2번째 스크립트 입니다.
이 스크립트는 파일 이름을 원래대로 복원해 줍니다.
잘 쓰게 되진 않지만, 혹시 저장소 아끼거나, 패치 돌려서 할 일이 쓸만 할 것 같아서 제작 했습니다.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import string
import os
import Image

def convertFile(dirpath, filename):
    path = dirpath+'/'+filename
    s =  os.path.splitext(path)
    if s[1] == '.png' and  path.find('@2x') > 0 :
        rename = path.replace('@2x', '')
        if os.path.exists(rename):
            print "Revert Filename : " + rename
            os.remove(rename)
            os.rename(path, rename)
            return 1
        return 0

convertCount = 0;
totalFileCount = 0;
for dirpath, dirnames, filenames in os.walk("."):
    if dirpath.find('.svn') > 0:
        continue
    for filename in filenames:
        if convertFile(dirpath, filename):
            convertCount = convertCount + 1
        totalFileCount = totalFileCount + 1
print '========================================'
print '   Total File(s) :'+str(totalFileCount)
print ' Convert File(s) :'+str(convertCount)
print '========================================'

사용 방법은 이전과 동일 합니다.
그럼 잘 사용하세요.

참고

Lion에서 Python PIL 사용하기



댓글