Get rotation property of the extracted image #1297
-
Hi, My current goal is to extract the images from PDF together with some other properties. I also want to know if the inserted image is rotated and by how many degrees. I've found this one Get the transformation applied to an image For PDF file created using PyMuPDF: import fitz
from io import BytesIO
from PIL import Image
document = fitz.open('Via PyMuPDF.pdf')
for page_idx, page in enumerate(document):
for idx, img in enumerate(page.get_images(), start=1):
xref = img[0]
image = document.extract_image(xref)
image_bytes = image['image']
image_buffer = BytesIO(image_bytes)
pil_image = Image.open(image_buffer)
print(pil_image.info)
# ---- RESULT ----
# {'dpi': (96.012, 96.012)} For PDF file created using MS Word: import fitz
from io import BytesIO
from PIL import Image
document = fitz.open('Via MS Word.pdf')
for page_idx, page in enumerate(document):
for idx, img in enumerate(page.get_images(), start=1):
xref = img[0]
image = document.extract_image(xref)
image_bytes = image['image']
image_buffer = BytesIO(image_bytes)
pil_image = Image.open(image_buffer)
print(pil_image.info)
# ---- RESULT ----
# {'jfif': 257, 'jfif_version': (1, 1), 'dpi': (0, 0), 'jfif_unit': 1, 'jfif_density': (0, 0)} As you can see, they rendered different result. But still I've also tried examining if there is import fitz
doc = fitz.open('Via PyMuPDF.pdf')
for page in doc:
for img in page.get_images():
print(img) # (1)
xref = img[0]
image = doc.extract_image(xref)
print(image) # (2) Result for first print: (5, 0, 600, 400, 8, 'DeviceRGB', '', 'fzImg0', '') Result for second print: {
"ext": "png",
"smask": 0,
"width": 600,
"height": 400,
"colorspace": 3,
"bpc": 8,
"xres": 96,
"yres": 96,
"cs-name": "DeviceRGB",
"image": <remove for display purposes>
} The following are the sample files I've used: Any help will be greatly appreciated. Thank you so much 🙇♂️ |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
You can determine the bbox and transformation for each image on the page: |
Beta Was this translation helpful? Give feedback.
-
I should have written “clockwise“. Please consult the documentation on Matrix. Look at this snippet:
>> import fitz
>> fitz.Matrix(90) # rotation matrix
Matrix(0.0, 1.0, -1.0, 0.0, 0.0, 0.0)
>> fitz.Matrix(0.4, 0.4) # scale matrix
Matrix(0.4, 0.0, 0.0, 0.4, 0.0, 0.0)
>> fitz.Matrix(90) * fitz.Matrix(0.4, 0.4) # the product
Matrix(0.0, 0.4000000059604645, -0.4000000059604645, 0.0, 0.0, 0.0)
>>
Jorj
From: Arjun ***@***.***>
Sent: Mittwoch, 29. September 2021 04:05
To: ***@***.***>
Cc: Jorj X. ***@***.***>; ***@***.***>
Subject: Re: [pymupdf/PyMuPDF] Get rotation property of the extracted image (Discussion #1297)
@JorjMcKie<https://github.com/JorjMcKie>
I'm sorry, I don't understand how it got to a conclusion that the image is rotated 90 degrees just by the side being scaled by 0.4 factor.
>> shrink * transform
Matrix(0.0, -0.39920157194137573, 0.3992016017436981, 0.0, 100.0, 287.6247253417969)
>> #------------------------------------------------
>> # the above shows:
>> # image sides scaled by same factor 0.4
>> # image rotated by 90 degrees anti-clockwise
>> #----------------------------------------------
Is there some sort of formula to get this? 😮
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1297 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AB7IDIUWOTKZ3XO2MMV453LUELCEHANCNFSM5E7DISBA>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
-
You can use this script to do some basic matrix analysis. Can only investigate rotations of 90 degree multiples yet.
Jorj
________________________________
From: Arjun Manipes ***@***.***>
Sent: Wednesday, September 29, 2021 4:05:23 AM
To: pymupdf/PyMuPDF ***@***.***>
Cc: Jorj X. McKie ***@***.***>; Mention ***@***.***>
Subject: Re: [pymupdf/PyMuPDF] Get rotation property of the extracted image (Discussion #1297)
@JorjMcKie<https://github.com/JorjMcKie>
I'm sorry, I don't understand how it got to a conclusion that the image is rotated 90 degrees just by the side being scaled by 0.4 factor.
>> shrink * transform
Matrix(0.0, -0.39920157194137573, 0.3992016017436981, 0.0, 100.0, 287.6247253417969)
>> #------------------------------------------------
>> # the above shows:
>> # image sides scaled by same factor 0.4
>> # image rotated by 90 degrees anti-clockwise
>> #----------------------------------------------
Is there some sort of formula to get this? 😮
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<#1297 (reply in thread)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AB7IDIUWOTKZ3XO2MMV453LUELCEHANCNFSM5E7DISBA>.
Triage notifications on the go with GitHub Mobile for iOS<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
|
Beta Was this translation helpful? Give feedback.
-
BTW a scale matrix, fitz.Matrix(x, x), is equal to fitz.Identity * x
Therefore the previous could have been also expressed by fitz.Matrix(90) * 0.4.
|
Beta Was this translation helpful? Give feedback.
-
Just an update, I've found your old discussion relating to my problem just in case someone needs this. |
Beta Was this translation helpful? Give feedback.
You can determine the bbox and transformation for each image on the page:
page.get_image_rects(5, transform=True)
. How to interpret the returned matrix is explained here.