'Why does Fitz only return a very close RGB to the true RGB for a specific PDF?
I'm trying to create a script that identifies colours in a PDF that do not adhere to corporate branding requirements.
I decided to use Fitz to extract a pixel map from each page and from each pixel I subtract what I can identify as the closest approved RGB colour and print out the image.
The process works but two things are happening. The first is that clean lines now have a blur/meld effect and the pixel colour I get does not match exactly what is in the PDF.
I printed out the original image before I adjust the pixels and the using Snipping tool on the PDF and the generated image I can see that RGB values don't match exactly.
What can I do to reduce and or mitigate these issues?
Edit: After doing some research, could this be caused by the colour being encoded using CMYK or Hex and this issue is caused by Fitz converting from CMYK to RGB or due to another colour style conversion?
def GetDoc(path):
return fitz.open(path)
def GetPixMap(doc):
docPixMap = list()
zoom = 3
for page in doc:
docPixMap.append(page.get_pixmap())
#mat = fitz.Matrix(zoom, zoom)
#docPixMap.append(page.get_pixmap(matrix=mat, alpha=True))
return docPixMap
def SubtractClosestColorFromEachPixel(docPixMap):
totPages = str(len(docPixMap))
for i, pixMap in enumerate(docPixMap):
pixMap.pil_save("Org " + str(i)+".png")
print("Page " + str(i+1) + "/" + totPages)
for h in range(pixMap.height):
for w in range(pixMap.width):
pixelColour = pixMap.pixel(w,h)
closestColour = GetClosestColour(pixelColour)
negativeColour = SubtractColour(pixelColour, closestColour[2])
pixMap.set_pixel(w,h,negativeColour)
#print(pixelColour,negativeColour,pixMap.pixel(w,h))
pixMap.pil_save("test " + str(i)+".png")
def SubtractColour(c1,c2):
return [Adj(c1[0], c2[0]),Adj(c1[1], c2[1]),Adj(c1[2], c2[2])]
def Adj(i1,i2):
return 255 - abs(i1-i2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
