Вы достигнете своей цели, если замените строку:
x_train, x_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.3,random_state=0)
на:
idx_train = int((1-.3)* x.shape[0]) # train is (1-.3) of your data
x_train = x[:idx_train,:]
x_test = x[idx_train:, :]
y_train = y[:idx_train]
y_test = y[idx_train:]
Это решение, в основном, такое же, как у Can Berk Güder, но, потратив некоторое время на написание и комментирование, я почувствовал, что posting.
Эта функция создает миниатюру, которая в точности равна размеру, который вы ей даете. Размер изображения изменяется в соответствии с размером миниатюры. Если он не подходит точно в обоих направлениях, он центрируется в хвосте. Обширные комментарии объясняют, что происходит.
function thumbnail_box($img, $box_w, $box_h) {
//create the image, of the required size
$new = imagecreatetruecolor($box_w, $box_h);
if($new === false) {
//creation failed -- probably not enough memory
return null;
}
//Fill the image with a light grey color
//(this will be visible in the padding around the image,
//if the aspect ratios of the image and the thumbnail do not match)
//Replace this with any color you want, or comment it out for black.
//I used grey for testing =)
$fill = imagecolorallocate($new, 200, 200, 205);
imagefill($new, 0, 0, $fill);
//compute resize ratio
$hratio = $box_h / imagesy($img);
$wratio = $box_w / imagesx($img);
$ratio = min($hratio, $wratio);
//if the source is smaller than the thumbnail size,
//don't resize -- add a margin instead
//(that is, dont magnify images)
if($ratio > 1.0)
$ratio = 1.0;
//compute sizes
$sy = floor(imagesy($img) * $ratio);
$sx = floor(imagesx($img) * $ratio);
//compute margins
//Using these margins centers the image in the thumbnail.
//If you always want the image to the top left,
//set both of these to 0
$m_y = floor(($box_h - $sy) / 2);
$m_x = floor(($box_w - $sx) / 2);
//Copy the image data, and resample
//
//If you want a fast and ugly thumbnail,
//replace imagecopyresampled with imagecopyresized
if(!imagecopyresampled($new, $img,
$m_x, $m_y, //dest x, y (margins)
0, 0, //src x, y (0,0 means top left)
$sx, $sy,//dest w, h (resample to this size (computed above)
imagesx($img), imagesy($img)) //src w, h (the full size of the original)
) {
//copy failed
imagedestroy($new);
return null;
}
//copy successful
return $new;
}
Пример использования:
$i = imagecreatefromjpeg("img.jpg");
$thumb = thumbnail_box($i, 210, 150);
imagedestroy($i);
if(is_null($thumb)) {
/* image creation or copying failed */
header('HTTP/1.1 500 Internal Server Error');
exit();
}
header('Content-Type: image/jpeg');
imagejpeg($thumb);
Это не обрезает изображение, но при необходимости оставляет пространство вокруг нового изображения, что, на мой взгляд, лучше подход (чем обрезка) при создании миниатюр.
$w = 210;
$h = 150;
$orig_w = imagesx($original);
$orig_h = imagesy($original);
$w_ratio = $orig_w / $w;
$h_ratio = $orig_h / $h;
$ratio = $w_ratio > $h_ratio ? $w_ratio : $h_ratio;
$dst_w = $orig_w / $ratio;
$dst_h = $orig_h / $ratio;
$dst_x = ($w - $dst_w) / 2;
$dst_y = ($h - $dst_h) / 2;
$thumbnail = imagecreatetruecolor($w, $h);
imagecopyresampled($thumbnail, $original, $dst_x, $dst_y,
0, 0, $dst_w, $dst_h, $orig_w, $orig_h);
У вас есть Imagick ? Если это так, вы можете загрузить изображение с ним и сделать что-то вроде thumbnailimage ()
Там вы можете пропустить любой из параметров (высота или ширина), и он изменит свой размер правильно.
Я бы предпочел изменить размер так, чтобы изображение содержало в пределах вашего лимита, а затем заполните пустые части. Таким образом, в приведенном выше примере вы изменили бы размеры, чтобы высота была в порядке, а затем залили (по 7 пикселей на каждом конце) слева и справа цветом фона.
Just a tip for high-quality fast thumbnail generation from large images: (from the php.net site)
If you do the thumbnail generation in two stages:
then this can be much faster; the resize in step 1 is relatively poor quality for its size but has enough extra resolution that in step 2 the quality is decent, and the intermediate image is small enough that the high-quality resample (which works nicely on a 2:1 resize) proceeds very fast.
Изменение размера изображений из веб-страницы на основе PHP может быть проблематичным. Большие изображения (около 2 + МБ на диске) могут быть настолько большими, что для их обработки требуется более 32 МБ памяти.
По этой причине я склонен делать это либо из сценария на основе CLI, с объемом до 128 МБ. доступной ему памяти или стандартной командной строки, которая также использует столько, сколько ей нужно.
# where to put the original file/image. It gets resized back
# it was originally found (current directory)
SAFE=/home/website/PHOTOS/originals
# no more than 640x640 when finished, and always proportional
MAXSIZE=640
# the larger image is in /home/website/PHOTOS/, moved to .../originals
# and the resized image back to the parent dir.
cd $SAFE/.. && mv "$1" "$SAFE/$1" && \
convert "$SAFE/$1" -resize $MAXSIZE\x$MAXSIZE\> "$1"
'convert' является частью инструментов командной строки ImageMagick.
Может быть, посмотрите на PHPThumb (работает с GD и ImageMagick)
это миниатюры? если они есть, обрезка не является большой проблемой. Мы делаем это все время. я даже не уклоняюсь от обрезания произвольных соотношений в искалеченные квадратные миниатюры, полностью испортить изображение (да, я тот хардкор ), если это просто выглядит хорошо. Это дизайнерский ответ на технический вопрос, но все же. не бойся урожая!
Я думаю, что есть небольшая путаница .. Если вы хотите только изменить его размер, сохранив исходное соотношение, правильная операция будет такой:
$ratio = $originalWidth / $originalHeight;
if(//you start from the width, and want to find the height){
$newWidth = $x;
$newHeight = $x / $ratio;
}else if(//you start from the height, and want to find the width){
$newHeight = $x;
$newWidth = $x * $ratio;
}
Иначе, если префикс newWidth и newHeight не может быть изменен, а соотношение большого пальца отличается от исходного соотношения, единственный способ - обрезать или добавьте границы к большому пальцу.
Если вы хотите идти по пути, эта функция может вам помочь (я написал несколько лет назад за 5 минут, возможно, нужно что-то улучшить), например, она работает только с jpg; ):
function thumb_cut($nomeimage, $source_path, $destination_path, $new_width, $new_height){
list($width, $height, $type, $attr) = getimagesize($source_path.$nomeimage);
if($type == 2){
if($width > $new_width){
$new_width = $width;
$new_height = $height;
}
$compression = 100;
$destimg = imagecreatetruecolor($new_width,$new_height) or die("Problems creating the image");
$srcimg = ImageCreateFromJPEG($source_path.$nomeimage) or die("problem opening the image");
$w = ImageSX($srcimg);
$h = ImageSY($srcimg);
$ro = $new_width/$new_height;
$ri = $w/$h;
if($ro<$ri){
$par = "h";
}else{
$par = "w";
}
if($par == "h"){
$ih = $h;
$conv = $new_width/$new_height;
$iw = $conv*$ih;
$cw = ($w/2)-($iw/2);
$ch = ($h/2)-($ih/2);
}else if($par == "w"){
$iw = $w;
$conv = $new_height/$new_width;
$ih = $conv*$iw;
$cw = ($w/2)-($iw/2);
$ch = ($h/2)-($ih/2);
}
ImageCopyResampled($destimg,$srcimg,0,0,$cw,$ch,$new_width,$new_height,$iw,$ih) or die("problems with resize");
ImageJPEG($destimg,$destination_path.$nomeimage,$compression) or die("problems with storing new image");
}
}
Метод заключается в следующем:
И наконец, если вы озадачены тем, как выполнить математику изменения размера, помните, что если пропорции исходного и конечного изображений одинаковы, то это соотношение сохраняется:
SourceWidth / SourceHeight = DestinationWidth / DestinationHeight
Если вам известны три параметра, вы можете легко рассчитать четвертый.
Я написал статью об этом:
Обрезать изображение по ASP / PHP
Вам придется обрезать 5 пикселей сверху и снизу, чтобы достичь целевого размера, однако это может разрушить изображение.
Действительно, у вас должна быть целевая ширина или высота, а затем отрегулируйте другой размер в той же пропорции.