Resizing screenshots/screen captures for inclusion in Beamer - image-processing

Sorry, this may or may not be a programming question directly, but I am trying to resize screenshots with Imagemagick and Gimp to include in a Beamer presentation, but it comes out even blurrier than the resizing done by LaTeX.
For instance, in Beamer I might have a command to rescale the image \includegraphics[width=.5\textwidth]{fig.png}. Using something like
\begin{frame}
\message{width = \the\textwidth}
\message{height = \the\textheight}
\end{frame}
I have gotten the \textwidth and \textheight parameters in points (345.69548, 261.92444). So I have a script (in Python) that sends a system call to Imagemagick:
'convert %s -resize %.6f# resized_%s' % (f,a,f)
where a is calculated as \textwidth*\textheight*0.5**2 and f is the file name. When I then go back into my Beamer presentation and include the resized figure, \includegraphics{resized_fig.png}, the size looks approximately correct but it's super-blurry. I also tried resizing in Gimp (using the GUI) but no luck either... help? Thanks...

If you want to preserve pixel sharpness, i.e. do not scale and interpolate pixels, but represent them as small squares, I suggest this approach:
Convert you PNG image to EPS/PDF with sam2p.
Include converted EPS or PDF in your document as usual. Every pixel will be just a small rectangular box, it will look crisp and sharp, and will scale without interpolation.
For example, let's assume we have a small raster image, that we want to show:
We can convert it to vector PDF with this command:
sam2p 10x10.png PDF: 10x10.pdf
Now as we have a vector version (where every pixel is a rectangular), we can include it in whatever LaTeX document and scale freely. All pixels will scale, but will not be interpolated.
For example,
\documentclass[12pt]{article}
\usepackage[papersize={4cm,4cm},margin=2pt]{geometry}
\usepackage{graphicx}
\begin{document}
\includegraphics[width=0.8\linewidth]{10x10.pdf}
\end{document}
This will look like this:
Drawbacks:
PDF version of the image is likely to be several times bigger than its original PNG version.

Related

How to change GIMP print size with script-fu

I am using GIMP 2.10.24. I have some image and I need to change Print Size Width to 21mm and Height to 30mm.
I can do that with Set Image Print Resolution Dialog (Menu->Image->Print Size):
screenshot
But there is my question: how could I do that using script-fu or python-fu?
Print size, size in pixels, and print definition are completely related:
print size = size in pixels รท print definition
So to change the image print definition you use
In Python:
pdb.gimp_image_set_resolution(image, xresolution, yresolution)
In Script-fu:
(gimp-image-set-resolution image xresolution yresolution)
In both case the X/Y resolutions are in dots per inch.
However if you are using Gimp just for this creating a Gimp script is overkill (the learning curve is quite steep). If the image is in a common format (JPEG, PNG, TIFF) the print definition is part of the image metadata (JPEG header, or EXIF data) and can be changed directly without decoding/reencoding the image using CLI utilities. For instance with ExifTool:
exiftool ${your_image} -xResolution=321 -yResolution=321

Gimp exports: Why is the image size increased?

