Comments (9)
Ok, thanks for the hint concerning wand (never heard of it before).
I'll have a look into it.
from pymupdf-utilities.
In addition, if I don’t set the alpha, the wrong color also don’t appear in the same position. So I would like to know what happens when we use setAlpha method and why applied the samples value to set alpha value?
from pymupdf-utilities.
I found there are several pixel which has different value and color from what we see in PDF
Hard to tell without seeing an example PDF and the code snippet extracting the PNG.
In general, transparent images in PDF are stored as separate sub images: one image contains the color pixels, e.g. (R, G, B) for an RGB image, and the other the alphas (transparency in formation).
If you extract such an image and make a pixmap of it, it will contain the 4-tuple (R, G, B, A) for each pixel. All these 4 values are integers from 0 to 255. In case of the alpha value A, this is interpreted as transparency, e.g. 128 => 128/255 ~ 50%.
The setAlpha
method changes all those alpha values in a pixmap - based on an array of integers. If all values are set to 255, the image will no longer be transparent. If 0, it will be fully transparent and you won't see anything any more, etc.
from pymupdf-utilities.
Closing for lack of reaction.
from pymupdf-utilities.
I am having the same issue. however some images get fixed when i wrap it with fitz.Pixmap(fitz.csRGB, fitz.Pixmap(pix1))
right before setting the alpha. but when I do that some other images get these weird artifacts again.
this is without the conversion before applying the alpha:
this is with the fitz.csRGB
conversion
you can reproduce this by changing the recoverpix function.
this is the code I used (its basically a simple version of recoverpix)
def recoverpix(doc, item):
x = item[0] # xref of PDF image
s = item[1] # xref of its /SMask
pix1 = fitz.Pixmap(doc, x)
if s == 0:
return fitz.Pixmap(fitz.csRGB, pix1)
pix2 = fitz.Pixmap(doc, s)
#pix3 = fitz.Pixmap(fitz.csRGB, fitz.Pixmap(pix1)) # with conversion
pix3 = fitz.Pixmap(pix1) # without conversion
pix3.setAlpha(pix2.samples)
return fitz.Pixmap(fitz.csRGB, pix3)
what would be perfect is if I could somehow detect if the conversion is needed
any idea @JorjMcKie ?
edit:
its not very nice but for now i managed to fix it by applying the mask using the wand library rather then using the setAlpha function:
def apply_mask(image, mask, invert=False):
image.alpha_channel = True
if invert:
mask.negate()
with Image(width=image.width, height=image.height, background=Color("transparent")) as alpha_image:
alpha_image.composite_channel(
"alpha",
mask,
"copy_alpha",
0, 0)
image.composite_channel(
"alpha",
alpha_image,
"multiply",
0, 0)
def recoverpix(doc, item):
x = item[0] # xref of PDF image
s = item[1] # xref of its /SMask
pix1 = fitz.Pixmap(doc, x)
if s == 0:
return fitz.Pixmap(fitz.csRGB, pix1), None
pix2 = fitz.Pixmap(doc, s)
pix3 = fitz.Pixmap(fitz.csRGB, fitz.Pixmap(pix1))
return pix3, pix2
pix, pix_alpha = recoverpix(self.fitz_pdf, img)
with Image(blob=pix.getPNGData()) as image:
with image.clone() as image:
if pix_alpha is not None:
with Image(blob=pix_alpha.getPNGData()) as image_a:
with image_a.clone() as image_a:
apply_mask(image, image_a)
image.save(filename=path)
from pymupdf-utilities.
Ok, thanks for the hint concerning wand (never heard of it before).
I'll have a look into it.
No problem. I made some small changes to the code tog et it a little cleaner but the functionality is still the same. (edited the original post) I hope this helps finding the issue with the random pixels
from pymupdf-utilities.
@meruiden
My approach to recover the original, transparency-loaden image simply is too naive.
MuPDF actually does provide the necessary functionality for recovering that original. I erroneously thought I can take this type of shortcut.
What I will do, is reworking the doc.extractImage()
method such that it uses MuPDF more consequently and either hide any smask xrefs (e.g. by always set them to 0) in the response dictionary, and / or change the example scripts containing that cited type of code.
from pymupdf-utilities.
@meruiden - could you please send me one of those problem examples?
Made a few changes and would like to test ...
from pymupdf-utilities.
@meruiden - in the meantime I have changed my position on this: I gave up resolving issues around transparent images using MuPDF. There simply is no reliable way to do it (or I am too dumb figuring it out).
Their own code also has defects around the same topic. If you page.apply_redactions()
overlapping transarent images, those images will be updated ignoring their /SMask. Similarly if you extract page text to HTML, transparent images are not processed correctly, etc.
Instead I am using PIL/Pillow. The image extraction scripts in the examples directory already are updated accordingly. I hope they are worry-free to use now.
from pymupdf-utilities.
Related Issues (20)
- Suggestion for Jupyter notebooks HOT 3
- ModuleNotFoundError: No module named 'ParseTab' HOT 2
- export-toc script outputs invalid csv when bookmark entry has newline character
- anonymize.py raises UnicodeDecodeError HOT 2
- libcrypt.so.2: cannot open shared object file: No such file or directory HOT 3
- multi_column.py does not identify multiple columns in some cases HOT 3
- How to use fitz to delete tables in PDF? Need help, Thanks. HOT 1
- document rescale after replacing fonts HOT 1
- fitzcli.py open file as in-memory stream & parameter for printing instead of writing to .txt HOT 1
- multi_column.py errors with latest version of pymupdf HOT 4
- Use a template for documenting the examples
- Make sure the examples are PEP8 compliant HOT 1
- Merge the demo scripts into the examples folder HOT 2
- Update links in Read the Docs HOT 3
- Merge the conversion scripts into the examples folder
- Merge the image-replacement scripts into the examples folder
- Merge the font-replacement scripts into the examples folder
- Merge the text-extraction scripts into the examples folder
- Merge the textbox-extraction scripts into the examples folder
- Define a folder structure for the examples
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pymupdf-utilities.