转自
https://github.com/zhasm/scripts/raw/master/resize.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename resize.py
#created: 2011-01-05 21:35
import re
from sys import exit
from optparse import OptionParser
from os.path import abspath
from os.path import dirname as dirname
from os.path import join as pathjoin
try:
import Image
except:
print "Please install PIL module for python before using this script"
exit(1)
def getPath(sufix=""):
'''get absolute path of the current dir'''
path = dirname(__file__)
try:
index=path.index("..")
if index!=-1:
path=path[:index]
except:
pass
return pathjoin(abspath(path), sufix).replace('\\','/')
def getOptions():
'''get and parse options '''
usage = "usage: %prog [options] FILE1 [ FILE2 ...]"
parser=OptionParser(usage=usage)
parser.add_option("-r", "--resolution", dest="resolution", help="the destination resolution in 1024x768 format. You may use Non-digital chars to separate width and height, but do not use space as delimiter")
parser.add_option("-p", "--percent", dest="percent", help="the destination resolution in 90 format, where 90 stands for 90% percent. Please do not add the '%' sign.")
parser.add_option("-d", "--destination", dest="destination", help="the path to save the result. if omitted, the current folder will be used.")
(options, args) = parser.parse_args()
if not ( options.resolution or options.percent):
print parser.parse_args( [ '--help' ] )
exit(1)
if not args:
print "Please Specify Picture files to resize!"
print parser.parse_args( [ '--help' ] )
exit(1)
if not (options.destination):
options.destination=getPath()
return{
'files': args,
'option': options,
}
def resize(files, resolutionString="", percent=None, output="./"):
'''resize all files with given opts'''
size=[]
if resolutionString:
size=re.findall(r"\d+", resolutionString)
size=[int(i) for i in size]
if not size:
print "Seems the resolution argument is not correct."
exit(1)
elif percent:
percent=int(percent)
if not percent:
print "Seems the percent argument is not correct."
exit(1)
all=len(files)
index=0
for file in files:
try:
im=Image.open(file)
try:
filename=re.findall(r"[^/]+$", file)[0]
except:
print 'Error occured when try to get the real filename'
continue
outputfilename=pathjoin(abspath(output), "r_"+filename).replace('\\','/')
if percent and not size:
size=[int(i * percent / 100) for i in im.size ]
result=im.resize(size, Image.ANTIALIAS)
result.save(outputfilename)
index+=1
print "[%d of %d] %s has been resized." % (index, all, file)
except:
print "Error occured when resizing %s" % file
if __name__=="__main__":
args=getOptions()
files=args['files']
option=args['option']
resize(files, resolutionString=option.resolution,
percent=option.percent,
output=option.destination)
分享到:
相关推荐
这个工具包集成了多种常用的图片处理功能,包括图片的批量重命名、批量调整尺寸、批量转换格式、批量添加水印等,极大地提高了用户在处理大量图片文件时的工作效率。该工具包采用了简洁明了的用户界面设计,即使是对...
在 Windows 下,我们很容易找到各种批量改名的小工具,但如果你使用 Mac OS,想找一款好用且免费的的批量重命名文件的软件就稍微难点了,特别是如果你不会自己写批处理代码的话。 今天咱们推荐一款名为 NameChanger...
Pngyu是一款Png图片批量处理工具,支持mac和Windows两个系统,Pngyu是一款使用 pngquant作为压缩引擎,是一款简单的PNG图像文件批量压缩工具。 软件特色 压缩小巧快速 使用 pngquant作为压缩引擎 能批量...
Image Smith for mac这是一款Mac平台上的一款图片批量转换工具,它支持多种常见的图像格式,如JPEG,GIF,PNG,TIFF,PSD,bmp等。Image Smith for mac包含了30种以上预置设定。你可以缩放与大小调整、翻转或旋转、...
XnConvert是一款简单易用的批量图像格式转换软件,其所支持图片格式有JPG、PNG、TIFF、GIF、RAW、JPEG2000、WebP、OpenEXR等等。你可以轻松的实现图像格式的转换、缩放图像比例、添加滤镜特效、水印等等。XnConvert...
Pixer mac版是一款可以在苹果电脑MAC OS平台上使用的非常实用的Mac图像处理软件,Pixer mac这款软件非常的简单,最主要的功能就是帮助用户进行图片的批量处理,用户还可以自己定义指令,简化批量处理图片的操作你...
名称:图压,软件支持 MAC、Win 系统,软件界面清新、几乎可以做到无损画质压缩,图片处理速度快 软件简介: 软件界面清新、美观 无损画质压缩,处理速度快 支持压缩到指定大小、尺寸 支持图片批量压缩 软件界面...
Capture One PRO拥有无限制批量冲洗功能,多张对比输出功能,色彩曲线编辑,数码信息支持,附加 对数码相机RAW文件支持以及其它的功能。软件可以提供最好的转换质量,工作流程获得了世界的好评,因此Capture One PRO...
1、下载后把“选中对象...默认状态下是将你选中的所有内容当成一个对象来处理,如果你想分别导出,请先将你要导出的对象分别群组,然后勾选“单独保存选定对象”即可。 如果你的AI对象很复杂,请耐心等待脚本处理完成!
NameChanger 是强大的批量文件改名工具,专门解决类似麻烦。将需要重新命名的文件加入列表,随心所欲定制改名规则。...在处理图片时还可以打开图像浏览窗口,用拖动操作改变图像顺序,直观调整编号
特别适合批量处理图片资源,比如全套的icon之类的。这可是APP切图最实用的无损图片压缩神器。 另外,如果各位想要对APP切图@2x,@3x图片资源进行批量压缩的话,方法非常简单!全选拖到下面这个压缩界面就行啦!
如果处理的图片比较少则使用在线压缩即可,非常方便,但如果图片处理量比较大,使用在线压缩一次一次的上传下载则显得有些麻烦了,因此用Python写了一个简单的脚本,用于批量压缩图片。使用方法一.配置环境Python: ...
Photo Sense for Mac版是一款可以在苹果电脑MAC OS平台上使用的强大的批量图片编辑和增强软件,简单易用, 让您的图片更完美。可自动增强图片,也可让您自定义增强效果,调整增强效果;支持批量处理和同步图片设置,...
Photomatix用来处理同一场景下不同曝光设置的照片。这些照片称为包围曝光照片,许多型号的相机都有自动包围曝光功能。 但并非一定需要多张照片才能使用Photomatix。色调映射工具(Tone Mapping tool)也能用于48位...
Pngyu是一款Png图片批量处理工具,支持mac和Windows两个系统,Pngyu是一款使用 pngquant作为压缩引擎,是一款简单的PNG图像文件批量压缩工具。 Pngyu(PNG图像压缩) v1.0.1截图
Athentech Perfectly Clear Complete是专业图像清晰化滤镜,超越人为的照片编辑,开创模仿人眼的自动批量修正功能,让用户可以花更少的时间进行手动修改,以最简单,最快捷的方式来改善你每一照片,将你的图片处理为...
一次处理一批PDF文件 将页面插入PDF 将页面从一个PDF插入到另一个 根据需要排列页面顺序 从PDF文件中删除任何页面 压缩PDF文件 减少PDF文件的文件大小主要是通过降低图像质量和有效缩放。 10种预设压缩选项可满足您...
这款NFT生成器软件具有快捷方便的预览功能,灵活的组合方案,强大的图层编辑功能,可以轻松处理复杂的图层组合规则,快速批量生成NFT作品图片,同时输出详细的metadata元数据。访问官网: https://fancynft.art
ImageMagick是一个免费的开源,功能丰富,基于文本和跨平台的图像处理工具,用于...它专为批量处理图像而设计(即ImageMagick允许您在脚本(shell,DOS,Python,Ruby,Perl,PHP和许多其他)中组合图像处理操作)。