"Monitorize" an Image
Matlab 2013, 150 bytes
Here is my attempt in Matlab. Definitely won't be the shortest code, but it's a start.
Warning, this overwrites the original image, so make a copy first.
Golfed Version
function mi(f,n)
o=imread(f);
s=size(o);
imwrite([imresize(o(:,1:((s(2)-n)/2),:),[s(1),s(2)/2]) imresize(o(:,((s(2)+n)/2):end,:),[s(1),s(2)/2])], f);
end
Ungolfed Code, with improvements for odd image sizes and odd number of columns
function monitorizeImage( filename, num_columns )
orig = imread(filename);
orig_size = size(orig);
f = factor(orig_size(2));
origsize_iseven = f(1)==2;
f = factor(num_columns);
num_columns_iseven = f(1)==2;
odd_even_size_mismatch = xor(origsize_iseven,num_columns_iseven);
img_resized = imresize(orig,[orig_size(1) orig_size(2)+odd_even_size_mismatch]);
leftimg = img_resized(:,1:((orig_size(2)+odd_even_size_mismatch-num_columns)/2),:);
leftimg = imresize(leftimg,[orig_size(1),floor(orig_size(2)/2)]);
rightimg = img_resized(:,((orig_size(2)-odd_even_size_mismatch+num_columns)/2):end,:);
rightimg = imresize(rightimg,[orig_size(1),floor(orig_size(2)/2)]);
monitorized_image = [leftimg rightimg];
monitorized_image = imresize(monitorized_image,[orig_size(1),orig_size(2)+ ~origsize_iseven]);
[~, ~, ext] = fileparts(filename);
imwrite(monitorized_image,strcat(filename(1:end-length(ext)),'_',num2str(num_columns),ext));
end
Wolfram Language, 134, 127, 119 111 bytes
f[i_,c_]:=(d=ImageDimensions@i;ImageAssemble[ImageTake[i,a=All,#]&/@{{0,e=-#&@@d/2-c/2},{-e,a}}]~ImageResize~d)
Creates a function f
that takes an image as the first input (as a symbol in Mathematica or the Wolfram Cloud), and an integer as the second input.
Ungolfed:
f[image_,columns_]:=( (*Define Function*)
d=ImageDimensions[image]; (*Get image dimensions*)
e=d[[1]]/2+columns/2; (*Add half the image width to half the number of removed columns*)
ImageResize[ImageAssemble[Map[ImageTake[i,All,#]&,{{0,-e},{e,All}}]],d] (*Map the function onto a list with the desired column ranges and merge and scale the resulting image*)
)
Technically, it won't work properly if either of the image dimensions exceed 362,880 pixels, but I assume that's okay, since that is way outside the scope of the problem (and some computers). Fixed!
PHP, 206 bytes
($c=imagecopyresized)($t=imagecreatetruecolor($w=imagesx($s=imagecreatefrompng($argv[1])),$h=imagesy($s)),$s,0,0,0,0,$v=$w/2,$h,$x=$v-$argv[2]/2,$h);$c($t,$s,$v,0,$w-$x,0,$v,$h,$x,$h);imagepng($t,$argv[3]);
takes three command line arguments: source file name, number of lines to crop and target filename.
Run with -r
.
You may want to use imagecopyresampled
instead of imagecopyresized
(+2 bytes) for a better result.
ungolfed
$s=imagecreatefrompng($argv[1]); # load source image
$w=imagesx($s);$h=imagesy($s); # get image dimensions
$t=imagecreatetruecolor($w,$h); # create target image
$v=$w/2; # $v = half width
$x=$v-$argv[2]/2; # $x = width of remaining halves
# resize and copy halves:
imagecopyresized($t,$s, 0,0, 0,0,$v,$h,$x,$h);
imagecopyresized($t,$s,$v,0,$w-$x,0,$v,$h,$x,$h);
imagepng($t,$argv[3]); # save target image
I could save 9 more bytes by sending the PNG result to STDOUT ... but what for?