function resizephotos(directory, wh, isrecursive, isoverwrite, savetopath, supportFormat)
% resizephotos: resize a batch of photos
%
% resizephotos -dir max_width_and_height, will let you choose a directory,
% and resize all the photos in the directory. When max_width_and_height is
% omitted, it uses 1600 by default(1600px is enough for most monitors).
%
% resizephotos -files max_width_and_height, will let you choose some files,
% and resize all the files in the directory.
%
% You might be asked for whether to recursively resize all the photos in
% subdirectories.
%
% and You might be asked for whether to overwrite the original files. If
% you choose 'No', the program will save the resized images in new
% directories or files with prefix 'resize-'.
%
% resizephotos(file_or_directory, max_width_and_height, is_recursive, ...
% is_overwrite, path_to_save): you can use the function by specifying
% the path of the directories and files. When using this method, you can
% specific the max width and height of the resized photos. When path_to_save
% is omitted, the path to save the resized photos will generated automatically
% depends on is_overwrite parameter.
%
%
% See also imresize
%
% Copyright 2010, zhiqiang.org
% author: zhang@zhiqiang.org, url:
% http://zhiqiang.org/blog/it/batch-resize-images-using-matlab.html
% $Revision: 1.1.6.28.2.1 $ $Date: 2009/01/27 04:47:43 $ %% cope with the input parameters
% if the directory is not set, we open a file select dialog for use to
% select the files or path to deal with
if nargin == 0 || (nargin >= 1 && ischar(directory) && strcmp(directory, '-dir'))
directory = uigetdir;
% let user to choose whether recursively resize all the subdirectories
tmp = questdlg('Did you want to recursively resize all the subdirectories?');
if strcmp('Yes', tmp)
isrecursive = true;
else strcmp('No', tmp)
isrecursive = false;
end
isoverwrite = askforoverwrite();
elseif (nargin >= 1 && ischar(directory) && strcmp(directory, '-files'))
[p, f] = uigetfile({'*.*', 'All files'}, 'MultiSelect', 'on');
if isnumeric(p) && ~p
return;
end
directory = cell(size(p));
if iscell(p)
for i = 1:numel(p)
directory{i} = [f, p{i}];
end
else
directory=[f p];
end
isoverwrite = askforoverwrite();
end % set other parameters
if ~exist('wh', 'var'), wh = 1600; elseif ischar(wh), wh = eval_r(wh); end %����eval����_r
if ~exist('isrecursive', 'var'), isrecursive = false; end
if ~exist('isoverwrite', 'var'), isoverwrite = false; end
if ~exist('savetopath', 'var'), savetopath = []; end
if nargin <= 5 || isempty(supportFormat), supportFormat = '*'; end %% mult-files
% if directory is a cell, it indicate multi-files or multi-directories,
% we resize them one by one if iscell(directory)
for i = 1:numel(directory)
resizephotos(directory{i}, wh, isrecursive, isoverwrite, savetopath, supportFormat);
end
return;
end %% if everything are OK if exist(directory, 'file') && ~isdir(directory) % for a file,
if nargin <= 4 || isempty(savetopath)
if isoverwrite
savetopath = directory;
else
[pd, fd] = lastdirectory(directory);
savetopath = [pd, 'resize-', fd];
end
end
resizesinglephoto(directory, wh, savetopath);
elseif isdir(directory) % if directory is a real directory
% the last char of directory should be a '\'
if directory(end) ~= '\'
directory = [directory, '\'];
end
% generate the saved directory
if nargin <= 4 || isempty(savetopath) % when at the first recursive and savetopath is not set
% we need to generate the saved directory
if isoverwrite
savetopath = directory;
elseif ~isoverwrite
[savetopath, lastd] = lastdirectory(directory);
savetopath = [savetopath, 'resize-', lastd, '\'];
end
% when the savetopath do not exist, we create one
if ~isdir(savetopath)
mkdir(savetopath); %����������������
end
end % now we resize all photos in current directory, and recursive resize
% all the directories if needed
allfiles = dir(directory); %����������������(������������)������������������ % before we generate now image file, we first make sure the savetopath
% exists, otherwise generate it.
if ~isdir(savetopath)
savetopath=[savetopath(1:end-1) 'resize' '\'];
mkdir(savetopath); %��������������������������������resize
end
for i = 1:numel(allfiles)
cur = allfiles(i).name;
% ignore the path '.' and '..'
if numel(cur) > 2 || ~min(cur == '.')
if allfiles(i).isdir && isrecursive
resizephotos([directory cur '\'], wh, isrecursive, isoverwrite, ...
[savetopath cur '\'], supportFormat);
elseif ~allfiles(i).isdir
resizesinglephoto([directory cur], wh, [savetopath cur]);
end
end
end
end %% resize single photo, save it to savetopath
function resizesinglephoto(photo, wh, savetopath) try
I = imread(photo);
catch ME %#ok<NASGU>
% disp(ME.message);
disp(['!!! ' photo ' is not recognized as an image file, and it''s ignored']);
return;
end % [w, h] is the width and height of original graph
w = size(I, 1);
h = size(I, 2); % ����resize�������� [maxw, maxh]
if numel(wh) == 1
if w > h
maxw = wh;
maxh = wh*h/w;
else
maxh = wh;
maxw = wh*w/h;
end
else
maxw = wh(1);
maxh = wh(2);
if (w-h)*(maxw-maxh) < 0
[maxw, maxh] = deal(h, w);
end if w/h < maxw/maxh
maxw = maxh*w/h;
else
maxh = maxw*h/w;
end
end if maxw < w
I = imresize(I, [maxw, maxh]); %����������������������������������������������������
end
imwrite(I, savetopath);
disp([photo ' is resized, and is saved to ' savetopath]); %% lastdirectory
function [pd, ld] = lastdirectory(d)
% return the last directory, for example, lastdirectory('\abc\edf\') =
% ['\abc\','edf']. When d is a file, return the path and the file name, i.e
% lastdirectory('abc\def\x.jpg') = ['abc\def\', 'x.jpg'] % remove the last \
if isdir(d) && d(end) == '\'
d = d(1:end-1);
end % if there is a \
if max(d=='\')
ld = d(find(d=='\', 1, 'last')+1:end);
else
ld = d;
end pd = d(1:end-numel(ld)); %% ask for whether to overwrite original files
function isoverwrite = askforoverwrite()
tmp = questdlg(['Did you want to overwrite the original files? ' ...
'if you choose No, we will add a ''resize-'' prefix to your ' ...
'files or directories.']);
if strcmp('Yes', tmp)
isoverwrite = true;
elseif strcmp('No', tmp)
isoverwrite = false;
end

  

MATLAB 批量处理图片的更多相关文章

  1. 安装glue,用glue批量处理图片的步骤

     glue批量处理图片:http://glue.readthedocs.io/en/latest/quickstart.html#and-why-those-css-class-names 首先需要安 ...

  2. photoshop动作面板批量处理图片边框技巧

    1,想给图片加上边框,在不改变图片大小的前提下,可以这样做:ctrl+a,全选图片,然后“编辑”-----“描边”,在跳出来的选项卡里面可以设置边框颜色,大小,位置,及混合模式, ,我们设置好了,就可 ...

  3. matlab批量读取一个文件夹里类似命名的mat文件

    参考网址: Matlab读取同一路径下多个txt或mat文件总结 matlab 批量读取数据文件.mat .dat 整理:matlab批量读入数据文件的方法 首先命名方式体现在只是名字里数字有变化,其 ...

  4. PS如何批量处理图片

    喜爱摄影的朋友可能都有这样的体会,相机里面存了大量的图片,一般都是2048×1536或者更大像素的照片,每张都有1M以上,如果设置的清晰度高,则照片就更大,这样的图片是无法上传到博客中的(博客要求每张 ...

  5. 使用matlab批量处理图像后在指定文件夹存储

    使用matlab批量处理图像后在指定文件夹存储 clear;clc;close all; Files=dir('D:\文件及下载相关\文档\MATLAB\postgraduate\Kodak\*.jp ...

  6. Shell 命令行批量处理图片文件名

    Shell 命令行批量处理图片文件名 从网上下载了一堆图片,有的是*.jpg的,有的是*.jpeg的.并且文件名有长有短,很是糟心.因此,我想把这些文件给全部整理好,当然是用shell来处理啦! 说干 ...

  7. MATLAB批量修改图片名称

    申明:转载请注明出处. 设在“D:\UserDesktop\pic\”目录下有很多张格式为jpg照片,命名不规则,如图. 现在用MATLAB批量修改所有图片的命名格式,改为1.jpg,2.jpg,.. ...

  8. matlab批量灰色预测

    没事玩了一下matlab 发现现在网上的代码都是一组数据预测 所以我就写个批量数据的预测 顺便学习下matlab ----------------------------------我是快乐的分割线- ...

  9. Matlab批量读取文件夹文件

    现在有一个文件夹 里面有50个左右的txt文件 每个文件大概三万行 两列 第一列是字符串 第二列是浮点数字 我只需要读第二列 现在我想写一个.M文件 批量读取这个文件夹里的txt文件 读取完以后的数组 ...

随机推荐

  1. QSplitter实现滑动窗口和悬浮按钮

    1         QSplitter实现滑动窗口和悬浮按钮 软件应用中需要设计右侧滑动窗口,通过一个按钮来实现窗口的隐藏和显示,应用场景比如显示主界面的详细信息. (1)   在qt design中 ...

  2. CC2 条理分明-----AACTP教你谈恋爱

      AACTP是一个神奇的地方,这里可以培养你的自信,培养你的沟通表达能力,培养你的领导管理能力:但是你不知道的是AACTP还可以培养你恋爱的本领.想知道是怎么回事吗?听我给你慢慢说来.我把恋爱分为三 ...

  3. 拒绝采样 Rejection Sampling

    2018-12-09 16:40:30 一.使用Rand7()来生成Rand10() 问题描述: 问题求解: 这个问题字节跳动算法岗面试有问到类似的,有rand6,求rand8,我想了好久,最后给了一 ...

  4. Interval 间隔问题

    2018-09-07 09:03:14 一.Merge Intervals 问题描述: 问题求解: public List<Interval> merge(List<Interval ...

  5. selenium+Page Objects(第一话)

    简单介绍一种selenium用来做web自动化测试的设计模式:Page Objects 一.Page Objects介绍 用官话说它是selenium中的一种页面对象设计模式(不是测试框架!是一种开展 ...

  6. docker安装成功启动失败

    docker安装成功却启动失败,查看docker服务,systemctl status docker.service, 服务日志提示Failed to start Docker Application ...

  7. 雷林鹏分享:XML 命名空间

    XML 命名空间 XML 命名空间提供避免元素命名冲突的方法. 命名冲突 在 XML 中,元素名称是由开发者定义的,当两个不同的文档使用相同的元素名时,就会发生命名冲突. 这个 XML 携带 HTML ...

  8. (转) gffcompare和gffread | gtf | gff3 格式文件的分析 | gtf处理 | gtfparse

    工具推荐:https://github.com/openvax/gtfparse 真不敢相信,Linux自带的命令会这么强大,从gtf中提取出需要的transcript,看起来复杂,其实一个grep就 ...

  9. 国外(英文)——WPF较好的奇葩问题解决网站

    https://stackoverflow.com/questions/6415908/c-sharp-wpf-datagrid-vertical-scroll

  10. 20180821ImportContactFromExcel

    Excel创建vcf文件,借助百度云助手导入Iphone6Plus Sub CreateContractList() Set Wb = Application.ThisWorkbook FilePat ...