Crop Image from The Top

Crop image from the top

Add position: relative; to the div container (and a height).

Then add position: absolute; bottom: 0; to the image itself:

.img-container {       overflow: hidden;      height: 100px;       max-height: 300px;       position: relative;   }
.img { display: block; width: 100%; height: auto; position: absolute; bottom: 0; }
<div class="img-container">   <img class="img" src="http://placekitten.com/400/500" /></div>
<p>Full image below</p> <img src="http://placekitten.com/400/500" />

How to crop 45px from the top and bottom of a jpeg

<?php
function CropImage($sourceImagePath, $width, $height){

// Figure out the size of the source image
$imageSize = getimagesize($sourceImagePath);
$imageWidth = $imageSize[0];
$imageHeight = $imageSize[1];

// If the source image is already smaller than the crop request, return (do nothing)
if ($imageWidth < $width || $imageHeight < $height) return;

// Get the adjustment by dividing the difference by two
$adjustedWidth = ($imageWidth - $width) / 2;
$adjustedHeight = ($imageHeight - $height) / 2;

$src = imagecreatefromjpeg($sourceImagePath);

// Create the new image
$dest = imagecreatetruecolor($width,$height);

// Copy, using the adjustment to crop the source image
imagecopy($dest, $src, 0, 0, $adjustedWidth, $adjustedHeight, $width, $height);

imagejpeg($dest,'somefile.jpg');
imagedestroy($dest);
imagedestroy($src);
}

while display image crop Top and Bottom of image using css

Update

I have updated the answer yet I'm not certain what you want, you just said what you didn't want. So I'm assuming you want to:

  • Maintain aspect ratio

  • Avoid cropping

  • No black bars, just the image edge to edge

We know this is a widescreen poster with the aspect ratio of 16:9, so if you want a width of 270px, do the following:

Divide the width by 16

270/16 = 16.875

Take that quotient and multiply it by 9

16.875 * 9 = 151.875

Round up or down

Round up to 152px

Change the height with the result then apply object-fit:cover

152px is the height of an image that's 270px wide and has an aspect ratio of 16:9.

Please review the Fiddle and updated Snippet


Edit

To reflect the update and better understanding of OP's objective, this Snippet is edited.

object-fit is a simple CSS property. See this article The Snippet below is annotated. Btw, the only code that you need from this Snippet is object-fit: cover, the rest of the styles and markup is just for presentation.

Snippet

/* We know this is a widescreen poster with the aspect ratio of 16:9, so if you want a width of 270px, do the following:
270/16 = 16.875 16.875 * 9 = 151.875 Round up to 152px 152px is the height of an image that's 270px wide and has an aspect ratio of 16:9 */
.bg1 {  width: 270px;  height: 152px;  object-fit: cover;  outline: 2px dashed blue;}.bg2 {  width: 270px;  height: 152px;  object-fit: contain;  outline: 2px dashed blue;}.bg3 {  width: 270px;  height: 152px;  outline: 2px dashed blue;}aside {  height: 100%;  width: 40%;  display: inline-block;  position: absolute;  right: 0;  top: 0;}figure {  height: 180px;  width: 270px;  max-width: 50%;}.pointer {  position: absolute;}.pointer b {  font-size: 32px;}#a.pointer {  top: 43%;  left: 52%;}#b.pointer {  bottom: 5%;  left: 52%;}.box {  width: 600px;  height: 450px;  position: relative;}.spacer {  position: relative;  padding: .1% 0;  width: 2px;}code {  font-family: Consolas;  color: red;}
<section class="box">  <figure>    <figcaption>object-fit: cover</figcaption>    <img class="bg1" src="http://img.youtube.com/vi/MzMqjG9om18/hqdefault.jpg" />  </figure>  <!--<div class="pointer" id="a"><b>⬅</b>    <br/>Space</div>-->  <figure>    <figcaption>object-fit: contain</figcaption>    <img class="bg2" src="http://img.youtube.com/vi/MzMqjG9om18/hqdefault.jpg" />  </figure>

