Skip to content
This repository has been archived by the owner on Mar 31, 2023. It is now read-only.

Image Slicing Specific Size #46

Open
MaisumAbbas opened this issue Jul 27, 2021 · 4 comments
Open

Image Slicing Specific Size #46

MaisumAbbas opened this issue Jul 27, 2021 · 4 comments

Comments

@MaisumAbbas
Copy link

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.

@prettyflyforabeeguy
Copy link

+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!

@ghost
Copy link

ghost commented Dec 27, 2021

please can you clarify it in detail or script

@prettyflyforabeeguy
Copy link

prettyflyforabeeguy commented Dec 28, 2021

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.

@ghost
Copy link

ghost commented Dec 29, 2021

thanks

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants