3 examples of 'crop image opencv' in Python

Every line of 'crop image opencv' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
91@staticmethod
92def _opencv_image(source: Image, resize: int = 0) -> CVImage:
93 """Read image file to grayscale openCV int array.
94
95 The OpenCV algorithms works on a two dimensional
96 numpy array integers where 0 is black and 255 is
97 white. Color images will be converted to grayscale.
98 """
99 if isinstance(source, Path):
100 assert source.exists(), 'file {} not found'.format(source)
101 source = source.read_bytes()
102 if not isinstance(source, bytes):
103 raise TypeError('incorrect type')
104
105 data = numpy.frombuffer(source, numpy.uint8)
106 cv_image = cv2.imdecode(data, cv2.IMREAD_COLOR)
107 cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY)
108 if resize > 0:
109 w, h = cv_image.shape[1::-1] # type: int, int
110 multiplier = (resize**2 / (w * h))**0.5
111 dimensions = tuple(int(round(d * multiplier)) for d in (w, h))
112 cv_image = cv2.resize(
113 cv_image, dimensions, interpolation=cv2.INTER_AREA
114 )
115 return cv_image
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

559@tests.test_if(not skip_slow_tests)
560def crop():
561 """Crops in-place."""
562 with Image(filename=asset('croptest.png')) as img:
563 with img.clone() as cropped:
564 assert cropped.size == img.size
565 cropped.crop(100, 100, 200, 200)
566 assert cropped.size == (100, 100)
567 with Color('#000') as black:
568 for row in cropped:
569 for col in row:
570 assert col == black
571 with img.clone() as cropped:
572 assert cropped.size == img.size
573 cropped.crop(100, 100, width=100, height=100)
574 assert cropped.size == (100, 100)
575 with img.clone() as cropped:
576 assert cropped.size == img.size
577 cropped.crop(left=150, bottom=150)
578 assert cropped.size == (150, 150)
579 with img.clone() as cropped:
580 assert cropped.size == img.size
581 cropped.crop(left=150, height=150)
582 assert cropped.size == (150, 150)
583 with img.clone() as cropped:
584 assert cropped.size == img.size
585 cropped.crop(-200, -200, -100, -100)
586 assert cropped.size == (100, 100)
587 with img.clone() as cropped:
588 assert cropped.size == img.size
589 cropped.crop(top=100, bottom=200)
590 assert cropped.size == (300, 100)
591 with raises(ValueError):
592 img.crop(0, 0, 500, 500)
593 with raises(ValueError):
594 img.crop(290, 290, 50, 50)
595 with raises(ValueError):
596 img.crop(290, 290, width=0, height=0)
341def crop_image(img, i0, j0, ni, nj):
342 boxm2_batch.init_process("vilCropImageProcess")
343 boxm2_batch.set_input_from_db(0, img)
344 boxm2_batch.set_input_unsigned(1, i0)
345 boxm2_batch.set_input_unsigned(2, j0)
346 boxm2_batch.set_input_unsigned(3, ni)
347 boxm2_batch.set_input_unsigned(4, nj)
348 boxm2_batch.run_process()
349 (id, type) = boxm2_batch.commit_output(0)
350 img_out = dbvalue(id, type)
351 return img_out

Related snippets