<figure> <figcaption>Without anything but the height property</figcaption> <img class="bg3" src="http://img.youtube.com/vi/MzMqjG9om18/hqdefault.jpg" /> </figure>
<aside> <p><code>object-fit: cover</code> will stretch an image to the edges of it's container (parent element) while keeping aspect ratio and cropping.</p> <p>But when given the correct dimensions, <code>object-fit: cover</code> will result in a perfect fit edge to edge. No cropping either.</p> <div class="spacer"> </div> <p><code>object-fit: contain</code> will stretch an image to the edges of it's container while keeping aspect ratio but will not crop at the edges, so as you can see, this image has space left and right. At wider dimentions, the space will manifest below and above.</p> <div class="spacer"> </div> <p>This is the image when set to it's aspect ratio at 270 x 152px and as you can see, without <code>object-fit:cover</code>, math alone will not resolve the problem.</p> </aside> <!--<div class="pointer" id="b"><b>⬅</b> <br/>Space</div>--></section>

Crop image in CSS

You need to put some height to the container also, if not, it doesn't know how much it should show of what it is inside.

You can try something like this.

.portrait-crop{
display: inline-block;
height: 215px;
width: 50%;
overflow: hidden;
}

.portrait-crop img{
width: 100%;
}

fastest ways of cropping a fixed size image into 9 fixed size crops

Given that:

  • 2GHz processors have been available for around 20 years and only around 3GHz is "mainstream" today, and
  • quad-core and up to 16-core processors are fairly common nowadays

it would seem processors are becoming "fatter" (more cores) rather than "taller" (more GHz). So you would probably do well to leverage that.

Given that Python has a GIL, it is not so good for multi-threading, so you would probably do better to use multi-processing instead of multi-threading and allow each core to work independently on an image to minimise the amount of pickling and sharing data between processes.

You didn't mention the format or dimensions of your images. If they are JPEGs, you might consider using turbo-jpeg. If they are very large, memory may be an issue with multiprocessing.

The likely candidates are probably the following:

  • OpenCV
  • vips
  • Pillow
  • ImageMagick/wand

but it will depend on many things:

  • CPU - GHz, cores, generation
  • RAM - amount, speed, channels, timings
  • disk subsystem - spinning, SSD, NVMe
  • image format, dimensions, bit-depth

So you'll need to benchmark. I did some similar benchmarking here.


If you want to replace the process_file() in John's answer with a PIL or OpenCV version, it might look like this:

import pathlib
import numpy as np
from PIL import Image
import cv2

def withPIL(filename):
pathlib.Path(f"out/{filename}_tiles").mkdir(parents=True, exist_ok=True)
image = Image.open(filename)
for y in range(3):
for x in range(3):
top, left = y*720, x*1280
tile = image.crop((left,top,left+1280,top+720))
tile.save(f"out/{filename}_tiles/{x}_{y}.png")

def withOpenCV(filename):
pathlib.Path(f"out/{filename}_tiles").mkdir(parents=True, exist_ok=True)
image = cv2.imread(filename,cv2.IMREAD_COLOR)
for y in range(3):
for x in range(3):
top, left = y*720, x*1280
tile = image[top:top+720, left:left+1280]
cv2.imwrite(f"out/{filename}_tiles/{x}_{y}.png",tile)

How to crop image using masked image and overlay on top of other image with same color?

import cv2
cat = cv2.imread('cat.jpg')
mask_cat = cv2.imread('mask_cat.jpg', 0)
result = cv2.bitwise_and(cat,cat,mask = mask_cat)

Sample Image

I also see you try to reshape the image.It can be done as follows.

width, height = 1742, 815
reshaped_result = cv2.resize(result, dsize=(width, height))

Sample Image

To place the cropped image on resized image

width, height = 1742, 815
result_final = np.zeros((height,width,3), np.uint8)
h, w = result.shape[:2]
hh, ww = result_final.shape[:2]
yoff = round((hh-h)/2)
xoff = round((ww-w)/2)
result_final[yoff:yoff+h, xoff:xoff+w] = result

Sample Image

What is the easy way to crop a part of image using Python OpenCV

(x1, y1) are the coordinates of starting point, while (x2, y2) are the end points in the image. In a rectange you can think of them as top-left corner is (x1, y1) while (x2, y2) is bottom-right corner - or just like width and height.

But while cropping they have a little reverse format

cropImage = image[ y1: y2 , x1: x2] 
# or
cropImage = image[ Y: H, X: W ]

Pytorch crop images from Top Left Corner in Transforms

I used Lambda transforms in order to define a custom crop

from torchvision.transforms.functional import crop

def crop800(image):
return crop(image, 0, 0, 800, 800)

data_transforms = {
'images': transforms.Compose([transforms.ToTensor(),
transforms.Lambda(crop800),
transforms.Resize((400, 400))])}


Related Topics



Leave a reply



Submit