def shitch_to_transparent(self, img, tolerance=0, max_size=None): img = Image.open(img) if max_size: img = convert_obj_image(img, width=max_size, height=max_size, enlarge=False) img = img.convert("RGBA") if not img.size[0] or not img.size[1]: return img datas = ImageMatrix(img.getdata(), *img.size) newData = self.flood_fill(datas, 0, 0, (255, 255, 255, 0), tolerance=tolerance) ''' corner_point = datas.get(0, 0) newData = [] for item in datas: if item[0] == 255 and item[1] == 255 and item[2] == 255: newData.append((255, 255, 255, 0)) else: newData.append(item) ''' output = StringIO() img.putdata(newData) img.save(output, "PNG") output.seek(0) return output.read()
def switch_to_transparent(self, src_file, dest_file, tolerance=0, max_size=None): content = open(src_file, 'rb').read() fp2 = cStringIO.StringIO(content) img = Image.open(fp2) if not img.size[0] or not img.size[1]: raise ValueError('wrong image file') if max_size: img = convert_obj_image(img, width=max_size, height=max_size, enlarge=False) img = img.convert("RGBA") output = StringIO() img.save(output, "PNG") output.seek(0) open(dest_file, 'wb').write(output.read()) src_file = dest_file cmd = "%(app)s %(src)s -bordercolor white -border 1x1 -matte -fill none -fuzz %(fuzz)s%% -draw 'matte 0,0 floodfill' -shave 1x1 %(dst)s" cmd %= { 'app': settings.IMAGEMAGIC_PATH, 'src': src_file, 'dst': dest_file, 'fuzz': tolerance } os.system(cmd)
def switch_to_transparent(self, src_file, dest_file, tolerance=0, max_size=None): content = open(src_file, 'rb').read() fp2 = cStringIO.StringIO(content) img = Image.open(fp2) if not img.size[0] or not img.size[1]: raise ValueError('wrong image file') if max_size: img = convert_obj_image(img, width=max_size, height=max_size, enlarge=False) img = img.convert("RGBA") output = StringIO() img.save(output, "PNG") output.seek(0) open(dest_file, 'wb').write(output.read()) src_file = dest_file cmd = "%(app)s %(src)s -bordercolor white -border 1x1 -matte -fill none -fuzz %(fuzz)s%% -draw 'matte 0,0 floodfill' -shave 1x1 %(dst)s" cmd %= {'app': settings.IMAGEMAGIC_PATH, 'src': src_file, 'dst': dest_file, 'fuzz': tolerance} os.system(cmd)