From aba2d2b09855ccd3c1027d962050b9500747ed85 Mon Sep 17 00:00:00 2001 From: Mickael B Date: Sat, 17 Sep 2016 04:03:40 -0400 Subject: [PATCH] [SGI] Save uncompressed SGI/BW/RGB/RGBA files Save feature added to SgiImagePlugin.py, uncompressed method only --- PIL/SgiImagePlugin.py | 73 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/PIL/SgiImagePlugin.py b/PIL/SgiImagePlugin.py index d2efd3e25..e70387e67 100644 --- a/PIL/SgiImagePlugin.py +++ b/PIL/SgiImagePlugin.py @@ -2,14 +2,17 @@ # The Python Imaging Library. # $Id$ # -# SGI image file handling +# SGI image file handling (hack version) # # See "The SGI Image File Format (Draft version 0.97)", Paul Haeberli. # # +# # History: +# 2016-16-10 mb Add save method without compression # 1995-09-10 fl Created # +# Copyright (c) 2016 by Mickael Bonfill. # Copyright (c) 2008 by Karsten Hiddemann. # Copyright (c) 1997 by Secret Labs AB. # Copyright (c) 1995 by Fredrik Lundh. @@ -19,8 +22,10 @@ from PIL import Image, ImageFile, _binary +import struct +import os -__version__ = "0.2" +__version__ = "0.3" i8 = _binary.i8 i16 = _binary.i16be @@ -76,12 +81,74 @@ class SgiImageFile(ImageFile.ImageFile): elif compression == 1: raise ValueError("SGI RLE encoding not supported") + +def _save(im, fp, filename): + if im.mode != "RGB" and im.mode != "RGBA" and im.mode != "L": + raise ValueError("Unsupported SGI image mode") + + im = im.transpose(Image.FLIP_TOP_BOTTOM) # Flip the image, since the origin of SGI + # file is the bottom-left corner + + magicNumber = 474 # Define the file as SGI File Format + rle = 0 # Run-Length Encoding Compression - Unsupported + bpc = 1 # Byte-per-pixel precision, 1 = 256bits per pixel + dim = 3 # Number of dimensions (x,y,z) + x, y = im.size # X Dimension / Y Dimension + if im.mode == "L" and y == 1: + dim = 1 + elif im.mode == "L": + dim = 2 + z = len(im.mode) # Z Dimension: Number of channels + if dim == 1 or dim == 2: + z = 1 + pinmin = 0 # Minimum Byte value + pinmax = 255 # Maximum Byte value + imgName = os.path.splitext(os.path.basename(filename))[0][0:78] # Image name (79 characters max) + colormap = 0 # Standard representation of pixel in the file + channels = [] + for channelIndex in range(0, z): + channelData = list(im.getdata(channelIndex)) + channels.append(channelData) + fp.write(struct.pack('>h', magicNumber)) + fp.write(struct.pack('c', chr(rle))) + fp.write(struct.pack('c', chr(bpc))) + fp.write(struct.pack('>H', dim)) + fp.write(struct.pack('>H', x)) + fp.write(struct.pack('>H', y)) + fp.write(struct.pack('>H', z)) + fp.write(struct.pack('>l', pinmin)) + fp.write(struct.pack('>l', pinmax)) + for i in range(0, 4): + fp.write(struct.pack('c', chr(0))) + for c in imgName: + fp.write(struct.pack('c', c)) + fp.write(struct.pack('c', chr(0))) + if len(imgName) < 78: + charIndex = len(imgName) + for charIndex in range(len(imgName), 79): + fp.write(struct.pack('c', chr(0))) + fp.write(struct.pack('>l', colormap)) + for i in range(0, 404): + fp.write(struct.pack('c', chr(0))) + for zChannel in range(0, z): + dIndex = 0 + for yPos in range(0, y): + for xPos in range(0, x): + fp.write(struct.pack('c', chr(channels[zChannel][dIndex]))) + dIndex += 1 + fp.close() + + # # registry Image.register_open(SgiImageFile.format, SgiImageFile, _accept) - +Image.register_save(SgiImageFile.format, _save) +Image.register_mime(SgiImageFile.format, "image/sgi") +Image.register_mime(SgiImageFile.format, "image/rgb") Image.register_extension(SgiImageFile.format, ".bw") Image.register_extension(SgiImageFile.format, ".rgb") Image.register_extension(SgiImageFile.format, ".rgba") Image.register_extension(SgiImageFile.format, ".sgi") + +# End of file