def processGIF(image: bytes) -> bytes: with tmpFile(ext=".gif") as file1, tmpFile(ext=".png") as file2: with open(file1, "wb") as f: f.write(image) with Image.open(file1) as f: f.save(file2, "PNG") with open(file2, "rb") as f: imageRead: bytes = f.read() return imageRead
def determineImageType(image: bytes) -> str: with tmpFile() as filename: with open(filename, "wb") as f: f.write(image) with Image.open(filename) as f: imageType: str = f.format return imageType.upper()
def textAlign( img: bytes, text: str, font: Optional[str] = "./data/font.otf", fontSize: Optional[int] = 100, fontColor: Optional[str] = "#FF0000", ) -> bytes: with tmpFile() as tf: with open(tf, "wb") as f: f.write(img) with Image.open(tf) as im: imageFont = ImageFont.truetype(font=font, size=fontSize) imageWidth, imageHeight = im.size textWidth, textHeight = imageFont.getsize(text) imageDraw = ImageDraw.Draw(im) textCoordinate = [ (imageWidth - textWidth) / 2, (imageHeight - textHeight) / 2, ] imageDraw.text(xy=textCoordinate, text=text, fill=fontColor, font=imageFont) im.save(tf, "PNG") with open(tf, "rb") as f: fileRead = f.read() return fileRead
def save(self) -> bytes: if self._messageStorage: self.update(self._messageStorage, force=True) wordcloud = self._wordcloud.generate_from_frequencies( self._wordFreqency) with tmpFile(ext=".png") as tmpFileName: wordcloud.to_file(tmpFileName) with open(tmpFileName, "rb") as f: fileRead = f.read() return convertImageFormat(fileRead)
def mosaicImage(img: bytes) -> bytes: with tmpFile() as tf: with open(tf, "wb") as f: f.write(img) with Image.open(tf) as im: blured = im.filter(ImageFilter.GaussianBlur(radius=10)) blured.save(tf, "PNG") with open(tf, "rb") as f: fileRead = f.read() imageWithText = textAlign(fileRead, "R-18") return convertImageFormat(imageWithText)
def resizeImage(image: bytes, *, height: Optional[int] = None, width: Optional[int] = None) -> bytes: with tmpFile() as tmpFileName: with open(tmpFileName, "wb") as f: f.write(image) with Image.open(tmpFileName) as img: originWidth, originHeight = img.size if height: width = int(originWidth * (height / originHeight)) elif width: height = int(originHeight * (width / originWidth)) else: raise AttributeError img.resize((width, height), Image.ANTIALIAS) img.save(tmpFileName, "PNG") with open(tmpFileName, "rb") as f: data = f.read() return data
def _toImage(grid: sns.FacetGrid) -> bytes: with tmpFile(ext=".png") as tmpFileName: grid.savefig(tmpFileName) with open(tmpFileName, "rb") as f: fileRead = f.read() return fileRead