Color issue when saving PDF page Pixmap as PNG using PyMuPDF - pdf

I'm running the following bit of Python code from the PyMuPDF 1.16.17 documentation, which save PNG images for every page in a PDF file.
import sys, fitz # import the binding
fname = "test.pdf" # get filename from command line
doc = fitz.open(fname) # open document
for page in doc: # iterate through the pages
pix = page.getPixmap()
pix.writePNG("F:/cynthia/page-%i.png" % page.number) # store image as a PNG
The resulting PNG images' colors are off from the PDF originals (too saturated and high contrast). I know function Page.getPixmap() has a "colorspace" argument, and using Document.getPageImageList I found out that my PDF's colorspace is "DeviceCMYK". But when I try to get a Pixmap using CMYK as colorspace (replacing the pix = page.getPixmap() line with pix = page.getPixmap(colorspace="CMYK") or `pix = page.getPixmap(colorspace=csCMYK)), it doesn't change the resulting colors. Any help is appreciated.

Please upgrade your PyMuPDF version. Then ICC color support will be included which should improve your output.

Related

scale pdf pages in Python/ pypdf2

I have a question related to this post with the current pypdf2 and python versions. Using this code from the Pypdf2 documentation I receive an empty page 0x0mm. scaling content works as it should.
My understanding ist that the result should be scaled content as well as page size;
Other posts show that it has worked inn the past, obviously not with the current pypdf version.
Do you have an idea?
Thanks in advance for your help.
from PyPDF2 import PdfReader, PdfWriter, Transformation
# Read the input
reader = PdfReader("pdffile.pdf")
page = reader.pages[0]
# Scaling the content - works
op = Transformation().scale(sx=0.7, sy=0.7)
page.add_transformation(op)
# Scaling page - returns empty page
page.scale_by(0.7)
# Write the result to a file
writer = PdfWriter()
writer.add_page(page)
writer.write("out-pg-transform.pdf")

Creating a complex PDF file according to precise instructions and meticulous specifications

[Sorry for my bad English]
We are developing an application that manages information about public transportation. The application should generate posters for signage at bus stops.
The posters should conform to detailed and strict regulatory rules, in every detail. Typography, colors, tables, lines, symbols, embedded images and much more.
We need to produce the poster as a PDF file, which will be sent for printing.
Our question: How to produce this file in a reliable and efficient way?
Do we should to create an HTML+CSS file, then use a library that converts HTML to PDF?
Can we trust the library to convert the HTML completely accurately?
Or we should to use libraries that generate PDF directly like iText.
Do they support creating a complex PDF according to exact specifications?
And what is the most suitable environment to do it?
Our first priority is dotnet core, but if there is no choice, we will also consider using python or node.
And a final question, to which field of knowledge does this belong? What skills are needed to perform the task? We want to publish a tender for this task, and don't know what to ask for.
disclaimer: I am the author of borb, the library used in this answer
In general, there are two kinds of PDF libraries.
high level libraries: These libraries allow you to easily add content (images, text, tables, lists, etc) without having to specify too much. It's easier for you (the user) but you're giving up precise control.
low level libraries: These libraries bring you (the user) down to the nitty gritty level of the PDF. You can manipulate content and place it at exact positions. You can define a color space (ensuring the color can be calibrated), etc. This also means you give up comfort. You can not (easily) split text, automatically flow content blocks, etc
borb allows you to do both. You can place content at exact coordinates, you can specify your own fonts, you can set colors using RGB, HSV, etc
You can also use a PageLayout which will take over most of the content-placement.
This is an example using absolute positioning:
from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import Paragraph
from borb.pdf import PDF
from borb.pdf.canvas.geometry.rectangle import Rectangle
from decimal import Decimal
def main():
# create Document
doc: Document = Document()
# create Page
page: Page = Page()
# add Page to Document
doc.add_page(page)
# define layout rectangle
# fmt: off
r: Rectangle = Rectangle(
Decimal(59), # x: 0 + page_margin
Decimal(848 - 84 - 100), # y: page_height - page_margin - height_of_textbox
Decimal(595 - 59 * 2), # width: page_width - 2 * page_margin
Decimal(100), # height
)
# fmt: on
# the next line of code uses absolute positioning
Paragraph("Hello World!").paint(page, r)
# store
with open("output.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()
And this is that same example using a PageLayout
from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import PageLayout
from borb.pdf import SingleColumnLayout
from borb.pdf import Paragraph
from borb.pdf import PDF
def main():
# create Document
doc: Document = Document()
# create Page
page: Page = Page()
# add Page to Document
doc.add_page(page)
# set a PageLayout
layout: PageLayout = SingleColumnLayout(page)
# add a Paragraph
layout.add(Paragraph("Hello World!"))
# store
with open("output.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()

Spectral Python imshow displaying scrambled image

I am learning Spectral Python and using their own documentation and sample image files to display a multispectral image as RGB. However, for some reason, my image appears scrambled up. I have tested the image file by opening it in MultiSpec and it appears as it should, so I do not think the file is damaged. My code is as follows:
import spectral as s
import matplotlib as mpl
path = '/content/92AV3C.lan'
img = s.open_image(path)
print(img)
#Load and display hyperspectral image
arr = img.load()
view = s.imshow(arr, (29, 19, 9))
print(view)
#Load and display Ground truth image
gt = s.open_image('92AV3GT.GIS').read_band(0)
view = s.imshow(classes=gt)
Output is as follows:
I suggest that you try the following command instead of view=imshow(img, (RGB))`. SpectralPython has the smarts, once you identify the image type, i.e., *.lan to display the image in the correct format.

How to crop the detected object after training using YOLO?

I am using YOLO for model training. I want to crop the detected object.
For Darknet repository am using is: https://github.com/AlexeyAB/darknet/
For Detection and storing output coordinates in a text file am using this:
!./darknet detector test data_for_colab/obj.data data_for_colab/yolov3-tiny-obj.cfg yolov3-tiny-obj_10000.weights -dont_show -ext_output < TEST.txt > result.txt
Result.jpg
Considering in the TEST.txt file you have details as the sample image.
You can use re module of python for text pattern detection, ie your "class_name".
Parsing the .txt file
import re
path='/content/darknet/result.txt'
myfile=open(path,'r')
lines=myfile.readlines()
pattern= "class_name"
for line in lines:
if re.search(pattern,line):
Cord_Raw=line
Cord=Cord_Raw.split("(")[1].split(")")[0].split(" ")
Now we will get the coordinates in a list.
Coordinate calculation
x_min=int(Cord[1])
x_max=x_min + int(Cord[5])
y_min=int(Cord[3])
y_max=y_min+ int(Cord[7])
Cropping from the actual image
import cv2
img = cv2.imread("Image.jpg")
crop_img = img[y_min:y_max, x_min:x_max]
cv2.imwrite("Object.jpg",crop_img)

how to download image with scrapy when i got a base64

I want donwload a image with this selector
image = response.xpath('/html/body/div/div/div[3]/div[1]/div[1]/img/#src').extract()
with this, on the shell i have the result
['data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7']
Maybe is because the website is protected with hotlinking (cloudflare protection) and i need use other method? or simply im selecting bad the image for download
After you get the base64 string:
# Assume you get this image list
image_list = ['data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7']
import base64
# Save image to file by using this code
for i, item in enumerate(image_list):
with open("imageToSave{}.png".format(i), "wb") as fh:
fh.write(base64.decodebytes(item.split(',')[-1]))