Giter Site home page Giter Site logo

Comments (3)

hugovk avatar hugovk commented on September 17, 2024

And something similar on another file:

--- source
+++ first pass
@@ -1,6 +1,6 @@
-#
+# 
 # The Python Imaging Library.
 # $Id$
 #
 # SGI image file handling
 #
@@ -18,17 +18,14 @@
 # Copyright (c) 1997 by Secret Labs AB.
 # Copyright (c) 1995 by Fredrik Lundh.
 #
 # See the README file for information on usage and redistribution.
 #
-
-
 from . import Image, ImageFile
 from ._binary import i8, o8, i16be as i16
 import struct
 import os
-
 
 __version__ = "0.3"
 
 
 def _accept(prefix):
@@ -41,125 +38,110 @@
     (2, 1, 1): "L;16B",
     (2, 2, 1): "L;16B",
     (1, 3, 3): "RGB",
     (2, 3, 3): "RGB;16B",
     (1, 3, 4): "RGBA",
-    (2, 3, 4): "RGBA;16B"
+    (2, 3, 4): "RGBA;16B",
 }
 
 
 ##
 # Image plugin for SGI images.
 class SgiImageFile(ImageFile.ImageFile):
-
     format = "SGI"
     format_description = "SGI Image File Format"
 
     def _open(self):
-
         # HEAD
         headlen = 512
         s = self.fp.read(headlen)
-
         # magic number : 474
         if i16(s) != 474:
             raise ValueError("Not an SGI image file")
 
         # compression : verbatim or RLE
         compression = i8(s[2])
-
         # bpc : 1 or 2 bytes (8bits or 16bits)
         bpc = i8(s[3])
-
         # dimension : 1, 2 or 3 (depending on xsize, ysize and zsize)
         dimension = i16(s[4:])
-
         # xsize : width
         xsize = i16(s[6:])
-
         # ysize : height
         ysize = i16(s[8:])
-
         # zsize : channels count
         zsize = i16(s[10:])
-
         # layout
         layout = bpc, dimension, zsize
-
         # determine mode from bits/zsize
         rawmode = ""
         try:
             rawmode = MODES[layout]
         except KeyError:
             pass
-
         if rawmode == "":
             raise ValueError("Unsupported SGI image mode")
 
         self.size = xsize, ysize
         self.mode = rawmode.split(";")[0]
-
         # orientation -1 : scanlines begins at the bottom-left corner
         orientation = -1
-
         # decoder info
         if compression == 0:
             pagesize = xsize * ysize * bpc
             if bpc == 2:
-                self.tile = [("SGI16", (0, 0) + self.size,
-                              headlen, (self.mode, 0, orientation))]
+                self.tile = [
+                    ("SGI16", (0, 0) + self.size, headlen, (self.mode, 0, orientation))
+                ]
             else:
                 self.tile = []
                 offset = headlen
                 for layer in self.mode:
                     self.tile.append(
-                        ("raw", (0, 0) + self.size,
-                            offset, (layer, 0, orientation)))
+                        ("raw", (0, 0) + self.size, offset, (layer, 0, orientation))
+                    )
                     offset += pagesize
         elif compression == 1:
-            self.tile = [("sgi_rle", (0, 0) + self.size,
-                          headlen, (rawmode, orientation, bpc))]
+            self.tile = [
+                ("sgi_rle", (0, 0) + self.size, headlen, (rawmode, orientation, bpc))
+            ]
 
 
 def _save(im, fp, filename):
     if im.mode != "RGB" and im.mode != "RGBA" and im.mode != "L":
         raise ValueError("Unsupported SGI image mode")
 
     # Get the keyword arguments
     info = im.encoderinfo
-
     # Byte-per-pixel precision, 1 = 8bits per pixel
     bpc = info.get("bpc", 1)
-
     if bpc not in (1, 2):
         raise ValueError("Unsupported number of bytes per pixel")
 
     # Flip the image, since the origin of SGI file is the bottom-left corner
     orientation = -1
     # Define the file as SGI File Format
     magicNumber = 474
     # Run-Length Encoding Compression - Unsupported at this time
     rle = 0
-
     # Number of dimensions (x,y,z)
     dim = 3
     # X Dimension = width / Y Dimension = height
     x, y = im.size
     if im.mode == "L" and y == 1:
         dim = 1
     elif im.mode == "L":
         dim = 2
     # Z Dimension: Number of channels
     z = len(im.mode)
-
     if dim == 1 or dim == 2:
         z = 1
-
     # assert we've got the right number of bands.
     if len(im.getbands()) != z:
-        raise ValueError("incorrect number of bands in SGI write: %s vs %s" %
-                         (z, len(im.getbands())))
+        raise ValueError(
+            "incorrect number of bands in SGI write: %s vs %s" % (z, len(im.getbands()))
+        )
 
     # Minimum Byte value
     pinmin = 0
     # Maximum Byte value (255 = 8bits per pixel)
     pinmax = 255
@@ -181,18 +163,15 @@
     fp.write(struct.pack('4s', b''))  # dummy
     fp.write(struct.pack('79s', imgName))  # truncates to 79 chars
     fp.write(struct.pack('s', b''))  # force null byte after imgname
     fp.write(struct.pack('>l', colormap))
     fp.write(struct.pack('404s', b''))  # dummy
-
     rawmode = 'L'
     if bpc == 2:
         rawmode = 'L;16B'
-
     for channel in im.split():
         fp.write(channel.tobytes('raw', rawmode, 0, orientation))
-
     fp.close()
 
 
 class SGI16Decoder(ImageFile.PyDecoder):
     _pulls_fd = True
@@ -200,28 +179,24 @@
     def decode(self, buffer):
         rawmode, stride, orientation = self.args
         pagesize = self.state.xsize * self.state.ysize
         zsize = len(self.mode)
         self.fd.seek(512)
-
         for band in range(zsize):
             channel = Image.new('L', (self.state.xsize, self.state.ysize))
-            channel.frombytes(self.fd.read(2 * pagesize), 'raw',
-                              'L;16B', stride, orientation)
+            channel.frombytes(
+                self.fd.read(2 * pagesize), 'raw', 'L;16B', stride, orientation
+            )
             self.im.putband(channel.im, band)
-
         return -1, 0
 
-#
+
+# 
 # registry
-
-
 Image.register_decoder("SGI16", SGI16Decoder)
 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_extensions(SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"])
-
 # End of file
 

--- first pass
+++ second pass
@@ -195,8 +195,9 @@
 Image.register_decoder("SGI16", SGI16Decoder)
 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_extensions(SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"])
-# End of file
-
+Image.register_extensions(
+    SgiImageFile.format, [".bw", ".rgb", ".rgba", ".sgi"]
+)  # End of file
+

from black.

ambv avatar ambv commented on September 17, 2024

This is the same as #18. I'm working on a fix.

from black.

ambv avatar ambv commented on September 17, 2024

Thanks for your detailed reports! This is very helpful.

✨ 🍰 ✨

from black.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.