unable to display image using mpdf......instead of image it displays a cross sign - mpdf

$pdfFilePath = "Agreement.pdf";
$this->load->library('M_pdf');
$this->m_pdf->pdf->WriteHTML($html);
$this->m_pdf->pdf->Output($pdfFilePath, "I")

Related

Building a bounding box surrounding text in Google Vision API to extract the text

I am currently trying out Google Vision API and extracting text from an image of a form. Google Vision API extract everything on the form despite me setting up ROI on specific text location which I want. Is there a way to extract out the text that I want at specific location instead of the whole image?
There is no way to extract text only from a specific location of an image using the Google Vision API, it always extracts the text from the whole image. However, if you want to extract the text from a specific location, you could try cropping the image before passing it to the API. Another option would be filtering out the results from the API call using the position of the four bounding vertices associated with each piece of text.
You can find more info on what is possible to do with the Google Vision API here.
def get_text_within(document, x1, y1, x2, y2):
text = ""
for page in document.pages:
for block in page.blocks:
for paragraph in block.paragraphs:
for word in paragraph.words:
for symbol in word.symbols:
min_x = min(symbol.bounding_box.vertices[0].x, symbol.bounding_box.vertices[1].x,
symbol.bounding_box.vertices[2].x, symbol.bounding_box.vertices[3].x)
max_x = max(symbol.bounding_box.vertices[0].x, symbol.bounding_box.vertices[1].x,
symbol.bounding_box.vertices[2].x, symbol.bounding_box.vertices[3].x)
min_y = min(symbol.bounding_box.vertices[0].y, symbol.bounding_box.vertices[1].y,
symbol.bounding_box.vertices[2].y, symbol.bounding_box.vertices[3].y)
max_y = max(symbol.bounding_box.vertices[0].y, symbol.bounding_box.vertices[1].y,
symbol.bounding_box.vertices[2].y, symbol.bounding_box.vertices[3].y)
if (min_x >= x1 and max_x <= x2 and min_y >= y1 and max_y <= y2):
text += symbol.text
if (symbol.property.detected_break.type == 1 or
symbol.property.detected_break.type == 3):
text += ' '
if (symbol.property.detected_break.type == 2):
text += '\t'
if (symbol.property.detected_break.type == 5):
text += '\n'
return text

Fillable textboxes in PDF are not unique - all are duplicates of each other in the pdf, how to fix?

I am creating a report to send out to people and have them respond in the pdf with input in Adobe to save and send back to me.
I have managed to make text fields with hyperreff; however, the output pdf's textboxes duplicate eachother - so if I put "def" in one textbox - all the other textboxes in the document also put "def".
I am using:
\begin{Form} \TextField[width = 10cm, height = 3cm, multiline=true]{ } \end{Form}
which is used later in the document for user input:
\begin{Form} \TextField[width = 10cm, height = 3cm, multiline=true]{ } \end{Form}
You need to add a unique name to each field otherwise they all get the same name and then, in Acrobat, the same value when you edit one of them. See the name property below.
\begin{Form} \TextField[name = foo, width = 10cm, height = 3cm, multiline=true]{ } \end{Form}

Overlay multiple images in python

My problem is that I have bunch of jpgs and I would like to overlay all of them to see a pattern.
I checked out this answer(Overlay two same sized images in Python) but it only shows how two images can be overlayed.
Here are the piece of code which shows I'd like to do.
for file in os.listdir(SAVE_DIR):
img1 = cv2.imread(file)
img2 = cv2.imread('next file name') #provide previous output file here (dst)
dst = cv2.addWeighted(img1,0.5,img2,0.5,0)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
One approach is to store all your images as a list, and then iterate through each overlapping pair of images and callcv2.addWeighted() on each element in your list, passing in the last aggregate image in as img1 to your subsequent call to cv2.addWeighted().
So for example, say you have 4 images, with names [img1, img2, img3, img4].
You could do
jpeg_list = os.listdir(SAVE_DIR)
for i in range(len(jpeg_list)):
aggregate_file = cv2.imread(jpeg_list[i])
next_img = cv2.imread(jpeg_list[i+1])
dst = cv2.addWeighted(aggregate_file, 0.5, next_img, 0.5, 0)
cv2.imshow('dst', dst)

Saving a cropped image in R

