Undoing image transformations like flipping #3274
-
Description of the bugI tried to extract images from the pdf document that contains image and tried to run OCR on the extracted image in order to get the image data. I tried using How to reproduce the bugI tried debugging by printing the detected image with the following code and found that the detected images are flipped unexpectedly. Need help to find out if there's something wrong in my implementation or there's a bug in the image detection. import fitz
from PIL import Image
import io
doc = fitz.open("sample.pdf")
for page_index , page in enumerate(doc):
image_blocks = page.get_image_info(xrefs=True)
count = 0
for image in image_blocks:
if not image["xref"]:
continue
base_image_info = doc.extract_image(image["xref"])
image_bytes = base_image_info["image"]
image = Image.open(io.BytesIO(image_bytes))
image.save(f"./images/image__{page_index + 1}__{count}.png")
count = count + 1 Sample pdf file is PyMuPDF version1.23.26 Operating systemLinux Python version3.11 |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 5 replies
-
The PDF creator has rotated the images flipped top-bottom. import fitz
doc=fitz.open("sample.pdf")
page=doc[0]
from pprint import pprint
pprint(page.get_images())
[(7, 0, 1318, 1018, 8, 'DeviceGray', '', 'R183', 'FlateDecode'),
(8, 0, 1318, 1018, 8, 'DeviceGray', '', 'R182', 'FlateDecode'),
(9, 0, 1318, 1018, 8, 'DeviceGray', '', 'R181', 'FlateDecode'),
(10, 0, 1318, 1018, 8, 'DeviceGray', '', 'R180', 'FlateDecode'),
(11, 0, 1318, 1018, 8, 'DeviceGray', '', 'R179', 'FlateDecode'),
(12, 0, 1318, 1018, 8, 'DeviceGray', '', 'R178', 'FlateDecode'),
(13, 0, 1318, 1018, 8, 'DeviceGray', '', 'R177', 'FlateDecode'),
(14, 0, 1318, 1018, 8, 'DeviceGray', '', 'R176', 'FlateDecode'),
(15, 0, 1318, 1018, 8, 'DeviceGray', '', 'R175', 'FlateDecode'),
(24, 0, 155, 73, 8, 'DeviceGray', '', 'R8', 'DCTDecode')]
img7=doc.extract_image(7)
img7.keys()
dict_keys(['ext', 'smask', 'width', 'height', 'colorspace', 'bpc', 'xres', 'yres', 'cs-name', 'image'])
page.get_image_rects(7,transform=True)
[(Rect(1431.9599609375, 334.1400146484375, 2043.8399658203125, 1126.1400146484375), Matrix(0.0, -792.0, -611.8800048828125, -0.0, 2043.8399658203125, 1126.1400146484375))] As you can see, the transformation matrix has negative values for |
Beta Was this translation helpful? Give feedback.
-
The transformation matrix is also part of |
Beta Was this translation helpful? Give feedback.
-
Can't we prevent top-bottom flip and extract the image as how it is originally. @JorjMcKie |
Beta Was this translation helpful? Give feedback.
-
The image's original is flipped! The PDF creator has "un-flipped" it to properly display on the page. |
Beta Was this translation helpful? Give feedback.
-
Is't there any way to undo the "un-flipped" action by pdf-creator. This would help me restore the original content of the document |
Beta Was this translation helpful? Give feedback.
-
What you actually mean is to do the same thing as the creator: un-flip the image, right? |
Beta Was this translation helpful? Give feedback.
-
I've tried this as well but the problem I ran into was there is combined case for page flip as well as image flip. This shows the case page is not flipped and image is flipped But for the transformation matrix this shows that the page and image both are flipped and the above un flipping technique doesn't work |
Beta Was this translation helpful? Give feedback.
-
let's convert this to a discussion first ... |
Beta Was this translation helpful? Give feedback.
-
Here is a little helper for interpreting an image transformation matrix: The function returns appropriate Pillow image transformation actions. When multiple actions are required, use multiple Pillow actions. |
Beta Was this translation helpful? Give feedback.
You obviously have to take page rotation into account, because the transformation matrix (please read the documentation!) takes everything into account required to place the image on page: image's native transformation and page rotation.
You have several options, but the simplest thing is probably to remove page rotation before you extract the image. Us this function for de-rotating.