我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用PIL.Image.EXTENT。
def final_sampling_and_contrast_enhance(im_contrasted, out_size, x0, x1, y0, y1, contrast_enhance): subimage_coordinates = (x0, y0, x1, y1) if contrast_enhance == "PostEqualizeHistogram": # Resize, then equalize im_out = im_contrasted.transform(out_size, Image.EXTENT, subimage_coordinates, interpolation_format_sampling) # W interpolation_format format_nearest im_out = ImageOps.equalize(im_out) elif contrast_enhance == "SmartEqualizeHistogram": # Crop, then equalize, then resize out_size_crop = (x1 - x0, y1 - y0) im_out = im_contrasted.transform(out_size_crop, Image.EXTENT, subimage_coordinates, interpolation_format_sampling) # W interpolation_format format_nearest im_out = ImageOps.equalize(im_out) crop_coordinates = (0, 0, x1 - x0, y1 - y0) im_out = im_out.transform(out_size, Image.EXTENT, crop_coordinates, interpolation_format_sampling) # W interpolation_format format_nearest else: im_out = im_contrasted.transform(out_size, Image.EXTENT, subimage_coordinates, interpolation_format_sampling) return im_out
def transformImg(self, img, t): imgT = img.transform((int(img.size[0]*t[3]),int(img.size[1]*t[3])), Image.EXTENT, (0,0,img.size[0],img.size[1]), Image.BILINEAR) imgT = imgT.rotate(numpy.rad2deg(t[0]), Image.BILINEAR, expand=1) if t[4] == 1.: imgT = imgT.transpose(Image.FLIP_LEFT_RIGHT) # crop only valid part if self.crop: imgT = imgT.crop(self.getInscribedRectangle(t[0], (img.size[0]*t[3], img.size[1]*t[3]))) # crop from translation imgT = imgT.resize((int(self.imgSize[0]*1.1), int(self.imgSize[1]*1.1)), Image.BILINEAR) xstart = int((imgT.size[0] // 2 - t[1]) - self.imgSize[0] // 2) ystart = int((imgT.size[1] // 2 - t[2]) - self.imgSize[1] // 2) assert xstart >= 0 and ystart >= 0 return imgT.crop((xstart, ystart, xstart+self.imgSize[0], ystart+self.imgSize[1]))
def simple_normalization_Age(im, relevant_width, relevant_height, obj_avg=0.0, obj_std=0.2): width, height = im.size[0], im.size[1] # Notice that im is centered to the object, but might be much larger! relevant_left = int((width - relevant_width) / 2.0 + 0.5) relevant_right = relevant_left + int(relevant_width + 0.5) relevant_top = int((height - relevant_height) / 2.0 + 0.5) relevant_bottom = relevant_top + int(relevant_height + 0.5) # print relevant_left, relevant_right, relevant_top, relevant_bottom return simple_normalization_from_coords(im, relevant_left, relevant_right, relevant_top, relevant_bottom, show_points=False, obj_avg=obj_avg, obj_std=obj_std) # TODO: Add Scale/Zoom instead of pixel_sampling???? # use then: im.transform(out_size, Image.EXTENT, data), see xml_frgc_tests # pixelsampling_x = 1.0 / pixel_zoom_x # pixelsampling_y = 1.0 / pixel_zoom_y # trans_sampled: if true translations are done at the sampled image level, otherwise at the original image
def extract_subimages_basic(images, image_indices, coordinates, out_size=(64, 64), allow_out_of_image_sampling=True): if len(image_indices) != len(coordinates): raise Exception("number of images indices %d and number of coordinates %d do not match" % (len(image_indices), len(coordinates))) if len(images) < 1: raise Exception("At least one image is needed") subimages = [] for i, im_index in enumerate(image_indices): subimage_coordinates = (x0, y0, x1, y1) = coordinates[i] if (x0 < 0 or y0 < 0 or x1 >= images[im_index].size[0] or y1 >= images[im_index].size[1]) and \ not allow_out_of_image_sampling: err = "Image Loading Failed: Subimage out of Image" print "subimage_coordinates =", (x0, y0, x1, y1) print "Image size: im.size[0], im.size[1] = ", images[im_index].size[0], images[im_index].size[1] raise Exception(err) im_out = images[im_index].transform(out_size, Image.EXTENT, subimage_coordinates, interpolation_format_sampling) subimages.append(im_out) return subimages
def extract_subimages_rotate(images, image_indices, all_coordinates, delta_angs, out_size=(64, 64), interpolation_format=interpolation_format, contrast_enhance=None, obj_avg=0.11, obj_std=0.15): if len(image_indices) != len(all_coordinates): raise Exception("number of images indices %d and number of coordinates %d do not match" % (len(image_indices), len(all_coordinates))) if len(images) < 1: raise Exception("At least one image is needed") subimages = [] for i, im_index in enumerate(image_indices): subimage_coordinates = all_coordinates[i] if delta_angs[i] != 0: im_out = extract_subimage_rotate(images[im_index], subimage_coordinates, delta_angs[i], out_size=out_size, interpolation_format=interpolation_format, contrast_enhance=contrast_enhance, obj_avg=obj_avg, obj_std=obj_std) else: im_out = images[im_index].transform(out_size, Image.EXTENT, subimage_coordinates, interpolation_format) subimages.append(im_out) return subimages
def test_transform(path, image): with Img(fp=path(image['sub'])) as src, TemporaryFile() as tf: src.transform(image['size'], Image.EXTENT, (0, 0) + image['size']) src.save(fp=tf) with Img(fp=tf) as dest: assert (dest.width, dest.height, dest.frame_count) == image['size'] + (src.frame_count, )
def create_tile(xtile, ytile, base_path, zoom, rf_img, rf_geo_data, **kwargs): (lat_deg_start, lon_deg_start) = num2deg(xtile, ytile, zoom) (lat_deg_end, lon_deg_end) = num2deg(xtile+1, ytile+1, zoom) result_dir = os.path.join(base_path, str(zoom), str(xtile)) result_filename = os.path.join(result_dir, "{0}.png".format(ytile)) lat_per_pixel = math.fabs((rf_geo_data['bb'][1][0]-rf_geo_data['bb'][0][0])/rf_geo_data['imagesize'][0]) lon_per_pixel = math.fabs((rf_geo_data['bb'][1][1]-rf_geo_data['bb'][0][1])/rf_geo_data['imagesize'][1]) (start_pixel_x, start_pixel_y) = get_pixel_from_pos(rf_geo_data, lat_deg_start, lon_deg_start) (end_pixel_x, end_pixel_y) = get_pixel_from_pos(rf_geo_data, lat_deg_end, lon_deg_end) print("use pixel: {0}|{1} to {2}|{3}".format(start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y)) # TODO: refactor to use OpenCL #source_img = ImageChops.offset(rf_img,start_pixel_x, start_pixel_y) source_img = rf_img.transform((256,256),Image.EXTENT, (start_pixel_x, start_pixel_y, end_pixel_x, end_pixel_y)) source_img = source_img.resize((256,256)) source_img = source_img.convert('RGBA') #https://stackoverflow.com/questions/765736/using-pil-to-make-all-white-pixels-transparent pixdata = source_img.load() for y in xrange(source_img.size[1]): for x in xrange(source_img.size[0]): if pixdata[x, y] == (255, 255, 255, 255): pixdata[x, y] = (255, 255, 255, 0) # white to transparent elif pixdata[x, y] == (0, 0, 0, 255): pixdata[x, y] = (255, 255, 255, 0) # black to transparent if kwargs.get('blank_tiles') is not True and source_img.convert("L").getextrema() == (255, 255): print("skip tile: {0}".format(result_filename)) return if not os.path.exists(result_dir): logger.debug("create directory: {0}".format(result_dir)) os.makedirs(result_dir) print("create tile: {0}".format(result_filename)) # save tile source_img.save(result_filename, "PNG")