def file_to_img(width, height, filename):
# Do a check for file type
im = Image.open(filename).convert('RGBA')
im = im.resize((Settings.actualWidth, Settings.height), Image.NEAREST)
return im
# Takes index as an input at returns all indices for the character that index contains
python类NEAREST的实例源码
def create_crt_overlay(self):
crt_img = Image.new("RGB", (1, Settings.height*2), (0,0,0))
# Create repeating data for crt overlay
imgdata = ((200,200,200),(50,50,50))*Settings.height
crt_img.putdata(imgdata)
crt_img = crt_img.resize((Settings.width*2, Settings.height*2), Image.NEAREST)
return crt_img
# Returns the pixels visible at the current zoom level
def preview_putimg(self, ui_img):
self.unfilteredPreview = ui_img
pil_img = ui_to_pil(ui_img)
pil_img = pil_img.resize((Settings.width*2, Settings.height*2), Image.NEAREST)
pil_img = pil_img.convert("RGB")
pil_img = ImageChops.multiply(pil_img.filter(ImageFilter.SMOOTH), self.crt_overlay)
pil_img = ImageChops.screen(pil_img.filter(ImageFilter.GaussianBlur(5)), pil_img)
ui_img = pil_to_ui(pil_img)
self.superview['preview'].image = ui_img
return True
def set_background(self, np_array, color = False):
"""Takes a (numpy) array and sets this as background."""
if color:
img = Image.fromarray(np.flipud(np.uint8(np_array)), mode="RGB")
else:
img = Image.fromarray(np_array)
if self.background_image_id:
self.world_canvas.delete(self.background_image_id)
img = img.resize(self.extents, Image.NEAREST)
self.background_image = ImageTk.PhotoImage(img)
self.background_id = self.world_canvas.create_image(0, 0,
image=self.background_image, anchor=NW, tag="background")
# Make sure drawing order is correct.
self.set_display_order()
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.
def set_background(self, np_array, color = False):
"""Takes a (numpy) array and sets this as background."""
if color:
img = Image.fromarray(np.flipud(np.uint8(np_array)), mode="RGB")
else:
img = Image.fromarray(np_array)
if self.background_image_id:
self.world_canvas.delete(self.background_image_id)
img = img.resize(self.extents, Image.NEAREST)
self.background_image = ImageTk.PhotoImage(img)
self.background_id = self.world_canvas.create_image(0, 0,
image=self.background_image, anchor=NW, tag="background")
# Make sure drawing order is correct.
self.set_display_order()
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.
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.