我们从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, 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, Image.AFFINE, (1, 0, y - 14, 0, 1, x - 14), fill=0) image2 = Image.new("RGB", (28, 28), (255, 255, 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, h1 = image1.size image2 = Image.new("RGB", (28, 28), (255, 255, 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, 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, Image.AFFINE, (1, 0, y - 14, 0, 1, x - 14), fill=0) image2 = Image.new("RGB", (28, 28), (255, 255, 255)) image2.paste(image1, (14 - int(round(y, 0)), 14 - int(round(x, 0)))) image2.save(chr(code) + str(time.time()) + ".png")
def convert_im(code, image1): image1 = image1.crop(image1.getbbox()) w1, h1 = image1.size image2 = Image.new("RGB", (28, 28), (255, 255, 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, 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)) if math.isnan(x) or math.isnan(y): return image2 = image2.transform(image2.size, Image.AFFINE, (1, 0, y - 14, 0, 1, x - 14), fill=0) image2 = Image.new("RGB", (28, 28), (255, 255, 255)) image2.paste(image1, (14 - int(round(y, 0)), 14 - int(round(x, 0)))) image2.save(chr(code) + str(time.time()) + ".png")