image_slicer icon indicating copy to clipboard operation
image_slicer copied to clipboard

Image Slicing Specific Size

Open MaisumAbbas opened this issue 3 years ago • 4 comments

Use Case

Please provide a use case to help us understand your request in context

Can we set image slices to specific size? For example, I am creating 6 slices but the third slice needs to be a bit more in height as compared to other slices.

I was reading this documentation of 2018: https://readthedocs.org/projects/image-slicer/downloads/pdf/latest/ in which it's been said it will be overridable but I did not find any option which can do that or may be I missed it.

MaisumAbbas avatar Jul 27 '21 07:07 MaisumAbbas

+1 to slicing a specific size! I'd like each tile to be under a specific size. So if the starting image is 1,113 bytes, at 200 bytes per tile, I'd expect 6 images after the slice. Instead of them being equal size, what I see happening breaks down more like this: tile 1: 254 bytes tile 2: 203 bytes tile 3: 234 bytes tile 4: 274 bytes tile 5: 213 bytes tile 6: 226 bytes Total: 1,404 bytes I'm sure I'm missing something so corrections to my understanding are welcome!

prettyflyforabeeguy avatar Dec 01 '21 18:12 prettyflyforabeeguy

please can you clarify it in detail or script

ghost avatar Dec 27 '21 13:12 ghost

import image_slicer
from PIL import Image
import os
import math

file1 = "./img/logo.png"
file2 = "./img/earth.png"

img1 = Image.open(file1)
>>>img1
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=49x52 at 0xB6622710>

img2 = Image.open(file2)
>>> img2
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=120x75 at 0xB5CE1470>

file1_size = os.path.getsize(file1)
>>> file1_size
1113

file2_size = os.path.getsize(file2)
>>> file2_size
24928

nImages_f1 = int(file1_size) / 150
>>> nImages_f1
7.42

nImages_f2 = int(file2_size) / 150
>>> nImages_f2
166.18666666666667

# Round up
roundImg1 = math.ceil(nImages_f1)
roundImg2 = math.ceil(nImages_f2)

def sliceImage(self, sourcefile, nSplits, destdir):
    # Slice in two steps so a destination directory can be used.
    print("Slicing images...")
    tiles = image_slicer.slice(sourcefile, nSplits, save=False)
    filename = os.path.basename(sourcefile)
    prefix = filename[:-4]
    image_slicer.save_tiles(tiles, prefix=prefix, directory=destdir, format='png')
    print("Done Slicing!")

In this example above, I am attempting to divide the file size by 150 hoping that each tile split out results in this file size. So in the functions I can pass a roundImg value in nSplits and have a designated number of files broken into the same size.

prettyflyforabeeguy avatar Dec 28 '21 23:12 prettyflyforabeeguy

thanks

ghost avatar Dec 29 '21 10:12 ghost