Skip to content

Commit

Permalink
added support for wrapping offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
theFroh committed Sep 13, 2015
1 parent 9c1f609 commit a8e29d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
Binary file modified imagepacker/__pycache__/imagepacker.cpython-34.pyc
Binary file not shown.
26 changes: 18 additions & 8 deletions imagepacker/imagepacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def crop_by_extents(image, extent, wrap=False):
w,h = image.size
coords = [math.floor(extent.min_x*w), math.floor(extent.min_y*h),
math.ceil(extent.max_x*w), math.ceil(extent.max_y*h)]
print("\nEXTENT")
pprint(extent)

if min(extent.min_x,extent.min_y) < 0 or max(extent.max_x,extent.max_y) > 1:
Expand All @@ -160,18 +161,27 @@ def crop_by_extents(image, extent, wrap=False):
new_w, new_h = new_im.size

# Iterate through a grid, to place the background tile
for i in range(coords[0], new_w, w):
for j in range(coords[1], new_h, h):
for i in range(0, new_w, w):
for j in range(0, new_h, h):
#paste the image at location i, j:
new_im.paste(image, (i, j))

coords[0] = coords[0]
coords[1] = coords[1]
crop_coords = coords.copy()

coords[2] = coords[2]
coords[3] = coords[3]
if crop_coords[0] < 0:
crop_coords[2] = crop_coords[2] - crop_coords[0]
crop_coords[0] = 0
if crop_coords[1] < 0:
crop_coords[3] = crop_coords[3] - crop_coords[1]
crop_coords[1] = 0

image = new_im.crop(coords)
# crop_coords[0] = crop_coords[0]
# crop_coords[1] = crop_coords[1]

# crop_coords[2] = crop_coords[2]
# crop_coords[3] = crop_coords[3]

image = new_im.crop(crop_coords)

else:
coords[0] = max(coords[0], 0)
Expand Down Expand Up @@ -228,7 +238,7 @@ def pack_images(image_paths, background=(0,0,0,0), format="PNG", extents=None, w
packer = BlockPacker()
packer.fit(blocks)

output_image = Image.new("RGB", (packer.root.w, packer.root.h))
output_image = Image.new("RGBA", (packer.root.w, packer.root.h))

uv_changes = {}
for block in blocks:
Expand Down
13 changes: 10 additions & 3 deletions objuvpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def main():
if line.startswith("newmtl"):
name = line[7:]
print("\tsaw material name", name)
if name != "None":
if name and name != "None":
if len(dmaps) != len(names):
print("\tlast material did not have a diffuse, ignoring")
names.pop()
Expand All @@ -101,7 +101,9 @@ def main():
# dmap = guess_realpath(line[7:])
dmap = guess_realpath(m)
if not dmap:
raise ValueError("missing a required texture file " + line)
# raise ValueError("missing a required texture file " + line)
print("\tmissing a required texture file " + line)


# if dmap not in dmaps:
dmaps.append(dmap)
Expand Down Expand Up @@ -167,13 +169,15 @@ def __repr__(self):

uv_lines = []
curr_mtl = None
used_mtl = set()

for line_idx, line in enumerate(obj_lines):
if line.startswith("vt"):
uv_lines.append(line_idx)
elif line.startswith("usemtl"):
mtl_name = line[7:]
curr_mtl = mtl_name
# print("changed to", curr_mtl)
elif line.startswith("f"): # face definitions
for vertex in line[2:].split(): # individual vertex definitions
v_def = vertex.split(sep="/")
Expand All @@ -184,13 +188,16 @@ def __repr__(self):
uv = [float(uv.strip()) for uv in uv_line.split()]

if curr_mtl and curr_mtl in texmap:
# pprint(uv)
used_mtl.add(mtl_name)
textents[texmap[curr_mtl]].add(uv[0], uv[1])
else:
print(curr_mtl, "not in texmap")
# print(curr_mtl, textents)
# get uv values at uv_idx
# alter them in the original file

# pprint(textents)
# pprint(used_mtl)

if args.wrap:
# loop through UV AABB's, warning when out of range and prompting
Expand Down

0 comments on commit a8e29d9

Please sign in to comment.