Partitioning Images Based on Their White Space

Partitioning images based on their white space

As @ypnos said, you want to collapse the rows by summation, or averaging. That will leave you with a vector the width of the image. Next clip everything below a high threshold, remembering that high numbers correspond to high brightness. This will select the white space:

The result of clipping the collapsed brightness.

Then you simply cluster the remaining indices and select the middle two clusters (since the outer two belong to the bordering white space). In python this looks like so:

import sklearn.cluster, PIL.Image, numpy, sys, os.path
# import matplotlib.pyplot as plt

def split(fn, thresh=200):

img = PIL.Image.open(fn)
dat = numpy.array(img.convert(mode='L'))
h, w = dat.shape
dat = dat.mean(axis=0)
# plt.plot(dat*(dat>thresh);

path, fname = os.path.split(fn)
fname = os.path.basename(fn)
base, ext = os.path.splitext(fname)

guesses = numpy.matrix(numpy.linspace(0, len(dat), 4)).T
km = sklearn.cluster.KMeans(n_clusters=2, init=guesses)
km.fit(numpy.matrix(numpy.nonzero(dat>thresh)).T)
c1, c2 = map(int, km.cluster_centers_[[1,2]])

img.crop((0, 0, c1, h)).save(path + '/' + base + '_1' + ext)
img.crop((c1, 0, c2, h)).save(path + '/' + base + '_2' + ext)
img.crop((c2, 0, w, h)).save(path + '/' + base + '_3' + ext)

if __name__ == "__main__":
split(sys.argv[1], int(sys.argv[2]))

One shortcoming of this method is that it may stumble on images with bright objects (failing to properly identify the white space), or are not separated by a clean vertical line (e.g., overlapping in the composite). In such cases line detection, which is not constrained to vertical lines, would work better. I leave implementing that to someone else.

ImageView shows white space at top and bottom of image

add these attributes into that ImageView

android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"

How to remove vertical white space from an image

As said in the comments you would usually set the figure size and the subplotparams such that there is no space in the direction of choice. If the yaxis does have a given size which needs to be preserved you can first calculate its length, then adjust the figure size and subplot parameters such that the axis length is kept the same.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(5,6), dpi=50)
ax.plot([1,2])
ax.set_ylabel("ylabel")
ax.set_xlabel("xlabel")

### 1. Original image
ax.set_title("original")
plt.savefig("fsi01.png", facecolor="#edf9f9")
### 2. tight bbox
ax.set_title('bbox_inches="tight"')
plt.savefig("fsi02.png", facecolor="#edf9f9", bbox_inches="tight")

### 3. Only vertical adjustent
axes_height = fig.get_size_inches()[1]*(fig.subplotpars.top-fig.subplotpars.bottom)

top = 0.94; bottom=0.09
fig.subplots_adjust(top=top,bottom=bottom)
fig.set_size_inches((fig.get_size_inches()[0],axes_height/(top-bottom)))

ax.set_title('only vertical adjust')
plt.savefig("fsi03.png", facecolor="#edf9f9")

plt.show()

Sample Image
Sample Image
Sample Image

Of course the values top = 0.94; bottom=0.09 would need to be determined in each case individually.

Remove White Space above header image

The top margin is coming from the <h2> inside the parallaxImage div.

h2 {
margin-top:0;
}

Will resolve the issue.

Using wand.image + python 2.7 to trim white space

There should be a Image.trim method to do this.

>>> from wand.image import Image
>>> from wand.color import Color
>>> with Image(filename="logo:") as img:
... img.trim(Color("WHITE"))
... img.save(filename="output.png")

output.png

How to create an .IMG image of a disc (sd card) without including free space?

The best thing to do is

  1. Copy all the files from all the partitions preserving meta data

    mkdir -p myimage/partition1

    mkdir myimage/partition2

    sudo cp -rf --preserve=all /media/mount_point_partition1/* myimage/partition1/

    sudo cp -rf --preserve=all /media/mount_point_partition2/* myimage/partition2/

  2. Extract the MBR

    sudo dd if=/dev/sdX of=myimage/mbr.img bs=446 count=1

    replace /dev/sdX with the corresponding device.

  3. Partition the destination disk into partitions with sizes greater than copied data and should be of the same format and same flags using gparted. Google how to partition a disk.

  4. Mount the freshly formatted and partitioned disk. On most computers, you just need to connect the disk and you can find the mounted partitions in /media folder.

  5. Copy the previously copied data to destination partitions using following commands

    sudo cp -rf --preserve=all myimage/partition1/* /media/mount_point_partition1/

    sudo cp -rf --preserve=all myimage/partition2/* /media/mount_point_partition2/

  6. Copy back the MBR

    sudo dd if=myimage/mbr.img of=/dev/sdX bs=446 count=1

Now njoy Ur new disk!



Related Topics



Leave a reply



Submit