scale pdf pages in Python/ pypdf2 - pdf

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")

Related

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()

display pdf report in tkinter frame

I want to display pdf file in tkinter frame in Kiosk mode so that I can add print and other buttons too. I am using below code
from tkPDFViewer import tkPDFViewer as pdf
from tkinter import Tk, Button
class ShowPdf(pdf.ShowPdf):
def goto(self, page):
try:
self.text.see(self.img_object_li[page - 1])
except IndexError:
if self.img_object_li:
self.text.see(self.img_object_li[-1])
root = Tk()
Above code executed successfully but giving warning as below
Deprecation: 'getPixmap' removed from class 'Page' after v1.19 - use 'get_pixmap'.
Deprecation: 'getImageData' removed from class 'Pixmap' after v1.19 - use 'tobytes
Also unable to display PDF file in customize size.
Expert pls. guide me so that I can display pdf file in frame.
Thanks in Advance.

Color issue when saving PDF page Pixmap as PNG using PyMuPDF

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.

error: (-215:Assertion failed) !_src.empty() in function 'cvtColor' while using OpenCV 4.2 with swift [duplicate]

I am trying to do a basic colour conversion in python however I can't seem to get past the below error. I have re-installed python, opencv and tried on both python 3.4.3 (latest) and python 2.7 (which is on my Mac).
I installed opencv using python's package manager opencv-python.
Here is the code that fails:
frame = cv2.imread('frames/frame%d.tiff' % count)
frame_HSV= cv2.cvtColor(frame,cv2.COLOR_RGB2HSV)
This is the error message:
cv2.error: OpenCV(3.4.3) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
This error happened because the image didn't load properly. So you have a problem with the previous line cv2.imread. My suggestion is :
check if the image exists in the path you give
check if the count variable has a valid number
If anyone is experiencing this same problem when reading a frame from a webcam:
Verify if your webcam is being used on another task and close it. This wil solve the problem.
I spent some time with this error when I realized my camera was online in a google hangouts group. Also, Make sure your webcam drivers are up to date
I kept getting this error too:
Traceback (most recent call last):
File "face_detector.py", line 6, in <module>
gray_img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor
My cv2.cvtColor(...) was working fine with \photo.jpg but not with \news.jpg. For me, I finally realized that when working on Windows with python, those escape characters will get you every time!! So my "bad" photo was being escaped because of the file name beginning with "n". Python took the \n as an escape character and OpenCV couldn't find the file!
Solution:
Preface file names in Windows python with r"...\...\" as in
cv2.imread(r".\images\news.jpg")
If the path is correct and the name of the image is OK, but you are still getting the error
use:
from skimage import io
img = io.imread(file_path)
instead of:
cv2.imread(file_path)
The function imread loads an image from the specified file and returns
it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).
check if the image exists in the path and verify the image extension (.jpg or .png)
Check whether its the jpg, png, bmp file that you are providing and write the extension accordingly.
Another thing which might be causing this is a 'weird' symbol in your file and directory names. All umlaut (äöå) and other (éóâ etc) characters should be removed from the file and folder names. I've had this same issue sometimes because of these characters.
Most probably there is an error in loading the image, try checking directory again.
Print the image to confirm if it actually loaded or not
In my case, the image was incorrectly named. Check if the image exists and try
import numpy as np
import cv2
img = cv2.imread('image.png', 0)
cv2.imshow('image', img)
I've been in same situation as well, and My case was because of the Korean letter in the path...
After I remove Korean letters from the folder name, it works.
OR put
[#-*- coding:utf-8 -*-]
(except [ ] at the edge)
or something like that in the first line to make python understand Korean or your language or etc.
then it will work even if there is some Koreans in the path in my case.
So the things is, it seems like there is something about path or the letter.
People who answered are saying similar things. Hope you guys solve it!
I had the same problem and it turned out that my image names included special characters (e.g. château.jpg), which could not bet handled by cv2.imread. My solution was to make a temporary copy of the file, renaming it e.g. temp.jpg, which could be loaded by cv2.imread without any problems.
Note: I did not check the performance of shutil.copy2 vice versa other options. So probably there is a better/faster solution to make a temporary copy.
import shutil, sys, os, dlib, glob, cv2
for f in glob.glob(os.path.join(myfolder_path, "*.jpg")):
shutil.copy2(f, myfolder_path + 'temp.jpg')
img = cv2.imread(myfolder_path + 'temp.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
os.remove(myfolder_path + 'temp.jpg')
If there are only few files with special characters, renaming can also be done as an exeption, e.g.
for f in glob.glob(os.path.join(myfolder_path, "*.jpg")):
try:
img = cv2.imread(f)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
except:
shutil.copy2(f, myfolder_path + 'temp.jpg')
img = cv2.imread(myfolder_path + 'temp.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
os.remove(myfolder_path + 'temp.jpg')
In my case it was a permission issue. I had to:
chmod a+wrx the image,
then it worked.
must please see guys that the error is in the cv2.imread() .Give the right path of the image. and firstly, see if your system loads the image or not. this can be checked first by simple load of image using cv2.imread().
after that ,see this code for the face detection
import numpy as np
import cv2
cascPath = "/Users/mayurgupta/opt/anaconda3/lib/python3.7/site- packages/cv2/data/haarcascade_frontalface_default.xml"
eyePath = "/Users/mayurgupta/opt/anaconda3/lib/python3.7/site-packages/cv2/data/haarcascade_eye.xml"
smilePath = "/Users/mayurgupta/opt/anaconda3/lib/python3.7/site-packages/cv2/data/haarcascade_smile.xml"
face_cascade = cv2.CascadeClassifier(cascPath)
eye_cascade = cv2.CascadeClassifier(eyePath)
smile_cascade = cv2.CascadeClassifier(smilePath)
img = cv2.imread('WhatsApp Image 2020-04-04 at 8.43.18 PM.jpeg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here, cascPath ,eyePath ,smilePath should have the right actual path that's picked up from lib/python3.7/site-packages/cv2/data here this path should be to picked up the haarcascade files
Your code can't find the figure or the name of your figure named the by error message.
Solution:
import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('哈哈.jpg')#solution:img=cv2.imread('haha.jpg')
print(img)
If anyone is experiencing this same problem when reading a frame from a webcam [with code similar to "frame = cv2.VideoCapture(0)"] and work in Jupyter Notebook, you may try:
ensure previously tried code is not running already and restart Jupyter Notebook kernel
SEPARATE code "frame = cv2.VideoCapture(0)" in separate cell on place where it is [previous code put in cell above, code under put to cell down]
then run all the code above cell where is "frame = cv2.VideoCapture(0)"
then try run next cell with its only code "frame = cv2.VideoCapture(0)" - AND - till you will continue in executing other cells - ENSURE - that ASTERIX on the left side of this particular cell DISAPEAR and command order number appear instead - only then continue
now you can try execute the rest of your code as your camera input should not be empty anymore :-)
After end, ensure you close all your program and restart kernel to prepare it for another run
As #shaked litbak , this error arised with my initial use with the ASCII-generator , as i naively thought i just had to add to the ./data directory , with its load automatically .
I had to append the --input option with the desired file path .
I checked my image file path and it was correct. I made sure there was no corrupt images.The problem was with my mac. It sometimes have a hidden file called .DS_Store which was saved together with the image file path. Therefore cv2 was having a problem with that file.So I solved the problem by deleting .DS_Store
I also encountered this type of error:
error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'
The solution was to load the image properly. Since the file mentioned was wrong, images were not loaded and hence it threw this error. You can check the path of the image or if uploading an image through colab or drive, make sure that the image is present in the drive.
I encounter the problem when I try to load the image from non-ASCII path.
If I simply use imread to load the image, I am only able to get None.
Here is my solution:
import cv2
import numpy as np
path = r'D:\map\上海地图\abc.png'
image = cv2.imdecode(np.fromfile(path, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
Similar thing will happen when I save the image in a non-ASCII path. It will not be successfully saved without any warnings. And here is what I did.
import cv2
import numpy as np
path = r'D:\map\上海地图\abc.png'
cv2.imencode('.png', image)[1].tofile(path)
path = os.path.join(raw_folder, folder, file)
print('[DEBUG] path:', path)
img = cv2.imread(path) #read path Image
if img is None: # check if the image exists in the path you give
print('Wrong path:', path)
else: # It completes the steps
img = cv2.resize(img, dsize=(128,128))
pixels.append(img)
The solution os to ad './' before the name of image before reading it...
Just Try Degrading the OpenCV
in python Shell (in cmd)
>>> import cv2
>>> cv2.__version__
after Checking in cmd
pip uninstall opencv-python
after uninstalling the version of opencv install
pip install opencv-python==3.4.8.29

LICENSE.txt when loading data into tensorflow transfer learning

I am using code provided by tensorflow to load data: https://www.tensorflow.org/beta/tutorials/load_data/text
When I put in my own photos, it sends to a different directory. The code wants attributions from my LICENSE.txt, but I am not sure what the purpose of this code segment is.
I made my own LICENSE.txt file by just making a text file with each line being a title of an image. When I do this, it makes attributions a dictionary in which each key is the filename and each corresponding value is ''. When I run another method, I get a key error for every file.
import os
attributions = (data_root/"LICENSE.txt").open(encoding='utf- 8').readlines()
attributions = [line.split('\n') for line in attributions]
print(attributions)
attributions = dict(attributions)
import IPython.display as display
def caption_image(image_path):
image_rel = pathlib.Path(image_path).relative_to(data_root)
return "Image (CC BY 2.0) " + ' -'.join(attributions[str(image_rel)].split(' - ')[:-1])
for n in range(3):
image_path = random.choice(all_image_paths)
display.display(display.Image(image_path))
print(caption_image(image_path))
print()
I do not really know what to expect when I run the for loop in jupyter notebook, but it gives me a key error, the key being the file name.
I wrote that tutorial. The license lookup is only there so we can directly arttribute the individual photographers when we publish it. If you're working with your own images you don't need that part of the code at all.
All it's really doing is choosing a random image and displaying it. You can simplify it to:
import os
import IPython.display as display
for n in range(3):
image_path = random.choice(all_image_paths)
display.display(display.Image(image_path))