I am using jpeg package to read an image into R. This creates an object of class nativeRaster. I take a portion of this image using [ operator. The resulting object is a matrix of integers. Attempting to save this object returns the error image must be a matrix or array of raw or real numbers. What should I do to be able to save this new image?
Below is a snippet to reproduce the error
imageFile = 'address of the jpg file'
outputFile = 'new file to write into'
image = jpeg::readJPEG(imageFile, native=TRUE)
output = image[1:10,1:10]
writeJPEG(image = output, target = outputFile)
I think the function writeJPEG takes image of type nativeRaster. I am not entirely sure about this but converting class of output to nativeRaster works for me.
class(output) <- "nativeRaster"
writeJPEG(image = output, target = outputFile)
Even though I was unable to find a solution, I am working around this by using solely imagemagick to crop the image.
system(paste('identify',
imageFile), intern = TRUE) %>%
regmatches(.,regexpr("(?<=[ ])[0-9]*?x[0-9]*?(?=[ ])",.,perl=T)) %>%
strsplit('x') %>% .[[1]] %>% as.double
will return the dimensions of the image and
system(paste0('convert "',imageFile,
'" -crop ',
sizeX,
'x',
sizeY,
'+',
beginningX,
'+',
beginningY,
' "',
outputFile,'"'))
will crop the image and save it to outputFile
Here sizeX and sizeY are desired dimensions of the cropped image and beginningX and beginningY designate the top left corner of the crop site on the image.

Most efficient way of displaying jpgs with bokeh? Image_rgba surprisingly slow

I've tried using the bokeh image_rgba method but found it to be very slow, I'm just displaying a 1000*500 px image and the html takes ~5 seconds to load (nothing is web based here, I have everything running/stored locally)
Again the code itself runs fast, itùs just displaying the image thqt is slow. I've been trying exqmples from the bokeh gallery and the speed is fine.
I'm thus wondering if there is anything I could do for the html to load faster? Is image_rgba the best way to go to display an image with bokeh?
This is the code I use:
pic = PIL.Image.open('/Users/blabla/eiffelTower.jpg')
self.imgArray = np.array(pic)
N1 = imgArray.shape[0]
N2 = imgArray.shape[1]
img = np.zeros((N1,prolongatedN2), dtype=np.uint32)
view = img.view(dtype=np.uint8).reshape((N1, N2, 4))
view[:N1,:N2,0] = self.imgArray[range(N1-1,-1,-1),:N2,0]
view[:N1,:N2,1] = self.imgArray[range(N1-1,-1,-1),:N2,1]
view[:N1,:N2,2] = self.imgArray[range(N1-1,-1,-1),:N2,2]
fig = bokeh.plotting.figure(plot_width = plot_width, plot_height=plot_height)
fig.image_rgba(image=[img], x=[0], y=[0],
dw=[plot_width], dh=[plot_height])
script, div = bokeh.embed.components(p.fig, INLINE)
output_file('testBokeh.html')
show(fig)
Again I'm quite surprised that displaying a locally stored 1000*500 pixels would be so slow.
FWIW, I do this, and it's very fast.
from __future__ import division
import numpy as np
from PIL import Image
from bokeh.plotting import figure, show, output_file
# Open image, and make sure it's RGB*A*
lena_img = Image.open('Lenna_rect.png').convert('RGBA')
xdim, ydim = lena_img.size
print("Dimensions: ({xdim}, {ydim})".format(**locals()))
# Create an array representation for the image `img`, and an 8-bit "4
# layer/RGBA" version of it `view`.
img = np.empty((ydim, xdim), dtype=np.uint32)
view = img.view(dtype=np.uint8).reshape((ydim, xdim, 4))
# Copy the RGBA image into view, flipping it so it comes right-side up
# with a lower-left origin
view[:,:,:] = np.flipud(np.asarray(lena_img))
# Display the 32-bit RGBA image
dim = max(xdim, ydim)
fig = figure(title="Lena",
x_range=(0,dim), y_range=(0,dim),
# Specifying xdim/ydim isn't quire right :-(
# width=xdim, height=ydim,
)
fig.image_rgba(image=[img], x=0, y=0, dw=xdim, dh=ydim)
output_file("lena.html", title="image example")
show(fig) # open a browser

Resources