I'm trying to use a two step process of employing Gimp to delete sections of images and then using Inkscape for the remainder of the image work.
Unfortunately, I'm seeing a resolution change when doing the export to PNG from Gimp.
The exported image is around 50% larger than the original, which impacts the quality.
Is there a way to keep the resolution constant when exporting the file?
Hopefully I'm just forgetting something, since I've spent some time away from image work.
Please let me know if any additional info is required.
In the interim, I'll try another tool to do the Gimp step.
THANKS!
Edit: Updated size to resolution.
For a bitmap/raster image, resolution (for Gimp: "Image print resolution", see Image>Print size) is indicative. The only thing that counts is the size in pixels.
If you have image window set to "Dot for Dot" (Edit>Preferences>Image Windows->General>"Use dot for dot" or View>Dot for dot) the image is displayed with the definition of your screen (around 100PPI fore regular screens, 20OPPI for high def ones (Retina, etc...).
When you create the image (File>New...), you can specify a print definition and a print size, and Gimp will compute the required size in pixels.

How can I convert image background pattern?

On this similar thread they have been proposed solutions to convert the background color of some image to transparent.
But sometimes the background is a simple pattern, like in this case:
Note the square background pattern.
When processing images, the background does often need to be removed or changed, but firstly you need to detect it (i.e: change its color, or making it transparent). For example, on the above image example, I would like to obtain:
How can I detect/change a specified pattern inside an image?
GUI solutions accepted.
Open source solutions preferred (free at least required).
The simplest solution will be preferred (I would like to avoid installing some hundreds of MB program).
Note: I was thinking about posting this question at Photography
StackExchange site, but I would rather say the mind of a programmer (I
could need to edit dozens of such images) is more close to what I
want. I am not an artist.
This is not a fully developed answer, but it does go some way towards thinking about a method - maybe someone else would like to develop it...
If, as you say, your pattern is specified, you know what it is - good, aren't I? So, you could look for the "Minimum Repeating Unit" of your pattern in your image. In your case it is a 16x16 grid like this:
Now you can search for that pattern in your image. I am using ImageMagick at the command-line, but you can use other tools if you prefer. ImageMagick is installed on most Linux distros and is available for OSX and Windows for free. So, I search for that grid in your globe image and ImageMagick gives me an output image showing white dots at the top-left corner of every location where the two images match:
compare -metric ae -dissimilarity-threshold 1.0 -compose src -subimage-search globe.gif grid.png res.png
That gets me this in file res-1.png
Now the white dots are where the 16x16 "Minimum Repeating Unit" is found in the image, but at the top-left corner so I shift them right and down by 8 pixels to the centre of the matching area, then I create a new output image where each pixel is the maximum pixel of the 16x16 grid in which it existed before:
convert res-1.png -roll +8+8 -statistic maximum 16x16 z.png
I can now invert that mask and then use it to set the opacity of the original image, thereby blanking areas that matched the "Minimum Repeating Unit":
convert globe.gif \( z.png -negate \) -compose copy_opacity -composite q.png
No, it's not perfect, but it is an idea for an approach that could be refined...

How to batch convert images to a specific megapixel with Imagemagick?

I'm sorting my photos and want to resize them to something reasonable. Now the input files are a wild mix of small and big images, some landscape, some tower.
I want to resize them all (above a threshold) to a specific megapixel size, but all descriptions I found so far only resize to either a fixed size / aspect ratio (e.g. 1024x786) or a fixed percentage (e.g. 50%). None of these seem to apply with a folder that has some small images (e.g. 300x400) and images mixed in the range of 3-12 megapixel.
I'm looking for an aspect ratio independent option that puts them all in the same megapixel ballpark. Any suggestions?
Add \> at the end of your resize to only down-res larger images and not affect smaller ones and maintain aspect ratio too.
convert -resize 1200X800\> image.jpg
Or if you want a specific number of pixels, regardless of whether landscape or portrait, you can do
convert -resize 1000000#\> image.jpg
so you get a million pixels max, which may be 400x2,500 or 2,500x400 for example.
EDITED
I should maybe point out that the backslash in front of the greater than symbol is a shell escape to stop the shell deciding to redirect output like it normally does when it sees a greater than symbol. You could equally enclose the resize specification in single quotes to prevent the shell seeing it. If that doesn't make sense, then the following two commands are identical:
convert -resize 1000000#\> image.jpg
convert -resize '1000000#>' image.jpg

Converting between pt and pixels

My netbook has a monitor with 10.1 inch length and 1024*600 solution. I think 1pt is about 1/72inch - is the following computation right?
Since the resolution is 1024*600, the
diagonal has about 1186.83 pixels,
thus 1 inch is about 1186.83/10.1 =
117.51 pixels, and thus 1pt is about 117.51/72 = 1.63 pixels, or 1 pixel is about 0.6127 pt.
Using this relationship, I've inserted an image into a LaTeX document, converting pixels to pt, and take the result as a parameter to includegraphics, but the figure in the resulting document is rather blurred.
Is the computation correct? if
not, how or
where am I wrong?
How can I insert an image into a LaTeX document
with precisely the same dimensions as the
original?
Updated:
I'm using pdflatex to compile the document and the image is a png file, and the reason why I do such a stupid computation is that with no width parameter set, the image shown in the document is larger than the actual size, and I can't work out why.
LaTeX is for creating paper documents. The units it uses refer to distance on the paper output, not on the screen. So if you ask for a distance of 72pts in LaTeX, you'll get 1 inch on your printout, but the distance you get on your screen depends on the zoom-level of your pdf or ps reader, which probably doesn't know how big your screen is.
If you simply want to get the raster grid of your image to fit to the screen grid, I'd say the best thing to do is get a higher-quality graphic if you can.
(PS there's a TeX stack exchange site where you could ask your question too...)
I don't know if it will make any difference, but TeX actually uses 72.27 points to the inch (as it predates PostScript, which set the standard 72 points to the inch).

Resources