FHPythonUtils/GimpFormats

Feature: Add saving functionality to library

Closed this issue · 4 comments

System info

$ uname -a
Linux 5.14.0-4-amd64 #1 SMP Debian 5.14.16-1 (2021-11-03) x86_64 GNU/Linux

$ python3 --version
Python 3.9.9

$ pip freeze
aenum==3.1.5
binaryiotools==2021.0.3
blendmodes==2021.3.3
brackettree==0.2.5
deprecation==2.1.0
gimpformats==2021.1.3
numpy==1.21.4
packaging==21.3
Pillow==8.4.0
pyparsing==3.0.6

Describe the bug

Opening an XCF file and then saving, with either save or saveNew methods, results in a corrupt XCF file which can't be opened with gimpformats or Gimp. Test file attached test.zip.

Expected outcome

Creates an identical or at least valid XCF file.

Actual outcome

Python 3.9.9 (main, Nov 16 2021, 10:24:31) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from gimpformats.gimpXcfDocument import GimpDocument
>>> d = GimpDocument('test.xcf')
>>> d.saveNew('/tmp/test.xcf')
>>> d = GimpDocument('/tmp/test.xcf')
Traceback (most recent call last):
  File ".../lib/python3.9/site-packages/binaryiotools/__init__.py", line 118, in _read
    data = struct.unpack(fmt, self.data[self.index : self.index + size])[0]
struct.error: unpack requires a buffer of 4 bytes

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.9/site-packages/gimpformats/gimpXcfDocument.py", line 81, in __init__
    self.load(fileName)
  File ".../lib/python3.9/site-packages/gimpformats/gimpXcfDocument.py", line 89, in load
    self.decode(data)
  File ".../lib/python3.9/site-packages/gimpformats/gimpXcfDocument.py", line 134, in decode
    self._propertiesDecode(ioBuf)
  File ".../lib/python3.9/site-packages/gimpformats/GimpIOBase.py", line 669, in _propertiesDecode
    self._propertyDecode(propertyType, ioBuf.getBytes(dataLength))
  File ".../lib/python3.9/site-packages/gimpformats/GimpIOBase.py", line 383, in _propertyDecode
    self.blendMode = ioBuf.u32
  File ".../lib/python3.9/site-packages/binaryiotools/__init__.py", line 362, in u32
    return self.u32be
  File ".../lib/python3.9/site-packages/binaryiotools/__init__.py", line 515, in u32be
    return self._read(4, ">I")
  File ".../lib/python3.9/site-packages/binaryiotools/__init__.py", line 120, in _read
    raise Exception(
Exception: >I 4 0 0 b''

Hi thanks for your issue

Unfortunately this isn't currently supported and probably won't be anytime soon to be honest

You, or anyone else is more than welcome to open a Merge Request for this feature

That explains why all the versions I tried had the same issue 😄 I don't know how to implement the saving (layeredimage also doesn't support saving) but I will create a PR just to throw a NotImplemented exception to make it clearer that it's not supported.

I was only trying this to track down another issue I'm having.

Oh yeah that's a good idea! Thank you 😊

It was something that I had a decent look at but I think there's loads of broken bits of ex python 2 code and the codebase is rather large and at times hard to follow

It probably needs bits rewriting in more modern pythonic ways but that'll take more time than I have at present

Probably going to call this solved even though we don't have the saving functionality as there is some useful output for not implemented now. Thanks for the issue and your prs :)