Deleting rows and columns from an image without leaving gaps

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Iarla
Posts: 4
Joined: 2017-09-17T15:16:36-07:00
Authentication code: 1151

Deleting rows and columns from an image without leaving gaps

Post by Iarla » 2017-09-17T15:25:01-07:00

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

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2017-09-17T15:49:38-07:00

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

User avatar
fmw42
Posts: 22101
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

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

Post by fmw42 » 2017-09-17T18:25:03-07:00

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

Iarla
Posts: 4
Joined: 2017-09-17T15:16:36-07:00
Authentication code: 1151

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

Post by Iarla » 2017-09-18T00:21:39-07:00

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

snibgo
Posts: 9399
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2017-09-18T04:19:42-07:00

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
snibgo's IM pages: im.snibgo.com

Iarla
Posts: 4
Joined: 2017-09-17T15:16:36-07:00
Authentication code: 1151

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

Post by Iarla » 2017-09-18T06:10:01-07:00

I can confirm that that works too snibgo

snibgo
Posts: 9399
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2017-09-18T06:50:01-07:00

Good stuff. I've realised that the "-extent 3084x2250" isn't necessary, so you can remove that.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

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

Post by GeeMack » 2017-09-18T08:22:27-07:00

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".

snibgo
Posts: 9399
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post by snibgo » 2017-09-18T09:42:05-07:00

Very neat, GeeMack.
snibgo's IM pages: im.snibgo.com

Iarla
Posts: 4
Joined: 2017-09-17T15:16:36-07:00
Authentication code: 1151

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

Post by Iarla » 2017-09-18T11:02:31-07:00

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!

Post Reply