天天看點

給自己的 圖檔處理工具 (py2exe)

工具為  dist/myImg.exe

工具使用:

在 dist 中

myImg.exe Ratio  c:/需要等比壓縮圖檔/*.jpg  d:/等比後路徑   等比款width  等比高height 

myImg.exe Watermark c:/水印圖檔/x.png  c:/需要加水印圖檔/*.jpg  d:/添加水印後圖檔   加水印width處  加水印height處

等比壓縮執行個體:

myImg.exe Ratio  

    "C:"Documents and Settings"lky"My Documents"My Dropbox"tools"img"src"python"dist"test"img"*.jpg" 

    "C:"Documents and Settings"lky"My Documents"MyDropbox"tools"img"src"python"dist"test"toimg2" 

    600 400

結果在  ../dist"test"toimg  目錄下 生成 等比圖檔 testratio_600_400.jpg

水印添加執行個體:

myImg.exe Watermark 

    "C:"Documents and Settings"lky"My Documents"My Dropbox"tools"img"src"python"dist"test"img"logo.png"   

    "C:"Documents and Settings"lky"My Documents"My Dropbox"tools"img"src"python"dist"test"toimg2"*.jpg" 

    "C:"Documents and Settings"lky"My Documents"My Dropbox"tools"img"src"python"dist"test"toimg3" 

    100 220

結果在  ../dist"test"toimg3  目錄下 生成 等比圖檔 test_ratio_600_400_watermark_100_220.jpg

安裝 python PIL 包

和安裝 python py2exe 

工具腳本 py 

工具編譯過程:

python setup.py py2exe 

import sys

def imgRatio(imgpath,tpath='.',width=600,height=400):

    import Image,os

    im = Image.open( imgpath )

    im.thumbnail( (width,height) )

    imgpath = os.path.split(imgpath)[1]

    if not os.path.exists(tpath) : os.makedirs(tpath)

    im.save( tpath+'/'+imgpath.split('.')[0]+'_ratio_'+str(width)+'_'+str(height)+'.jpg' )

def imgWatermark(imgwate,imgpath,sw=100,sh=50,tpath='.'):

    baseim = Image.open( imgpath )

    floatim = Image.open( imgwate )

    baseim.paste( floatim, (sw,sh ) )

    baseim.save( tpath+'/'+imgpath.split('.')[0]+'_watermark_'+str(sw)+'_'+str(sh)+'.jpg' )

args = sys.argv

import glob,os

# Ratio  /path/*.jpg  /tpath   width  height  

if args[1] == 'Ratio' :

    for img in glob.glob(args[2]) :

        imgRatio(os.path.abspath(img),args[3],int(args[4]),int(args[5]))

# Watermark /path/x.png  /path/*.jpg  /tpath   width  height 

if args[1] == 'Watermark' :

    for img in glob.glob(args[3]) :

        imgWatermark(args[2],img,int(args[5]),int(args[6]),args[4])

py2exe

#!/usr/bin/python

# -*- coding: utf-8 -*-

#  py2exe file

# 1.install py2exe application

# 2.python setup.py py2exe

from distutils.core import setup

import py2exe

setup(console=['myImg.py'])