I want to extract images from PDFs retaining a knowledge of their content (page_number and coordinates on page). (Some tools (e.g. pdfminer) only emit image files with non-semantic names, e.g. Img0.bmp). I can do this with PDFBox (Java) but I'd ideally like a Python tool
My current (arbitrary) designs is to create filenames of the form:
image_<page>_<serial_in_page>_<x1>_<x2>__<y1>_<y2>.png
Currently pdfplumber exposes cooordinates but with a PDFStream and encoding information rather than an image. Code to convert the stream to a *.png would solve the problem.
(NOTE: the pdfplumber approach of rendering to the screen and capturing the known rectangle (which I use) is not a solution as the image is often degraded and frequently overwritten with text.)
(NOTE: I have had problems with several Python tools (pdfminer.six, PuMuPDF) extracting images as they make the background black which obscures black text, etc. PDFBox (Java) doesn't have this problem.)
Python tools are likely to have similar problems to any tools even those that require a single line to manipulate images or extract their details.
Here we can see a visual layout of all the compressed images in the file by using one command line to extract images. Here the individual object references have been converted into normal tiff or jpg (other tools may use pbm and pgm especially for OCR but the result is generally similar). The Greyscale Alpha softmask (B&W) transparency components are not necessarily tied direct to a page or an image other than by internal references, and usually appear like negatives.
What you may note is that the objects that were inserted most likely as one PNG are broken in two when injected into the PDF and their scaled placement is defined. Note that a raw PNG (whatever its source common resolution was) will retain number of dots but its scale when inserted into the PDF could be totally different horizontal and vertical, thus the only meaningful data is W x H in pixel values.
It is not trivial to overlay the mask on the RGB component when simply extracted but can allow for colour changes if desired.
So PDFbox is one of the simpler/better tools for blending to a suitable output, (as you have discovered) but for Python it is generally the top end library products that can identify the placement of the two images and combine into a suitable alpha output like a new PNG.
For many suggestions see Extract images from PDF without resampling, in python?.
Your related part question was knowing where those components are placed on each page since one image (and its alpha mask) could be placed multiple times such as a heading logo on each page. Again it is easy in a single command line to see which pages are referenced by a group of images, but to see which image is placed where requires analyzing each pages resources, again requiring a library interrogation of page contents, thus best done via power house libraries such as iText or any other like PDFtron for python.
For a related command in PyMuPDF see https://pymupdf.readthedocs.io/en/latest/page.html#Page.get_image_rects
I don't have a solution in Python but here is a small script using Ruby and HexaPDF:
require 'hexapdf'
class ImageBorderProcessor < HexaPDF::Content::Processor
def initialize(page, index)
super()
#page = page
#index = index
#count = 0
end
def paint_xobject(name)
super
xobject = resources.xobject(name)
return unless xobject[:Subtype] == :Image
w, h = xobject.width, xobject.height
llx, lly = graphics_state.ctm.evaluate(0, 0)
lrx, lry = graphics_state.ctm.evaluate(1, 0)
urx, ury = graphics_state.ctm.evaluate(1, 1)
ulx, uly = graphics_state.ctm.evaluate(0, 1)
# If the image is rotated, you will need all 4 coordinates, nut just the 2
filename = "image_#{#index}_#{#count}_#{llx}_#{urx}_#{lly}_#{ury}"
xobject.write(filename) rescue puts "Can write image #{#index}-#{#count}"
#count += 1
end
end
doc = HexaPDF::Document.open(ARGV[0])
doc.pages.each_with_index do |page, index|
processor = ImageBorderProcessor.new(page, index)
page.process_contents(processor)
end
It will iterate over all pages of the input document provided on the command line and create files using your file naming scheme. Since HexaPDF doesn't currently support writing all types of PDF images, you might get some error messages for those that can't be written.
If a supported image has an associated image mask defined, it will automatically be used to create a transparent image.
The script will output all images found, even repeated ones. This could easily be changed so that just a soft link is created for repeated images.
I am working in R Markdown to create a PDF Output and trying to fit my outputs to a single page. I'm a bit over at the moment, and want to remove the extra white space around an image.
knitr::include_graphics(("File_Name.jpeg"))
I have tried par(mar(c(0,0,0,0)) which seemed promising based on the description here but that seems to scale the image.
I also tried using out.width but that didn't seem to impact the image at all.
Two steps resolved this for me.
First, I added the code below to the ggplot code which was creating the original graph image in a separate r script file, that I was saving and later inserting into the rmd file.
plot +
theme(plot.margin = margin(0, 0, 0, 0, "cm"))
This helped to reduce the margins but there was still an extra space at the bottom which appeared in PDF Output files between blocks.
So, Second, following the guidance in https://stackoverflow.com/questions/59640925/rmarkdown-how-to-reduce-space-between-title-block-and-start-of-body-text
I added the code below after inserting the image (right before the first set of text) to reduce the extra spacing Markdown adds between blocks.
\vspace{-5truemm}
Apologies if this is a dupe, I've been searching for over an hour but the search terms are all really broad and I just keep getting the same results. Also I'm fairly new to matlab so apologies for any misunderstandings.
Anywho, I have a matlab program which needs to frequently save an image generated from a matrix, but I just can't figure out how to do that without displaying it first. Basically I'm caught in between two functions, image and imwrite, both only do half of what I want.
image is able to take my matrix and create the desired output, but it just displays it to a figure window
imwrite is able to save an image to a file without displaying it, but the image is completely wrong and I can't find any parameters that would fix it.
Other questions I've seen deal with using imread and managing figures and stuff, but I'm just doing (for example)
matrix = rand(20);
colormap(winter);
image(matrix, 'CDataMapping', 'scaled');
or
matrix = rand(20);
imwrite(matrix, winter(256), 'filename.png');
Is there some way to call the image function such that it doesn't display a figure window and then gets saved to a file? Something analogous to calling imshow and then savefig in matplotlib.
Just do this:
matrix = rand(20);
f = figure('visible', 'off');
colormap(winter);
image(matrix, 'CDataMapping', 'scaled');
print(f, '-dpng', 'filename.png');
I would like send ZPL instructions to a Zebra printer (GK420t for now).
I'm printing 50mm x 20mm labels.
I would like a logo (small ~ 5mm x 5mm image) to be printed on the upper left corner of the label.
I would like to know the steps I should follow to do this.
I have been reading and trying a few things from the ZPL manual but I don't really understand how it works and couldn't find a working example.
It looks like I have to "load" the image into the printer first (in a so-called "storage area"/DRAM?) and then print it.
The .GRF file extension is mentioned many times in the manual.
I couldn't find the tool to convert a .PNG or .BMP image into a .GRF file.
I read that a .GRF file is an ASCII HEX representation of a graphic image... but it didn't help me do the work.
I could print the logo on the labels using the "Zebra Setup Utilities", by "Downloading Fonts and Graphics", choosing any available .MMF file, adding a .BMP picture, downloading it [to the printer] and printing a test page.
But until now, I couldn't do it using ZPL instructions.
I am also wondering what are the best dimensions I should use given the fact that I need a small image ~5mm x 5mm to be printed on the labels.
The image I printed is a 40px x 40px image.
Also, if I have to make a .GRF file from an original image what should be the type of this file (.BMP, .PNG, .JPG)?
Can you advise me how to proceed?
It sounds like you have some existing ZPL code, and all you want to do is add an image to it.
If that's the case, the easiest solution is probably to go to the Labelary online ZPL viewer, paste your ZPL into the viewer, click "Add image", and upload the image that you want to add to the ZPL.
This should modify your ZPL by adding the image ZPL commands that you need, and you can then tweak the position, etc.
Here is another option: I created my own image to .GRF converter in python. Feel free to use it.
from PIL import Image, ImageOps
import re
import itertools
import numpy as np
# Use: https://www.geeksforgeeks.org/round-to-next-greater-multiple-of-8/
def RoundUp(x, multiple_of = 8):
return ((x + 7) & (-1 * multiple_of))
def image2grf(filePath, width = None, height = None, rotate = None):
image = Image.open(filePath).convert(mode = "1")
#Resize image to desired size
if (width != None):
size = (width, height or width)
if (isinstance(size[0], float)):
size = (int(size[0] * image.width), int(size[1] * image.height))
#Size must be a multiple of 8
size = (RoundUp(size[0]), RoundUp(size[1]))
# image.thumbnail(size, Image.ANTIALIAS)
image = image.resize(size)
if (rotate != None):
image = image.rotate(rotate, expand = True)
image_asArray = np.asarray(np.asarray(image, dtype = 'int'), dtype = 'str').tolist()
bytesPerRow = len(image_asArray[0])
nibblesPerRow = bytesPerRow // 4
totalBytes = nibblesPerRow * len(image_asArray)
#Convert image to hex string
hexString = "".join(
format(int("".join(row[i*4:i*4 + 4]), 2) ^ 0xF, "x")
for row in image_asArray
for i in range(nibblesPerRow)
)
#Compose data
data = "~DGimage," + str(totalBytes // 2) + "," + str(nibblesPerRow // 2) + "," + hexString
#Save image
fileHandle = open(r"labelPicture.grf", "w")
fileHandle.write(data)
fileHandle.close()
if __name__ == '__main__':
# image2grf(r"warning.bmp")
image2grf(r"pallet_label_icons.png", rotate = 90)
Edit: I updated the code above to use my new conversion method, which produces better resolution GRF files
Just install ZebraDesigner, create a blank label, insert a image object to the template and add the required logo image.
Print to File this label (a *.prn file) and open the recently created file with Notepad++ (MS Notepad will ruin the data if opened and saved with). Find a huge string of seemingly random characters, and there is your image's data. Careful not to lose any of those characters, including the control ones, as the whole string is a textual representation of your image (as it would be if it were base64).
Tip0: Always have your ZPLII Programmer's Guide at hand, you'll need/want to check if ZebraDesigner sent the image to memory or directly to the printer buffer.
Tip1: Before adding the logo to the label and get the text, prepare the image making it greyscale (remember to check the printer's dithering configuration!) or, in my case, plain black and white (best result IMHO). The image can be colored, the ZebraDesigner will make it work for the printer converting the image to greyscale before conversion to commands and text.
I created a PHP script to convert PNG images to .GRF similar to Josh Mayberry's image2grf.py:
https://gist.github.com/thomascube/9651d6fa916124a9c52cb0d4262f2c3f
It uses PHP's GD image function and therefore can work with all the file formats GD can open. With small modifications, the Imagick extension could be used but performance seems to be better with GD.
Try codeproject's sharpzebra project. The test program that is part of project prints a graphic and I know this works at least it did on a ZM400
go to Object ==> Picture and your curser will change to something else.. when it changed go and click on the working area and a dialog box iwll apear... so on there select the image so you can see the image whant you wanna print on the printer i am using GT800 so for me i did like that hope this will helps you
Use ZebraNet Bridge Enterprise Software to convert BMP to GRF file format
I had to figure this out again today. In the ZPL code, you can output the graphic bytes for every single label (which means a lot of additional data when you're printing a few thousand labels), or you can define the image first and then refer to it.
I used an online ZPL viewer to save on the number of labels printed when testing. I used:
http://staging.advanced-technology-group.com/
and here is another that does the same:
http://labelary.com/viewer.html
These (currently) have an 'add image' function. This transfers a png to the GRF format that ZPL works with (see the other answers if you need to generate these bytes yourself).
Outputting the bytes for every label
Using the "Add image" function generates a command and the graphic bytes, which looks like:
^FO50,50^GFA,11118,11118,17,,<lots of data>
You can adjust the FO as that tells the printer where to position the graphic.
That should be fine for shorter runs / smaller pictures / you're in a hurry.
Downloading the image once and then referring
This is what I had to do, so I needed to rearrange the bytes a bit (nice pun?).
THe ^GF command stands for Graphic Field: ^GFa,b,c,d,data where
a: A|B (A, non-binary, B = binary)
b: number of bytes transmitted
c: number of bytes comprising the graphic format
d: number of bytes per row
and what I needed to do is to reformat this as ~DGR:000.GRF,11118,17,, so that I could refer to it with ^XGR:000.GRF,1,1. After the print run, I'd need to delete the graphic from memory again with: ^ID000.GRF
The properties for ~DGd:o.x,t,w,data mean
d: memory destination - R for RAM
o: image name (1-8 alphanumeric chars)
x: filename extension, always GRF
t: number of bytes in the graphic
w: number of bytes per row
So I turned:
^FO50,50^GFA,11118,11118,17,,<data>
into:
~DGR:000.GRF,11118,17,,<data>
This definition goes before the label-definition, so:
~DGR:000.GRF,11118,17,,<data>
^XA (start of label)
...
^FT360,700^XGR:000.GRF,1,1^FS <-- this outputs the graphic
...
^XZ (end of label)
^ID000.GRF
I am creating a GUI containing an image using the following code:
try
Imagenamehere = imread('Imagenamehere.jpg');
axes(handles.Logo)
image(Imagenamehere)
set(gca,'xtick',[],'ytick',[])
catch
msgbox('Please download all contents from the zipped file into working directory.')
end
The image shows up but for some reason is completely coloured blue as if put through a blue filter. I don't think it would be wise to upload the image but it is a simple logo coloured black and white.
Anyone know what could be causing this?
Check the size, type (probably uint8) and range of your image. It sounds like for some reason your images are being displayed with colormap as jet (the default), and possibly also that your range is not what MATLAB expects (e.g. 0 to 1 not 0 to 255), resulting in all your values being relatively low (blue on the jet colormap).
"black and white" is just one way of interpreting an image file which contains only two colors. MATLAB makes several assumptions when you pass data into a display function like image. If you don't specify colormap and image data range, it will make a guess based off things like data type.
One possibility is that your logo file is an indexed image. In these cases you need to do:
[Imagenamehere map] = imread('Imagenamehere.jpg');
colormap(map);