Python Image 模块,AFFINE 实例源码
我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用Image.AFFINE。
def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC):
if (scale is None) and (center is None):
return image.rotate(angle=angle, resample=resample)
nx,ny = x,y = center
sx=sy=1.0
if new_center:
(nx,ny) = new_center
if scale:
(sx,sy) = (scale, scale)
cosine = math.cos(angle)
sine = math.sin(angle)
a = cosine/sx
b = sine/sx
c = x-nx*a-ny*b
d = -sine/sy
e = cosine/sy
f = y-nx*d-ny*e
return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
def get_image_src2():
filename = "test.png"
image0 = Image.open(filename)
image1 = Image.open(filename)
datas = image0.getdata()
newdata = []
for item in datas:
if item[0] == 0 and item[1] == 0 and item[2] == 0: # Black -> White
newdata.append((255, 255, 255))
else:
newdata.append((0, 0, 0))
image0.putdata(newdata)
image0.save("st1.png")
image1 = image0.crop(image1.getbBox())
image1.save("step3.5.png")
w1, h1 = image1.size
image2 = Image.new("RGB", (28, 28), (255, 255))
image1.thumbnail((20, 20), Image.ANTIALIAS)
image2.paste(image1, (0, 0))
image2.save("step4.png")
digit_image = mpimg.imread("step4.png")
gray_digit = np.dot(digit_image[...,:3], [0.299, 0.587, 0.114])
gray_digit = gray_digit.flatten()
for i in range(len(gray_digit)):
gray_digit[i] = 1.0 - gray_digit[i]
gray_digit[i] = round(gray_digit[i], 8)
# Calculating center of mass of the image
x, y = ndimage.measurements.center_of_mass(gray_digit.reshape(28, 28))
image2 = image2.transform(image2.size, (1, y - 14, 1, x - 14), fill=0)
image2 = Image.new("RGB", 255))
image2.paste(image1, (14 - int(round(y, 0)), 14 - int(round(x, 0))))
image2.save("step6.png")
return "step6.png"
def convert_im(code, image1):
image1 = image1.crop(image1.getbBox())
w1, 255))
datas = image1.getdata()
newdata = []
"""
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255: # Red -> Black
newdata.append(white)
elif item[0] == 0 and item[1] == 0 and item[2] == 0: # Black -> White
newdata.append(white)
else:
newdata.append(black)
"""
#image1.putdata(newdata)
image1.thumbnail((20, 0))))
image2.save(chr(code) + str(time.time()) + ".png")
def convert_im(code, 28))
if math.isnan(x) or math.isnan(y):
return
image2 = image2.transform(image2.size, 0))))
image2.save(chr(code) + str(time.time()) + ".png")
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。