def psd_stream_to_svg(psd_stream): """ converts a psd file path to an svg :param psd_stream: the psd stream :returns: the svg string """ psd = PSDImage.from_stream(psd_stream) return psd_to_svg(psd)
def upload_file(): try: psd_file = request.files['psd_file'].stream psd = PSDImage.from_stream(psd_file) except: abort(400, "Unable to parse uploaded file as PSD.") try: svg_str = psd_to_svg(psd).encode('utf8') return Response(svg_str, mimetype='image/svg+xml') except: abort(500, "Unable to handle request.")
def image_file_to_clipboard(fileObj, is_psd=False): '''Read an image file and write the image data into clipboard. See http://stackoverflow.com/questions/7050448/write-image-to-windows-clipboard-in-python-with-pil-and-win32clipboard ''' from psd_tools import PSDImage if is_psd: psd = PSDImage.from_stream(fileObj) image = psd.as_PIL() elif isinstance(fileObj, Image.Image): image = fileObj else: image = Image.open(fileObj) image_to_clipboard(image)
def image_file_to_clipboard(fileObj, is_psd=False): '''Read an image file and write the image data into clipboard. See http://stackoverflow.com/questions/7050448/write-image-to-windows-clipboard-in-python-with-pil-and-win32clipboard ''' if is_psd: psd = PSDImage.from_stream(fileObj) image =psd.as_PIL() else: image = Image.open(fileObj) sio = StringIO() image.convert('RGB').save(sio, 'BMP') data = sio.getvalue()[14:] sio.close() win32clipboard.OpenClipboard() try: win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data) finally: win32clipboard.CloseClipboard()
def checkAndConvertImage(self, data, fname, imgType): """ Helper function that returns a **PIL.Image** object and the binary image data of subject to the following transformations: * if the images is in PSD format, convert it to PNG and extract text content from the file * for item image, image are sized down to 800x800 or 1200x1200 depending on the original size. If the input image is not square, it will be centered appropriately vertically or horizontally. * for description image, if the image width exceeds 790, it is resized to 790 while keeping the image aspect ratio """ data = b64decode(data) if fname.endswith('.psd'): psd = PSDImage.from_stream(io.BytesIO(data)) content = extractTextFromPSD(psd) img = psd.as_PIL() else: try: img = PIL.Image.open(io.BytesIO(data)) except OSError: raise RPCUserError('该文件不是图片格式。') if img.format not in ('JPEG', 'PNG', 'GIF'): raise RPCUserError('只支持jpg、png、gif格式的图片。') content = None modified = False if imgType == 'item': if img.width < 800 and img.height < 800: raise RPCUserError('商品主图宽度和高度必须至少有一个超过800。') if len(data) > 500 * 1024: raise RPCUserError('商品主图大小不能超过500KB。') out_size = 1200 if img.width >= 1200 or img.height >= 1200 else 800 ratio = min(out_size / img.width, out_size / img.height) w, h = int(img.width * ratio), int(img.height * ratio) if w != out_size or h != out_size: img = img.resize((w, h), PIL.Image.LANCZOS) out_img = PIL.Image.new('RGB', (out_size, out_size), 'white') out_img.paste(img, ((out_size - w) // 2, (out_size - h) // 2)) modified = True img = out_img else: # this is image for description if img.width < 790: raise RPCUserError('商品描述图片宽度最小为790。') if not 0.3 <= img.height / img.width <= 2: raise RPCUserError('商品描述图片高宽比(高度/宽度)必须在0.3-2之间。') if img.width > 790: img = img.resize((790, int(790 / img.width * img.height)), PIL.Image.LANCZOS) modified = True if modified: img.format = 'JPEG' stream = io.BytesIO() img.save(stream, img.format, optimize=True, quality=95) data = stream.getvalue() if len(data) > 2 * 1024 * 1024: raise RPCUserError('图片大小不能超过2MB。') return img, data, content
def psd_image_factory(data, *args): try: return PSDImage.from_stream(data).as_PIL() except Exception: raise TypeError
def psd_image_factory(data, *args): return PSDImage.from_stream(data).as_PIL()