Page 1 of 1

Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-17T15:25:01-07:00
by Iarla
There is a grid overlaid on an image I want to correct. It's a stitched-together group of screenshots from a game[1]. What switch/argument on imagemagick allows me to delete rows / colums without leaving a gap? In other words, for every column of 2px I delete, the overall image should become 2px less wide.

1: http://amiga.lychesis.net/game/Flashbac ... vel01.html

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-17T15:49:38-07:00
by fmw42
What do you want to do with the black background regions?

Please always provide your IM version and platform, since syntax may vary.

There is no simple method of doing that in Imagemagick, that I know about. You can crop the image into tiles. Then chop off appropriate edges of each image. Then you can montage all the images together again. You can create a script to do that.

Or if you make the grid lines transparent, then crop into regularly spaced tiles, you can use the -smush function to append them together again moving them so that the transparent areas do not make gaps. But you need smash the tiles together for each row and then -smush the rows together.

See
http://www.imagemagick.org/Usage/crop/#crop_tile
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#chop
http://www.imagemagick.org/Usage/montage/
http://www.imagemagick.org/script/comma ... .php#smush

Alternately, if on Windows, user snibgo has a hole filling (pinpointing) tool, that can fill in texture from the image where the image is transparent. So you could recolor you lines to make them transparent and then fill in the image texture.

See
http://im.snibgo.com/fillholes.htm
http://im.snibgo.com/fillholespri.htm

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-17T18:25:03-07:00
by fmw42
Another option would be to write a script to loop over each tile location and crop that tile skipping over the gridlines. Then montage all the tiles together. This would be the simplest approach, I think. But scripting is different for Windows and Unix-like systems. Of course you need to know ahead of time how big the tiles are without the gridlines and how thick the grid lines are and how many rows and columns of tiles you want.

In Unix bash shell scripting language, you can use sub shell processing to the following with the assumptions that:

1) your grid lines are 2 pixels thick
2) your tile size is 512x448
3) you have a 6x5 arrangement of cells

Code: Select all

convert Flashback_Level01.tft1.png Flashback_Level01.tft1.mpc
(
for ((j=0; j<5; j++)); do
yoff=$((j*450))
for ((i=0; i<6; i++)); do
xoff=$((i*514))
convert Flashback_Level01.tft1.mpc -crop 512x448+${xoff}+${yoff} miff:-
done
done
) | montage - -tile 6x5 -geometry +0+0 Flashback_Level01.tft1_new.png
rm -f Flashback_Level01.tft1.mpc Flashback_Level01.tft1.cache
http://www.fmwconcepts.com/misc_tests/t ... t1_new.png

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T00:21:39-07:00
by Iarla
Hi fmw42,

Wow, this is really impressive, thank you! It doesn't seem to have taken any of the picture away from the outer edges either which is great. I am on linux. Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31, so the script is perfect.

Thanks again,
Iarla

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T04:19:42-07:00
by snibgo
This can be done in a singe "convert" (or "magick"). First, "-extent" to add two extra columns on the right and rows at the bottom. Then crop into vertical strips each 514 pixels wide. These all have an extra 2 pixels on the right, so crop them all to 512 pixels wide, and append them sideways. Similarly, crop into horizontal strips each 450 pixels high, crop off the bottom two rows from each, and append them vertically.

Windows BAT syntax. For bash, change line endings to "\".

Code: Select all

convert ^
  Flashback_Level01_tft1.png ^
  -gravity NorthWest -extent 3084x2250 ^
  -crop 514x +repage ^
  -crop 512x+0+0 +repage ^
  +append ^
  -crop x450 +repage ^
  -crop x448+0+0 +repage ^
  -append ^
  fout.png

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T06:10:01-07:00
by Iarla
I can confirm that that works too snibgo

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T06:50:01-07:00
by snibgo
Good stuff. I've realised that the "-extent 3084x2250" isn't necessary, so you can remove that.

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T08:22:27-07:00
by GeeMack
Iarla wrote:
2017-09-17T15:25:01-07:00
What switch/argument on imagemagick allows me to delete rows / colums without leaving a gap? In other words, for every column of 2px I delete, the overall image should become 2px less wide.
If your tiles are always all the same size and your separator lines are always 2 pixels wide you can do something like this...

Code: Select all

convert input.png -border 1 -crop 6x1@ -shave 1 +append -border 1 -crop 1x5@ -shave 1 -append output.png
That adds a single pixel border all around, crops the image into 6 columns, then shaves 1 pixel from all around each piece. The shaving includes the border you added before the crop and the vertical borders that were between the columns. Then it reassembles the pieces with "+append". Follow that by adding a 1 pixel border again, cropping the image into 5 horizontal rows, shaving the 1 pixel that surrounds each piece, and reassemble them vertically with "-append".

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T09:42:05-07:00
by snibgo
Very neat, GeeMack.

Re: Deleting rows and columns from an image without leaving gaps

Posted: 2017-09-18T11:02:31-07:00
by Iarla
I can confirm that one works too GeeMack :) I think your solution is easier to understand and therefore modify for a newbie like myself. I can't get over how many possibilities this tool provides!