from PIL import Image from io import BytesIO from service.remove_bg_ali import RemoveBgALi as RemoveBg class Picture: def __init__(self, in_path): self.im = Image.open(in_path) self.x, self.y = self.im.size # print(self.x, self.y) def resize_regular(self, width, high): self.im = self.im.resize((width, high), Image.Resampling.LANCZOS) def save_img(self, out_path, quality=90): # self.im = self.im.convert("RGB") self.im.save(out_path, quality=quality) def crop_with_png(self): self.im = self.im.crop(self.im.getbbox()) self.x, self.y = self.im.size def resize(self, width=None, high=None): if width: re_x = int(width) re_y = int(self.y * re_x / self.x) else: re_y = int(high) re_x = int(self.x * re_y / self.y) self.im = self.im.resize((re_x, re_y)) self.x, self.y = self.im.size def save_to_io(self, format): img = BytesIO() self.im.save(img, format=format) # format: PNG or JPEG img.seek(0) # rewind to the start return img def corp_square(self): if self.y < self.x: return self.im = self.im.crop((0, int((self.y - self.x) / 2), self.x, self.y - int((self.y - self.x) / 2))) class PicDeal(object): def __init__(self): self.mode = "ali" def remove_bg(self, in_path, out_path): """ 1、三方软件进行抠图处理 2、图片进行清晰度转化,转化成原始图片尺寸 """ if Remove_Bg_Mode == "zuo_tang" or Remove_Bg_Mode == "ali": remove_bg_ins = RemoveBg() if remove_bg_ins.run(in_path, out_path): return True else: return False def create_800image(self, original_image, cut_image_path, out_path, image_mode=None): """ image_mode: 处理模式,主要用于图片缩放大小确定 original_image:原始图,用于获取商品阴影 cut_image_path:抠图结果 """ image = Picture(original_image) image.crop_with_png() # image.im.show() if image.x >= image.y: image.resize(width=575) else: image.resize(high=575) image_bg = Image.open("resources/goods_bg.jpg") image_bg_x, image_bg_y = image_bg.size image_x, image_y = image.x, image.y _x = int((image_bg_x - image_x) / 2) _y = int((image_bg_y - image_y) / 2) image_bg.paste(image.im, (_x, _y), image.im) # image_bg.show() image_bg.save(out_path) def resize_and_save(self, image_path, out_path, width): im = Image.open(image_path) x, y = im.size re_x = int(width) re_y = int(y * re_x / x) im = im.resize((re_x, re_y), Image.ANTIALIAS) im.save(out_path) def save(self, out_path): pass if __name__ == '__main__': pass