我正在尝试提供一个函数,该函数可以将某些查询参数验证为位,并且不会将字符串值强制为0(甚至可以处理数字字符串输入)。我希望将字符串输入的值评估为from PIL import Image, ImageFont, ImageDraw
__author__ = 'Nikolas'
ASCII_SHADING_CHARS = ['M', 'W', 'N', 'Q', 'B', 'H', 'K', 'R', '#', 'E', 'D', 'F', 'X', 'O', 'A', 'P', 'G', 'U', 'S',
'V', 'Z', 'Y', 'C', 'L', 'T', 'J', '$', 'I', '*', ':', '.', ' '] # from darkest to lightest 32
#ASCII_SHADING_CHARS = ASCII_SHADING_CHARS[::-1]
class Asciify:
def __init__(self, img, new_width=500):
self.width, self.height = img.size # image.size returns a 2 tuple (width, height) in pixels
self._nw = new_width
self._nh = int(new_width * self.height / self.width)
self.im = img
def grayify_and_resize(self):
"""
Split the GIF into individual frames. Resize and convert each frame to monochrome.
:returns: a list of resized black&white Image objects
"""
new_width = self._nw
new_height = self._nh
num_frames = self.im.n_frames # number of frames in the .gif animation
result_list = []
for i in range(0, num_frames - 1):
# convert to mode L (b&w); resize to new dimensions
result_list.append(self.im.convert('L').resize((int(new_width), new_height)))
self.im.seek(self.im.tell() + 1) # move to the next frame in the gif animation
return result_list
def ascii_map(self, im_list, color_width=int(255 / len(ASCII_SHADING_CHARS))):
"""
Maps an ascii shading character to a pixel of each frame of the GIF
:param im_list: a list of black and white Image objects
:param color_width: determines the color intensity of each pixel
:returns: a list of each frame of the gif converted to ascii pixels
"""
ascii_image_list = [] # unformatted ascii images; needs to be broken into proper rows and columns
result_list = [] # ascii_image_list broken into proper rows and columns; how convinient
for image in im_list:
pixels = image.getdata() # color data on every pixel per image
append_list = [] # temporary list to append to ascii_image_list
for pixel_value in pixels:
index = int(pixel_value // color_width)
if index >= len(ASCII_SHADING_CHARS):
append_list.append(ASCII_SHADING_CHARS[-1])
else:
append_list.append(ASCII_SHADING_CHARS[index]) # 'replace' pixel with ascii char
ascii_image_list.append(append_list) # adds an element to ascii_image_list containing every pixel for image
for ascii_image in ascii_image_list:
ascii_string = "".join(ascii_image)
result_list.append([ascii_string[index:index + self._nw]
for index in range(0, len(ascii_string), self._nw)])
return result_list
def gifify(self, ascii_image_list):
"""
Return the ascii strings to .gif format for use in a traditional image viewer.
:param ascii_image_list: A list of ascii 'pixel' images
:returns: None
"""
# 7 = nw*4, nh*10
# 5 = nw*3, nh*8
font = ImageFont.truetype('ascii.ttf', 5) # set font and font size
ascii_image_strings = ['\n'.join(image) for image in ascii_image_list]
ascii_image_gifs = []
for image in ascii_image_strings:
#print(image)
if image == ascii_image_strings[0]:
continue
temp_image = Image.new('RGBA', (self._nw*3, self._nh*8), (255,255,255,0)) # should be transparent, didn't work
image_draw = ImageDraw.Draw(temp_image)
image_draw.text((0, 0), image, font=font, fill='black')
temp_image.resize((self._nw, self._nh))
ascii_image_gifs.append(temp_image)
if image == ascii_image_strings[-1]:
save_as = Image.new('RGBA', (self._nw*3, self._nh*8), (0, 0, 0, 0)) # should also be transparent, didn't work
save_as.save('temp.gif', save_all=True, append_images=ascii_image_gifs, loop=0, fps=24)
save_as.close()
if __name__ == "__main__":
im = Image.open("trippy.gif")
asciify_im = Asciify(im, 110)
gif_list = asciify_im.grayify_and_resize()
ascii_images = asciify_im.ascii_map(gif_list)
asciify_im.gifify(ascii_images)
# Debugging ascii image creation #
# outfile = open("outfile.txt", 'w')
# for image in ascii_images:
# outfile.write("\n".join(image) + '\n\n')
。
我有一个用于强制转换为NULL
的存储过程,因为BIT(1)
不是做这种事情的有效方法,我认为这很聪明:
CAST(x AS BIT)
此操作成功将任何数字转换为0或1,并且CREATE FUNCTION `CAST_TO_BIT`(
`input` INT
)
RETURNS BIT(1)
...
BEGIN
RETURN input;
END
将返回为NULL
。但是,在使用该函数结束时,我意识到MySQL甚至会在到达函数代码之前将字符串输入参数转换为NULL
。问题是0
实际上是一个有效的返回值(我希望将字符串强制转换为0
)。
因此,我决定改为将数字输入强制为字符串:
NULL
现在,我涉及CREATE FUNCTION `CAST_TO_BIT`(
`input` VARCHAR(24)
)
RETURNS BIT(1)
...
BEGIN
RETURN CAST(CASE WHEN input REGEXP '^-?[0-9]+$' THEN input ELSE NULL END AS INT);
END
/个条件,该函数是一团糟;使用REGEXP
,我可以看到该函数比以前慢了大约6倍。我该如何处理这些错误的字符串输入?或多或少通常是一个错误的库将BENCHMARK
强制转换为空字符串。