I'm trying to edit a pdf file with 100 pages, all of them images I need to export as png, setting their image mode as greyscale, and setting also their resolution, width and heigth.
How can I write a scheme (or python) script that perform this actions so that i could apply them by gimp in batch mode?
I've searched in the internet but didn't find simpy stated instructions.
ImageMagick's convert will do all this in one call in a command prompt:
convert -density 200 -colorspace Gray input.pdf -geometry 1000 ouput.png
will produce 1000px-wide grayscale PNGs (output-0 to output-(N-1).png) using a 200DPI rendering of the PDF.
You can also use Gimp scripting but you'll have a lot more to learn and AFAIK the API for the PDF loader only loads at 100DPI.
A slightly more manual method could be to:
Load (manually) the image in Gimp (you can specifiy the DPI in that case). This loads all the pages as layers.
Image>Mode>RGB to convert the image to grayscale.
Image>Scale image to set the size of all the pages
Save the individual layers to PNG (there are scripts for this, for instance this one)
Related
I want to overlay multiple PNG images of different sizes on a transparent canvas using ImageMagick. First I create a transparent canvas of some fixed size, say like
convert -size 1500x1000 canvas:transparent PNG32:canvas.png
Then I loop over my images in order to add each image to the canvas
convert canvas.png nthimage.png -gravity Center -geometry xResxYres+xcoord+ycoord -composite canvas.png
This works fine, but I may overlay as many as 10 pictures and I do this for thousands of n-tuples of images, so a faster solution would be appreciated. So my question: Can I also do this in one step instead of creating the canvas first and then adding a single image at a time?
Edit: I use ImageMagick 7.0.11-13 on macOS 10.15.7. I run ImageMagick from within a python script, so a file containing a list of input files can be generated if needed. For concreteness, say my input files are file_1.png up to file_n.png with sizes A1xB1 up to AnxBn and should be placed at coordinates +X1+Y1 up to +Xn+Yn with respect to the center of the canvas and the output file is output.png and should have size 1500x1000.
I really wouldn't recommend shelling out subprocesses from Python to call ImageMagick thousands of times. You'll just end up including too much process creation overhead per image, which is pointless if you are already running Python which can do the image processing "in house".
I would suggest you use PIL, or OpenCV directly from Python, and as your Mac is certainly multi-core, I would suggest you use multi-processing too since the task of doing thousands of images is trivially parallelisable.
As you haven't really given any indication of what your tuples actually look like, nor how to determine the output filename, I can only point you to methods 7 & 8 in this answer.
Your processing function for each image will want to create a new transparent image then open and paste other images with:
from PIL import Image
canvas = Image.new('RGBA', SOMETHING)
for overlay in overlays:
im = Image.open(overlay)
canvas.paste(im, (SOMEWHERE))
canvas.save(something)
Documentation here.
I wanted to batch-convert some JPG images to PNG, so I used the following ImageMagick command:
convert before.jpg after.png
However, things didn't go as smooth as I planned. See two images below - first is before.jpg, second is after.png:
You can see that the second one is quite darker than the original.
I'm using ImageMagick 6.9.9-19.
Opening JPEG image in GIMP and exporting it as PNG gives expected result - output image is identical to original.
What am I doing wrong?
Edit: re-saving after.png with GIMP also makes the image identical to original.
Edit: even after I saved after.png with GIMP, re-converting with ImageMagick breaks colors again (convert after-gimp.png after-gimp-and-im.png).
Edit: converting to sRGB or CMYK TIFF, or even again to JPG, does not give this problem. However, I still need PNG.
The two images you posted are identical according to IM 6.9.9.20 Q16 Mac OSX compare -metric rmse GBooF.jpg geDxn.png null: Which produces: 0 (0). It is possible your viewers may display jpg and png differently, since neither have an sRGB profile. Though, png is usually assumed to be sRGB.
Also my viewer shows both of your images as the same --- no difference, in your post on Mac Safari and using Mac Preview.
Try adding an sRGB profile to your jpg. Then convert.
convert GBooF.jpg -profile path2/sRGB.icc GBooF2.jpg
convert GBooF2.jpg GBooF2.png
See if those view the same.
New to Juypter, trying to use it with Latex. Everything works fine except for the images. I used this tutorial: https://www.youtube.com/watch?v=m3o1KXA1Rjk&t=149s
Png Images are fine in markdown, i.e
But LaTex cannot determine size of png images. If I save the image as pdf I get the same issue. If I save the image as eps then LaTex complains that it cannot convert eps to pdf.
Has anyone had this issue? Anyone know how to solve it?
PNG images always contain the size in pixels.
Optionally, they can include a chunk of data named pHYs. This contains the resolution of the image. If this chuck is present, you should be able to find the actual text pHYs in the file.
If this chunk is missing from the PNG file, the scale of the image can not be found.
If you are on a UNIX-like operating system you could use grep or hexdump to check for the text pHYs in the PNG file. The identify program from the ImageMagick suite also can display the resolution of PNG images.
Note that there is an error in the video. The author first uses latex and then pdflatex three times. That is not a good idea, since they have different capabilities w.r.t. graphics. Stick with pdflatex.
I'm using ImageMagick to convert PDF files to PNGs. (Lots of text, so I'd rather use PNG over JPEG.) I'm doing this on OS X, 10.8.2.
I tried using Ghostscript, then ImageMagick, as was done here and I got a gray background. I shortened it to one line, since ImageMagick can work with PDFs and tried this:
convert -background transparent -transparent white \
Background-Page01.pdf TestClearX1.png
I've tried that with PNG files and JPEG files. (And with and without -transparent white as well.)
If it's a JPEG, I can get a white background (which is probably clear, but in my viewer, I can't tell), but with a PNG, I always get a dark background. I thought about, as a test, trying to generate a BMP, then converting that to PNG, but it won't generate BMP files.
How can I get a transparent background for a PNG file?
And if that's not possible, since JPEG files are not good for text, is there a better alternative?
This isn't an answer, but the format of the comment section doesn't allow sensible formatting, so I am offering it here instead.
You alluded to the fact that Preview in OS X doesn't show transparency properly, and that is correct. To get around that, I made the following script, which overlays the files you want to view onto a checkerboard pattern like Photoshop does. I save it as preview and then use chmod +x preview on it to make it executable. Here is the script:
#!/bin/bash
################################################################################
# preview
# Preview images using OSX "open" but overlay images on top of checkerboard
# first so you can see transparency/transparent pixels, since Preview renders
# them grey :-(
################################################################################
for f in "$#"
do
base=$(basename "$f")
composite -compose Dst_Over -tile pattern:checkerboard "$f" "/tmp/$base"
open -g "/tmp/$base"
done
That enables you to do:
/.preview file1.png file2.gif
and the -g option also means Preview doesn't steal focus too :-)
So, instead of looking like this:
it looks like this:
There's two ways to export a PDF in LibreOffice, and one way does not provide any warnings. The other way provides a warning that PDF/A cannot have transparent objects. The problem is you're using PDF/A files that don't support transparent objects—this results in your PNG always having a background colour.
I'm running Embedded Linux on an evaluation kit (Zoom OMAP35x Torpedo Development Kit). The board has an LCD and I would like to be able to take screen shots convert them into a gif or png. I can get the raw data by doing the following: "cp /dev/fb0 screen.raw", but I am stumped on how to convert the image into a gif or png format.
I played around with convert from ImageMagick (example: "convert -depth 8 -size 240x320 rgb:./screen.raw -swap 0,2 -separate -combine screen.png"), but have been unable to get an image that looks right.
Does anyone know of any other tools that I could try out? Or does anyone have tips for using ImageMagick?
Take a look at fbgrab, an application that does just that (it saves the framebuffer content as a png).
You can simply capture the framebuffer to a file and open it in any raw image viewer or try online eg: https://rawpixels.net/
cat /dev/fb0 > fbdump
It might not be possible / easy to do it directly with ImageMagick.
The Linux kernel 4.2 documentation https://github.com/torvalds/linux/blob/v4.2/Documentation/fb/api.txt#45 says:
Pixels are stored in memory in hardware-dependent formats. Applications need
to be aware of the pixel storage format in order to write image data to the
frame buffer memory in the format expected by the hardware.
Formats are described by frame buffer types and visuals. Some visuals require
additional information, which are stored in the variable screen information
bits_per_pixel, grayscale, red, green, blue and transp fields.
Visuals describe how color information is encoded and assembled to create
macropixels. Types describe how macropixels are stored in memory. The following types and visuals are supported.
A list of visuals and types follows, but the description is not enough for me to understand the exact formats immediately.
But it seems likely that it might not be a format that ImageMagick will understand directly, or at least you'd have to find out the used format to decide the ImageMagick options.