Created
October 16, 2012 18:49
-
-
Save snowleung/3901192 to your computer and use it in GitHub Desktop.
iphone @2x.png to png
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #coding=utf8 | |
| import Image | |
| import math | |
| import os | |
| import re | |
| import glob | |
| class image: | |
| watermarkfile_default = '~/warter.png' | |
| def __init__(self,watermark=''): | |
| self.error_message = '' | |
| if watermark != '': | |
| self.watermarkfile_default = watermark | |
| def error_to(self, code, target=''): | |
| dict_err = { | |
| 'nodir':'%s 文件夹不存在', | |
| 'nofile':'%s 文件不存在', | |
| 'noimg':'%s 不是一个有效的图片(jpg, gif, png, bmp)', | |
| 'nowater':'%s 水印文件有误', | |
| 'ex_cut':'%s 生成缩略图异常错误', | |
| 'ex_resize':'%s 改变图像尺寸异常错误', | |
| 'ex_watermark':'%s 生成水印异常错误' | |
| } | |
| self.error_message = dict_err[code] % target | |
| def thumbnail(self,img_path,width,height,path=None): | |
| '''use Image thumnail''' | |
| try : | |
| img = Image.open(img_path) | |
| except IOError: | |
| return None | |
| img.thumbnail((width,height),Image.ANTIALIAS) | |
| save_path =None | |
| if path: | |
| save_path = path | |
| img.save(save_path) | |
| else: | |
| base,ext = os.path.splitext(img_path) | |
| save_path =base+"_"+str(width)+'x'+str(width)+ext | |
| img.save(save_path,quality=100) | |
| return save_path | |
| def resize(self, srcfile ,width, height): | |
| '''将图片修改成指定大小,拉升,切割,变形 | |
| –srcfile [string] 源图片文件名 | |
| –width [int] 缩略图宽度 | |
| –height [int] 缩略图高度 | |
| return [bool] 成功: True, 失败: False''' | |
| try: | |
| im = Image.open(srcfile) | |
| except IOError: | |
| self.error_to('noimg',srcfile) | |
| return False | |
| try: | |
| mode = im.mode | |
| if mode not in ('L', 'RGB'): | |
| if mode == 'RGBA': | |
| # 透明图片需要加白色底 | |
| alpha = im.split()[3] | |
| bgmask = alpha.point(lambda x: 255-x) | |
| im = im.convert('RGB') | |
| # paste(color, box, mask) | |
| im.paste((255,255,255), None, bgmask) | |
| else: | |
| im = im.convert('RGB') | |
| im_w, im_h = im.size | |
| if im_w == im_h: | |
| region = im | |
| else: | |
| if im_w > im_h: | |
| delta = (im_w - im_h)/2 | |
| box = (delta, 0, delta+im_h, im_h) | |
| else: | |
| delta = (im_h - im_w)/2 | |
| box = (0, delta, im_w, delta+im_w) | |
| region = im.crop(box) | |
| thumb = region.resize((width,height), Image.ANTIALIAS) | |
| thumb.save(srcfile, quality=90) | |
| except: | |
| self.error_to('ex_resize',srcfile) | |
| return False | |
| return True | |
| def waterMark(self, srcfile, destfile = '',watermark_file=None): | |
| '''生成水印 | |
| –srcfile [string] 源图片文件名 | |
| –destfile [string] 目录图片文件名,默认为源图片文件 | |
| –width [int] 缩略图宽度 | |
| –height [int] 缩略图高度 | |
| return [bool] 成功: True, 失败: False''' | |
| if destfile =='': destfile = srcfile | |
| if watermark_file : | |
| watermarkfile = watermark_file | |
| else: | |
| watermarkfile = watermarkfile_default | |
| try: | |
| im = Image.open(srcfile) | |
| except IOError: | |
| self.error_to('noimg',srcfile) | |
| return False | |
| try: | |
| wm = Image.open(watermarkfile) | |
| except IOError: | |
| self.error_to('nowater',watermarkfile) | |
| return False | |
| try: | |
| #水印自适应 | |
| if (wm.size[0] > im.size[0]) or (wm.size[1] > im.size[1]): | |
| wm = self.img_resize(wm, im.size[0], im.size[1]) | |
| if im.mode != 'RGBA': | |
| im.convert('RGBA') | |
| layer = Image.new('RGBA', im.size, (0,0,0,0)) | |
| layer.paste(wm, ((im.size[0]-wm.size[0])/2, (im.size[1]-wm.size[1])/2)) | |
| im = Image.composite(layer, im, layer) | |
| im.save(destfile) | |
| except: | |
| self.error_to('ex_watermark',srcfile) | |
| return False | |
| return True | |
| imglib = image() | |
| #############readme!!! | |
| #this python script need PIL support | |
| #link : http://effbot.org/media/downloads/Imaging-1.1.7.tar.gz | |
| #python version:2.7 | |
| #Oct 17 2012 | |
| ############ | |
| ############################## | |
| #config here | |
| #set your root path | |
| #like:(use absoulte path) | |
| #imp_path_dir = '/Users/yourname/Desktop/resource' | |
| #or use: | |
| #img_path_dir = os.getcwd() | |
| ##this will get your current directory where your python file on; | |
| ##this is default value | |
| ############################## | |
| ###need to know: | |
| ###filename must *@2x.png | |
| ###after convert name is *.png | |
| img_path_dir = os.getcwd() | |
| file_dirs_list = [] | |
| file_dirs_list.append(img_path_dir) | |
| for a,b,c in os.walk(img_path_dir): | |
| for n in b: | |
| # print os.path.join(a,n) | |
| file_dirs_list.append(os.path.join(a,n)) | |
| file_list = [] | |
| #print file_dirs_list[0] | |
| for dirpath in file_dirs_list: | |
| filesss= glob.glob(os.path.join(dirpath,"*@2x.png")) | |
| for i in range(0,len(filesss)): | |
| existFileName = filesss[i].replace("@2x.png",".png") | |
| if not os.path.exists(existFileName): | |
| file_list.append(filesss[i]) | |
| print file_list | |
| for fname in file_list: | |
| img_current = Image.open(fname) | |
| #build save path | |
| base,ext = os.path.splitext(fname) | |
| save_path =base.replace("@2x","")+ext | |
| print imglib.thumbnail(fname,img_current.size[0]/2,img_current.size[1]/2,save_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment