Every line of 'python crop image' 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.
88 def test_crop_zero(self): 89 90 im = Image.new("RGB", (0, 0), "white") 91 92 cropped = im.crop((0, 0, 0, 0)) 93 self.assertEqual(cropped.size, (0, 0)) 94 95 cropped = im.crop((10, 10, 20, 20)) 96 self.assertEqual(cropped.size, (10, 10)) 97 self.assertEqual(cropped.getdata()[0], (0, 0, 0)) 98 99 im = Image.new("RGB", (0, 0)) 100 101 cropped = im.crop((10, 10, 20, 20)) 102 self.assertEqual(cropped.size, (10, 10)) 103 self.assertEqual(cropped.getdata()[2], (0, 0, 0))
26 def crop_image(self, image, x, y): 27 return image[y: y+self.target_rows, x: x+self.target_cols,...] if self.use_crop else image
23 def crop(image, x, y, width, height): 24 return image.copy(x, y, width, height)
559 @tests.test_if(not skip_slow_tests) 560 def 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)
23 def crop_to(image_to_crop, reference_image): 24 """ 25 Crops image to the size of a reference image. This function assumes that the relevant image is located in the center 26 and you want to crop away equal sizes on both the left and right as well on both the top and bottom. 27 :param image_to_crop 28 :param reference_image 29 :return: image cropped to the size of the reference image 30 """ 31 reference_size = reference_image.size 32 current_size = image_to_crop.size 33 dx = current_size[0] - reference_size[0] 34 dy = current_size[1] - reference_size[1] 35 left = dx / 2 36 upper = dy / 2 37 right = dx / 2 + reference_size[0] 38 lower = dy / 2 + reference_size[1] 39 return image_to_crop.crop( 40 box=( 41 int(left), 42 int(upper), 43 int(right), 44 int(lower)))
78 def get_image_crop(img, x, y, width, height, crop_size=224, padding=16): 79 """ 80 Get the image crop for the object specified in the COCO annotations. 81 We crop in such a way that in the final resized image, there is `context padding` amount of image data around the object. 82 This is the same as is used in RCNN to allow for additional image context. 83 :param img: The image ndarray 84 :param x: The x coordinate for the start of the bounding box 85 :param y: The y coordinate for the start of the bounding box 86 :param width: The width of the bounding box 87 :param height: The height of the bounding box 88 :param crop_size: The final size of the cropped image. Needed to calculate the amount of context padding. 89 :param padding: The amount of context padding needed in the image. 90 :return: 91 """ 92 # Scale used to compute the new bbox for the image such that there is surrounding context. 93 # The way it works is that we find what is the scaling factor between the crop and the crop without the padding 94 # (which would be the original tight bounding box). 95 # `crop_size` is the size of the crop with context padding. 96 # The denominator is the size of the crop if we applied the same transform with the original tight bounding box. 97 scale = crop_size / (crop_size - padding * 2) 98 99 # Calculate semi-width and semi-height 100 semi_width = width / 2 101 semi_height = height / 2 102 103 # Calculate the center of the crop 104 centerx = x + semi_width 105 centery = y + semi_height 106 107 img_width, img_height = img.size 108 109 # We get the crop using the semi- height and width from the center of the crop. 110 # The semi- height and width are scaled accordingly. 111 # We also ensure the numbers are valid 112 upper = max(0, centery - (semi_height * scale)) 113 lower = min(img_height, centery + (semi_height * scale)) 114 left = max(0, centerx - (semi_width * scale)) 115 right = min(img_width, centerx + (semi_width * scale)) 116 117 crop_img = img.crop((left, upper, right, lower)) 118 119 if 0 in crop_img.size: 120 print(img.size) 121 print("lowx {0}\nlowy {1}\nhighx {2}\nhighy {3}".format( 122 left, upper, right, lower)) 123 124 return crop_img
67 def manual_crop(self, sides=(0, 0, 0, 0)): # (top, right, bottom, left) 68 """ 69 Relative sizes! 70 :param sides: 71 :return: 72 """ 73 self._image = self._image.crop(sides) 74 return self
218 def crop(image, x1, y1, x2, y2): 219 """ 220 Crop image. 221 222 >>> import numpy as np 223 >>> image = np.reshape(np.arange(16, dtype='uint8'), (4, 4)) 224 >>> crop(image, 1, 2, 5, 5) 225 array([[ 9, 10, 11], 226 [13, 14, 15]], dtype=uint8) 227 228 :param numpy array image: Numpy array. 229 :param int x1: x-coordinate of left upper corner of crop (inclusive) 230 :param int y1: y-coordinate of left upper corner of crop (inclusive) 231 :param int x2: x-coordinate of right lower corner of crop (exclusive) 232 :param int y2: y-coordinate of right lower corner of crop (exclusive) 233 :return: Cropped image 234 :rtype: numpy array 235 """ 236 return image[y1:y2, x1:x2]
235 def do_crop_transform(self,item,params): 236 item.image=item.image.crop(params['pixel_rect'])
61 def crop(inputFile,inputSizeX,inputSizeY,outputFolder='.'): 62 '''Crops images into subimages of size 'inputSize'x'inputSize''' 63 x=0 64 y=0 65 img = Image.open(inputFile) 66 width, height = img.size 67 print width,height 68 while not ((x+inputSizeX>width) or (y+inputSizeY>height)): 69 img_small = img.crop((x,y,x+inputSizeX,y+inputSizeY)) 70 saveFile=outputFolder+'/'+os.path.split(inputFile)[-1][:-4]+"___"+str(x)+'_'+str(y)+".png" 71 img_small.save(saveFile) 72 if x+2*inputSizeX>width: 73 x=0 74 y=y+inputSizeY 75 else: 76 x = x + inputSizeX 77 image_index=find_before_convert(os.path.split(inputFile)[-1],'.png') 78 with open(imagesizes,'a+') as f: 79 f.write(image_index+','+str(width)+','+str(height)+''' 80 ''')