Skip to content

Instantly share code, notes, and snippets.

@rainyear
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainyear/ae1fa7e2b7ca9123c7f0 to your computer and use it in GitHub Desktop.
Save rainyear/ae1fa7e2b7ca9123c7f0 to your computer and use it in GitHub Desktop.
processing all jpg images from path <src> , crop <x> pixels from bottom, save to <dest>.
function cropBottomByX(src, dest, x)
srcImgs = [dir([src filesep '*.jpg'])];
for i = 1:length(srcImgs)
imgLocation = [src filesep srcImgs(i).name];
img = imread(imgLocation);
[h, w, d] = size(img);
disp(sprintf('Resizing image %s from %d to %d ...\n', srcImgs(i).name, h, h-x));
img2 = img(1:h-x, :, :);
imwrite(img2, [dest filesep srcImgs(i).name]);
end
end
function scaleToSizeXY(src, dest, width, height)
srcImgs = [dir([src filesep '*.jpg'])];
for i = 1:length(srcImgs)
imgLocation = [src filesep srcImgs(i).name];
img = imread(imgLocation);
[h, w, d] = size(img);
if h / w > height / width
h2 = floor(height / width * w);
img2 = img(floor((h - h2)/2):floor((h - h2)/2+h2), :, :);
elseif h / w <= height / width
w2 = floor(h * width / height);
img2 = img(:, floor((w-w2)/2):floor((w-w2)/2+w2),:);
end
disp(sprintf('Scale image %s...\n', srcImgs(i).name));
img3 = imresize(img2, height / h);
imwrite(img3, [dest filesep srcImgs(i).name]);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment