Python Image.NEAREST Examples

The following are 5 code examples of Image.NEAREST(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Image , or try the search function .
Example #1
Source File: ImageOps.py    From mxnet-lambda with Apache License 2.0 5 votes vote down vote up
def expand(image, border=0, fill=0):
    "Add border to image"
    left, top, right, bottom = _border(border)
    width = left + image.size[0] + right
    height = top + image.size[1] + bottom
    out = Image.new(image.mode, (width, height), _color(fill, image.mode))
    out.paste(image, (left, top))
    return out

##
# Returns a sized and cropped version of the image, cropped to the
# requested aspect ratio and size.
# <p>
# The <b>fit</b> function was contributed by Kevin Cazabon.
#
# @param size The requested output size in pixels, given as a
#     (width, height) tuple.
# @param method What resampling method to use.  Default is Image.NEAREST.
# @param bleed Remove a border around the outside of the image (from all
#     four edges.  The value is a decimal percentage (use 0.01 for one
#     percent).  The default value is 0 (no border).
# @param centering Control the cropping position.  Use (0.5, 0.5) for
#     center cropping (e.g. if cropping the width, take 50% off of the
#     left side, and therefore 50% off the right side).  (0.0, 0.0)
#     will crop from the top left corner (i.e. if cropping the width,
#     take all of the crop off of the right side, and if cropping the
#     height, take all of it off the bottom).  (1.0, 0.0) will crop
#     from the bottom left corner, etc. (i.e. if cropping the width,
#     take all of the crop off the left side, and if cropping the height
#     take none from the top, and therefore all off the bottom).
# @return An image. 
Example #2
Source File: service.py    From OCRbot with GNU Affero General Public License v3.0 5 votes vote down vote up
def check_image_background(image):
	im = convert_to_bw(image)
	im_smol = im.resize((int(x / 4) for x in im.size), Image.NEAREST)
	pixels = im_smol.getdata()
	black_threshold = 150 # Threshold doesn't really matter as the image is only 0 and 255
	nblack = 0
	n = len(pixels)
	for pixel in pixels:
		if pixel < black_threshold:
			nblack += 1

	if (nblack / float(n)) > 0.5:
		image = invert_image(image) # Invert image if more than half of the bw image is considered black
	return image 
Example #3
Source File: ImageOps.py    From CNCGToolKit with MIT License 5 votes vote down vote up
def expand(image, border=0, fill=0):
    "Add border to image"
    left, top, right, bottom = _border(border)
    width = left + image.size[0] + right
    height = top + image.size[1] + bottom
    out = Image.new(image.mode, (width, height), _color(fill, image.mode))
    out.paste(image, (left, top))
    return out

##
# Returns a sized and cropped version of the image, cropped to the
# requested aspect ratio and size.
# <p>
# The <b>fit</b> function was contributed by Kevin Cazabon.
#
# @param size The requested output size in pixels, given as a
#     (width, height) tuple.
# @param method What resampling method to use.  Default is Image.NEAREST.
# @param bleed Remove a border around the outside of the image (from all
#     four edges.  The value is a decimal percentage (use 0.01 for one
#     percent).  The default value is 0 (no border).
# @param centering Control the cropping position.  Use (0.5, 0.5) for
#     center cropping (e.g. if cropping the width, take 50% off of the
#     left side, and therefore 50% off the right side).  (0.0, 0.0)
#     will crop from the top left corner (i.e. if cropping the width,
#     take all of the crop off of the right side, and if cropping the
#     height, take all of it off the bottom).  (1.0, 0.0) will crop
#     from the bottom left corner, etc. (i.e. if cropping the width,
#     take all of the crop off the left side, and if cropping the height
#     take none from the top, and therefore all off the bottom).
# @return An image. 
Example #4
Source File: ImageOps.py    From keras-lambda with MIT License 5 votes vote down vote up
def expand(image, border=0, fill=0):
    "Add border to image"
    left, top, right, bottom = _border(border)
    width = left + image.size[0] + right
    height = top + image.size[1] + bottom
    out = Image.new(image.mode, (width, height), _color(fill, image.mode))
    out.paste(image, (left, top))
    return out

##
# Returns a sized and cropped version of the image, cropped to the
# requested aspect ratio and size.
# <p>
# The <b>fit</b> function was contributed by Kevin Cazabon.
#
# @param size The requested output size in pixels, given as a
#     (width, height) tuple.
# @param method What resampling method to use.  Default is Image.NEAREST.
# @param bleed Remove a border around the outside of the image (from all
#     four edges.  The value is a decimal percentage (use 0.01 for one
#     percent).  The default value is 0 (no border).
# @param centering Control the cropping position.  Use (0.5, 0.5) for
#     center cropping (e.g. if cropping the width, take 50% off of the
#     left side, and therefore 50% off the right side).  (0.0, 0.0)
#     will crop from the top left corner (i.e. if cropping the width,
#     take all of the crop off of the right side, and if cropping the
#     height, take all of it off the bottom).  (1.0, 0.0) will crop
#     from the bottom left corner, etc. (i.e. if cropping the width,
#     take all of the crop off the left side, and if cropping the height
#     take none from the top, and therefore all off the bottom).
# @return An image. 
Example #5
Source File: cropall.py    From cropall with GNU General Public License v3.0 4 votes vote down vote up
def load_imgfile(self, filename):
		self.currentName = filename
		fullFilename = os.path.join(self.inDir, filename)
		print "Loading " + fullFilename
		img = Image.open(fullFilename)

		self.imageOrig = img
		self.imageOrigSize = (img.size[0], img.size[1])
		print "Image is " + str(self.imageOrigSize[0]) + "x" + str(self.imageOrigSize[1])

		basewidth = 512
		wpercent = (basewidth/float(img.size[0]))
		hsize = int((float(img.size[1])*float(wpercent)))
		if fast_preview:
			#does NOT create a copy so self.imageOrig is the same as self.image
			img.thumbnail((basewidth,hsize), Image.NEAREST)
		else:
			if antialiase_original_preview:
				img = img.resize((basewidth,hsize), Image.ANTIALIAS)
			else:
				img = img.copy()
				img.thumbnail((basewidth,hsize), Image.NEAREST)
		self.image = img
		print "Resized preview"

		#self.geometry("1024x"+str(hsize + 100))
		self.configure(relief='flat', background='gray')

		self.imagePhoto = ImageTk.PhotoImage(self.image)
		self.imageLabel.configure(width=self.imagePhoto.width(), height=self.imagePhoto.height())
		self.imageLabel.create_image(0, 0, anchor=NW, image=self.imagePhoto)

		self.previewPhoto = ImageTk.PhotoImage(self.image)
		self.previewLabel.configure(image=self.previewPhoto)

		self.item = None

		self.verti_aux_item = None
		self.horiz_aux_item = None

		self.on_aspect_changed(None, None, None) #update aspect ratio with new image size

		#self.imageLabel.pack(side = "left", fill = "both", expand = "yes")
		#self.previewLabel.pack(side = "left", fill = "both", expand = "yes")
		#self.c.pack(side = "bottom", fill = "both", expand = "yes")

		#self.c.xview_moveto(0)
		#self.c.yview_moveto(0)
		#self.c.config(scrollregion=self.c.bbox('all'))