Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pixmap x- and y-coordinate are immutable #645

Closed
hex-yes opened this issue Sep 5, 2020 · 8 comments
Closed

Pixmap x- and y-coordinate are immutable #645

hex-yes opened this issue Sep 5, 2020 · 8 comments
Assignees
Labels

Comments

@hex-yes
Copy link

hex-yes commented Sep 5, 2020

Description of the bug

This might be a documentation bug rather than a functional one. I need to copy a region of a source Pixmap to a destination Pixmap. The recipe for doing so does not work as written.

To Reproduce

The recipe code fails at this line:

src.x = src.width * i # modify input's x coord

...with the error:

AttributeError: can't set attribute

Expected behavior

If the x and y coordinates of a Pixmap are intended to be mutable, this is a functional bug and the expected behavior would be as shown in the recipe.

If the x and y coordinates of a Pixmap are intended to be immutable, some documentation of the current best practice for copying a portion of one Pixmap to another would be welcomed. For example, is it necessary to apply a translation Matrix to the source Pixmap before copying, or is there a better way?

My configuration

>>> print(sys.version, "\n", sys.platform, "\n", fitz.__doc__)
3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) 
[Clang 6.0 (clang-600.0.57)] 
 darwin 
 
PyMuPDF 1.17.5: Python bindings for the MuPDF 1.17.0 library.
Version date: 2020-08-06 06:31:06.
Built for Python 3.8 on darwin (64-bit).

PyMuPDF was installed with a wheel using pip install PyMuPDF

@hex-yes
Copy link
Author

hex-yes commented Sep 5, 2020

Related to this, the documentation for Pixmap.copyPixmap includes this text:

Between irect and the target pixmap’s rectangle, an “intersection” is calculated at first. This takes into account the rectangle coordinates and the current attribute values source.x and source.y (which you are free to modify for this purpose).

This suggests the intent is consistent with the recipe code that the x- and y-coordinate of a Pixmap should be settable.

@JorjMcKie
Copy link
Collaborator

Correct - thanks for reporting this.
Will change with next version. Any urgency?

@hex-yes
Copy link
Author

hex-yes commented Sep 5, 2020

No urgency at all. Thanks for your work on this.

@JorjMcKie
Copy link
Collaborator

New version 1.17.7 addresses this and is being uploaded right now.

@hex-yes
Copy link
Author

hex-yes commented Sep 14, 2020

I updated to 1.17.7 with pip, but the issue persists. My updated configuration:

>>> print(sys.version, "\n", sys.platform, "\n", fitz.__doc__)
3.8.5 (v3.8.5:580fbb018f, Jul 20 2020, 12:11:27) 
[Clang 6.0 (clang-600.0.57)] 
 darwin 
 
PyMuPDF 1.17.7: Python bindings for the MuPDF 1.17.0 library.
Version date: 2020-09-14 06:33:06.
Built for Python 3.8 on darwin (64-bit).

@JorjMcKie
Copy link
Collaborator

Weird, here is my snippet:

>>> import fitz
>>> print(fitz.__doc__)

PyMuPDF 1.17.7: Python bindings for the MuPDF 1.17.0 library.
Version date: 2020-09-14 06:33:06.
Built for Python 3.7 on win32 (64-bit).

>>> pix=fitz.Pixmap("page8.png")
>>> pix
Pixmap(DeviceRGB, IRect(0, 0, 596, 794), 0)
>>> pix.setOrigin(5, 5)
>>> pix
Pixmap(DeviceRGB, IRect(5, 5, 601, 799), 0)
>>> 

@JorjMcKie
Copy link
Collaborator

The attributes pix.x, pix.y can indeed not be changed directly - that hasn't changed ...
This is rooted in an internal change of MuPDF.

@hex-yes
Copy link
Author

hex-yes commented Sep 14, 2020

setOrigin() does work as described. I modified the recipe code to use setOrigin() as follows:

import fitz

src = fitz.Pixmap('img-7edges.png')      # create pixmap from a picture
col = 3                                  # tiles per row
lin = 4                                  # tiles per column
tar_w = src.width * col                  # width of target
tar_h = src.height * lin                 # height of target

# create target pixmap
tar_pix = fitz.Pixmap(src.colorspace, (0, 0, tar_w, tar_h), src.alpha)

# now fill target with the tiles
for i in range(col):
    for j in range(lin):
        src.setOrigin(src.width * i, src.height * j)
        tar_pix.copyPixmap(src, src.irect) # copy input to new loc

tar_pix.writePNG('tar.png')

Thank you for your help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants