简单图像盒算法
boxmath的Python项目详细描述
这为链接多个大小调整提供了图像裁剪/大小调整算法, 裁剪操作并生成结果裁剪/调整操作对。
用法
用法相当简单:
fromboxmathimportbox,resize,crop,size,make_transformerfromwandimportimage# Load the image to get its width and heighti=image.Image(filename="chrysanthemum.jpg")b=box(i.width,i.height)# manipulate the virtual imageb=resize(b,629,483)b=crop(b,0,0,480,480)b=resize(b,1000,1000)# renderdefresizer(img,w,h):img.resize(int(w),int(h),filter=FILTER)returnimgdefcropper(img,l,t,r,b):img.crop(int(l),int(t),int(r),int(b))returnimgt=make_transformer(b,resizer,cropper)i=t(i)i.save(filename="chrysanthemum-1000x1000.jpg")
通常,如果我们想直接使用魔杖或PIL,每个调整都会 降低图像质量。先缩小再增大的动作 会破坏图像的质量;借助数学的力量,我们 仅在需要渲染图像时应用“调整大小”和“裁剪”。
不是宽度、高度、左侧、顶部、右侧和底部的值 传递给resizer和cripper函数的类型转换为int。
这是因为它们要么是fractions.fraction()实例,要么是 整数()。boxmath使用分数类确保 调整大小和裁剪。