I am trying to do a pyramid of tiles with a non-square image (width: 32768px and height: 18433px)
I am using libvips as follows:
vips dzsave my_image.tif out_folder --layout google --suffix .png
For the same purpose I have also used gdal2tiles:
python gdal2tiles.py -p raster -z 0-7 -w none my_image.tif
Because my image is not square, some padding is necessary when the 256x256 tiles are created. Padding however is different between vips and gdal2tiles. The former adds padding at the bottom of the tile where as the latter at the top (and is trasparent). See image below. What is shown in the the 256x256 tile at the root of the pyramid (ie zoom level=0). I have manually added the yellow background and the black outline.
With vips, is it possible to have similar padding to gdal2tiles so that the bottom-left corner of the tile coincide with that from the image? I am plotting points on my image, hence it helps to have the origin at the bottom-left.
How can I also have transparent background with vips? (that might better be in a separate post though...)
You can run dzsave as the output of any vips operation by using .dz as the file extension and putting the arguments in square brackets after the filename. For example, this command:
vips dzsave my_image.tif out_folder --layout google --suffix .png
Can also be written as:
vips copy my_image.tif out_folder.dz[layout=google,suffix=.png]
So you can solve your problem by expanding your input image to a square before running dzsave.
For example:
$ vips gravity Chicago.jpg dir.dz[layout=google,suffix=.png,skip_blanks=0] south-west 32768 32768 --extend white
32768 is the nearest power of two above that image width. The skip_blanks option makes dzsave not output tiles equal to the blank background tile.
That command makes this dir/0/0/0.png:
(I added the black lines to show the edges)
To get a transparent background, you need to add an alpha. This would require another command, and is beyond what the vips CLI is really designed for.
I would switch to something like Python. With pyvips, for example, you can write:
import sys
import pyvips
im = pyvips.Image.new_from_file(sys.argv[1], access='sequential')
im = im.addalpha()
# expand to the nearest power of two larger square ... by default, gravity will
# extend with 0 (transparent) pixels
size = 1 << int.bit_length(max(im.width, im.height))
im = im.gravity('south-west', size, size)
im.dzsave(sys.argv[2],
layout='google', suffix='.png', background=0, skip_blanks=0)
Run like this:
$ ./mkpyr.py ~/pics/Chicago.jpg x
To make this x/0/0/0.png:
(added the green background to show the transparency)
Related
We're doing a cognitive experiment that requires flashing 480 black-and-white images at high speed against a neutral-gray background. The image shown is detail at about 300% zoom; the full display is 932 x 612; there are actually other clusters similar to this one elsewhere on the screen.
We have to generate these images in a source app and later display them in the experimental app. At first, we were using 24-bit PNG's with the background color built in, but despite the size of each file hovering around 10KB, the total file payload of 480 is too large and takes way too long to preload. We can, however, set a default background color in the app and put up 1-bit images. Although we can change the background color in the source application, we don't have control over bit depth there, so we are stuck generating screen captures and then downsampling to binary. We have tried this with original images that sit on both a white background and the gray background. Using ImageMagick, both of these commands yield similar results:
magick gray.png -transparent "rgb(192,192,192)" -alpha set -depth 1 out\gray-out.bmp (or .png)
magick white.png -transparent white -alpha set -depth 1 out\white-out.bmp (or .png)
The PNGs are coming out to 1.5K to 5K, so this is where we want to be. However, we're stuck with that problem aliased edge whether we start with gray- or white-background images. We've also tried the -fuzz argument (1-99%) to no effect. The resulting pseudoalias artifact that we see in these 1-bit files is the same in multiple apps, so apparently it must be built into the output file generated by ImageMagick's attempt to filter the edge, and not an artifact of any single app or the OS.
We don't quite get how an alpha channel can even be attached to a 1-bit image if it is not strictly RGBA: an alpha channel with a 1-bit image doesn't even make sense. But if an alpha channel isn't being included, then where is the map coming from in a nonbinary image to distinguish that "white" edge from a nonwhite surface? It's not the destination apps. The output files must either not be strictly binary, or else ImageMagick is not computing the channel exactly to the contour edges. (When we remove the -alpha argument, ImageMagick generates a larger file; the aliasing (e.g., when we place into PowerPoint and select Transparent Color to white) may appear in slightly different places, but the effect is essentially the same.
The question is, how is it possible to remove this artifact in a 1-bit image using ImageMagick? We'd like to get the file sizes down to the minimum.
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...
We need to have huge amounts of png's resized to be divisible by 12, each png is variable in size and the image needs to stay 1:1 in the top left.
At the moment were having to manually bring in each file into Photoshop and enlarge the canvas on the x+y to be divisible by 12 and keep the image in the top left corner. With the amount of png's we need doing now and in future we need an automated process.
I would do this with ImageMagick, which is free and installed on most Linux distros and also available for OSX and Windows from here.
This little bash script will resize all the PNG files in the current directory and save them with the original name in the subdirectory called output. It is pretty easy to read - it basically loops through all the PNG files in the directory. It then uses ImageMagick's built-in calculator to work out the size of your output file as nearest multiple of 12. Then it loads the image and extends the background using transparent pixels (-background none) to that size (using -extent) and leaves the original image in the top-left corner (-gravity NorthWest).
#!/bin/bash
# Make output directory - ignore errors
mkdir output 2> /dev/null
# Make sure we don't barf if there are no files
shopt -s nullglob
# Make sure we process *.png, *.PNG, *.pNg etc
shopt -s nocaseglob
# Loop through all pngs in current directory
for f in *.png; do
# Calculate new extent as nearest multiple of 12
# In general, to round x to nearest n, you do ((x+n-1)/n)*n
extent=$(convert "$f" -format "%[fx:12*round(((w+11)/12)-0.5)]x%[fx:12*round(((h+11)/12)-0.5)]" info: )
# Now extend canvas transparently to new size and leave original image in top-left
convert "$f" -background none -gravity northwest -extent $extent output/"$f"
done
P.S. If installing ImageMagick on OSX, please ask for advice before trying.
P.P.S. If you have 10,000+ images to resize, and you do it often, and you are on OSX or Linux (probably not Windows), I would recommend GNU Parallel. If that is likely, please ask.
Never mind, this is a possible solution for your problem. This script will run in MATLAB or Octave (Octave is an open-source alternative to MATLAB, so you might want to use that.)
Copy the following function into a file and call it resizeIm.m. Then start Octave and call this function for every image you have.
function resizeIm(fileName)
% Read image
origIm = imread(fileName);
% Get size and calculate new size
origSize = size(origIm);
div = ceil(origSize ./ 12);
% Create new, padded image
newIm = zeros(12*div,class(origIm));
newIm(1:origSize(1),1:origSize(2)) = origIm;
% Write image to new file
[dir, name, ext] = fileparts(fileName);
newFileName = [dir,name,"_resized",ext];
imwrite(newIm,newFileName);
end
The function can be called by
resizeIm("C:\path\to\file\myimage.png")
I want to group 16 pixels into a single unit in an image.
For eg: If the image is a 16 x 16 image then the number of cells(ie, group of 16 pixels=1cell) that I would be creating will be 16. How to create a cell and display each cell with the respective pixel values?
I am not 100% sure I understand the maths of your question, but let's assume you have this image which is 48x32 - I am showing the images 10x larger than life so you can see them.
I would use ImageMagick, which is installed on most Linux distros, and is readily downloadable for OS X and Windows. Download here or use homebrew on OS X.
I presume you want to chop the image up into lumps that measure 16x16, so you would use this command:
convert -crop 16x16 +repage tile%d.png
and you will get 6 output files, one red, one green, one blue, one black one white and one grey, called tile0.png, tile1.png ... tile5.png.
tile0.png
tile1.png
I am doing a comparison with imagemagick on a project. I have a reference image and test image. I deleted few lines to make changes in test image. This reduced the size of test image. Is there any way that if I could add some white padding at bottom to test image so that while comparing reference image and test image the size of two images remains same.
Please help!
convert -border 5x5 inputimage.png outputimage.png
This command will draw a 5x5 pixel border around your image. Further, you can selectively control at the edges, color and size of the borders drawn on an image. This link will show how to do that.