Skip to content

Commit

Permalink
Fixed disabling wrapping, disabled some debug printouts
Browse files Browse the repository at this point in the history
  • Loading branch information
theFroh committed Oct 7, 2015
1 parent a8e29d9 commit 30114a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 76 deletions.
Binary file modified imagepacker/__pycache__/imagepacker.cpython-34.pyc
Binary file not shown.
67 changes: 12 additions & 55 deletions imagepacker/imagepacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,32 +138,30 @@ def grow_down(self, w, h):
return None


def crop_by_extents(image, extent, wrap=False):
def crop_by_extents(image, extent, wrap=False, crop=False):
image = image.convert("RGBA")
# overlay = Image.new('RGBA', image.size, (255,255,255,0))

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)
# # print("\nEXTENT")
# pprint(extent)

if min(extent.min_x,extent.min_y) < 0 or max(extent.max_x,extent.max_y) > 1:
print("WARNING! UV Coordinates lying outside of [0:1] space!")

pprint(coords)
# pprint(coords)

if extent.to_wrap:
h_w, v_w = extent.wrapping()
print("Ye", h_w, v_w)

new_im = Image.new("RGBA", (math.ceil(h_w*w), math.ceil(v_w*h)))
new_w, new_h = new_im.size

# Iterate through a grid, to place the background tile
# Iterate through a grid, to place the image to tile it
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))

crop_coords = coords.copy()
Expand All @@ -175,14 +173,7 @@ def crop_by_extents(image, extent, wrap=False):
crop_coords[3] = crop_coords[3] - crop_coords[1]
crop_coords[1] = 0

# 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)
coords[1] = max(coords[1], 0)
Expand All @@ -197,18 +188,11 @@ def crop_by_extents(image, extent, wrap=False):

# offset from origin x, y, horizontal scale, vertical scale
changes = (coords[0], coords[1], changed_w/w, changed_h/h)
pprint(changes)
# pprint(changes)

return (image, changes)

# pprint(coords)

# d = ImageDraw.Draw(overlay)
# d.rectangle(coords, fill=(255,0,0,50))

# return Image.alpha_composite(image, overlay)

def pack_images(image_paths, background=(0,0,0,0), format="PNG", extents=None, wrap=False):
def pack_images(image_paths, background=(0,0,0,0), format="PNG", extents=None, wrap=False, crop=False):
images = []
blocks = []
image_name_map = {}
Expand All @@ -223,7 +207,7 @@ def pack_images(image_paths, background=(0,0,0,0), format="PNG", extents=None, w
changes = None
if extents:
print(filename, image.size)
image, changes = crop_by_extents(image, extents[filename], wrap=wrap)
image, changes = crop_by_extents(image, extents[filename], wrap, crop)

images.append(image)
image_name_map[filename] = image
Expand All @@ -248,45 +232,18 @@ def pack_images(image_paths, background=(0,0,0,0), format="PNG", extents=None, w
uv_changes[fname] = {
"offset": (
# should be in [0, 1] range
(block.x - changes[0])/output_image.size[0],
(block.x - (changes[0] if changes else 0))/output_image.size[0],
# UV origin is bottom left, PIL assumes top left!
# 1 - (block.y + image.size[1])/output_image.size[1]
(block.y - changes[1])/output_image.size[1]
(block.y - (changes[1] if changes else 0))/output_image.size[1]
),

"aspect": (
(1/changes[2])* (image.size[0]/output_image.size[0]),
(1/changes[3])* (image.size[1]/output_image.size[1])
((1/changes[2]) if changes else 1) * (image.size[0]/output_image.size[0]),
((1/changes[3]) if changes else 1) * (image.size[1]/output_image.size[1])
),


# "global": {
# "aspect": (
# (image.size[0]/output_image.size[0]),
# (image.size[1]/output_image.size[1])
# ),

# "offset": (
# # should be in [0, 1] range
# (block.x/output_image.size[0],
# # UV origin is bottom left, PIL assumes top left!
# # 1 - (block.y + image.size[1])/output_image.size[1]
# (block.x - changes[1])/output_image.size[1]
# )
# }
}

output_image.paste(image, (block.x, block.y))

output_image = output_image.transpose(Image.FLIP_TOP_BOTTOM)
return output_image, uv_changes

if __name__ == '__main__':
import glob
from pprint import pprint
filenames = glob.glob("E:\Applications\BTSync\Home Share\Programming\SimpleModelPacker\*.tga")
print(filenames)
if len(filenames) > 0:
img,uv = pack_images(filenames)
img.show()
pprint(uv)
40 changes: 19 additions & 21 deletions objuvpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from imagepacker import pack_images

def guess_realpath(path):
"""Tries to rectify user or other errors in a filepath"""
"""Checks for a file in a path, or in a local path"""
basename = os.path.basename(path)

# first test the path
Expand Down Expand Up @@ -47,7 +47,7 @@ def main():
obj_name = os.path.splitext(os.path.basename(args.obj))[0]

if not args.output: # default to a reasonable name
output_name = obj_name + "_p"
output_name = obj_name + "_packed"

# we read the entire obj both to check for mtl now, and for processing later
obj_lines = []
Expand Down Expand Up @@ -117,12 +117,6 @@ def main():
print("\tignoring transparency value")
continue

# elif line.startswith("map_"):
# print("\tignoring other map", line)
# continue
# amap = guess_realpath(line[7:])
# print("\tsaw ambient map", amap)
# line = "map_Ka " + outname
new_mtl_lines.append(line)

if len(dmaps) != len(names):
Expand Down Expand Up @@ -152,6 +146,9 @@ def add(self, x,y):
self.max_x = max(self.max_x, x) if self.max_x else x
self.max_y = max(self.max_y, y) if self.max_y else y

def uv_wrap(self):
return (self.max_x - self.min_x, self.max_y - self.min_y)

def wrapping(self):
if self.min_x < 0 or self.min_y < 0 or self.max_x > 1 or self.max_y > 1:
return (self.max_x - self.min_x, self.max_y - self.min_y)
Expand Down Expand Up @@ -207,18 +204,19 @@ def __repr__(self):
print(name, extent)
if extent.wrapping():
h_w, v_w = extent.wrapping()
print("\nWARNING: The following texture has coordinates that imply it wraps {}x{} times:\n\t{}".format(round(h_w, 1), round(v_w, 1), name))
to_wrap = False
try:
to_wrap = strtobool(input("Do you want to unroll this wrapping? [y/N]: "))
except ValueError as ve:
pass
extent.to_wrap = to_wrap

if to_wrap:
print("Marking texture to be wrapped.")
else:
print("Ignoring texture wrapping.")
if h_w >= 2 or v_w >= 2:
print("\nWARNING: The following texture has coordinates that imply it wraps {}x{} times:\n\t{}".format(round(h_w, 1), round(v_w, 1), name))
to_wrap = False
try:
to_wrap = strtobool(input("Do you want to unroll this wrapping? [y/N]: "))
except ValueError as ve:
pass
extent.to_wrap = to_wrap

if to_wrap:
print("Marking texture to be wrapped.")
else:
print("Ignoring texture wrapping.")

else:
textents = None
Expand Down Expand Up @@ -290,7 +288,7 @@ def __repr__(self):

if __name__ == '__main__':
import traceback
import os
# import os
# try:
main()
# except Exception as ex:
Expand Down

0 comments on commit 30114a4

Please sign in to comment.