Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed abnoma Alpha of output img. Fixed a potential issues. #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 23 additions & 32 deletions scripts/Mosaic_Creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
parser.set_defaults(resize=True)
parser.add_argument('--shuffle', dest='shuffle', action='store_true',help='Whether shuffle the sequence when setting the grids')
parser.set_defaults(shuffle=False)
parser.add_argument('--alpha', nargs=2, dest='alpha', type=int, help='The alpha for images and target-image, The alpha values for images and target-image, in the range [0, 255]')
parser.add_argument('--alpha', nargs=2, dest='alpha', type=int, help='The alpha values for images and target-image, in the range [0, 255]')
parser.set_defaults(alpha=[255, 0])
parser.add_argument('--magnify', nargs=1, dest='magnification', type=float, help='Enlarge the final out_img, float type')
parser.set_defaults(magnification=(1.0,))
Expand All @@ -26,22 +26,14 @@

MATCH_INDECES = []

def getImages(images_directory,alpha_input):
def getImages(images_directory):
files = os.listdir(images_directory)
images = []
for file in files:
filePath = os.path.abspath(os.path.join(images_directory, file))
try:
fp = open(filePath, "rb")
im = Image.open(fp)

if not alpha_input[0] == 255:
# in alpha[0] not default(255), reset alpha of every img
im=im.convert("RGBA")
r, g, b, alpha = im.split()
alpha = alpha.point(lambda i: i>0 and alpha_input[0])
im.putalpha(alpha)

images.append(im)
im.load()
fp.close()
Expand Down Expand Up @@ -115,6 +107,9 @@ def createPhotomosaic(target_image, input_images, grid_size,
try:
avgs.append(getAverageRGB(img))
except ValueError:
# If no append inf, avgs_index and input_imgs_index will not correspond
avgs.append((float("inf"),float("inf"),float("inf")))
print("An unsafe img is found, you may need to run Image_Tester.py to delete it")
continue

grid_indices = np.arange(len(target_images))
Expand All @@ -133,34 +128,34 @@ def createPhotomosaic(target_image, input_images, grid_size,

mosaic_image = createImageGrid(output_images, grid_size)

if not alpha_input[1] == 0:
# reset the alpha layer of target_image
target_image = target_image.convert("RGBA")
r, g, b, alpha = target_image.split()
alpha = alpha.point(lambda i: i>0 and alpha_input[1])
target_image.putalpha(alpha)

# resize the target_image, matching the final out-img
target_image = target_image.resize(mosaic_image.size)

# merge target_image and Composite img
result_image = Image.alpha_composite(mosaic_image, target_image)
return (result_image)

mosaic_image = mosaic_image.convert("RGBA") #default 255
target_image = target_image.convert("RGBA")

# resize the target_image, matching the final out-img
target_image = target_image.resize(mosaic_image.size)

if not alpha_input[0] == 255:
mosaic_image.putalpha(alpha_input[0])

if not alpha_input[1] == 255:
target_image.putalpha(alpha_input[1])


# merge target_image and Composite img
mosaic_image = Image.alpha_composite(mosaic_image, target_image) # RGBA
return (mosaic_image)


### ---------------------------------------------

# alpha value of ..
# alpha value of imgs and target_img
alpha_input = args.alpha

target_image = Image.open(args.target)

# input images
print('reading input folder...')
input_images = getImages(args.images,alpha_input)
input_images = getImages(args.images)

# check if any valid input images found
if input_images == []:
Expand Down Expand Up @@ -213,11 +208,7 @@ def createPhotomosaic(target_image, input_images, grid_size,
mosaic_image = createPhotomosaic(target_image, input_images, grid_size, reuse_images,shuffle_input,alpha_input)

# write out mosaic
if alpha_input == [255, 0]:
mosaic_image.convert("RGB").save(output_filename, 'jpeg') #defalut, no alpha, always discard alpha
else:
mosaic_image.save(output_filename, 'png') #customer alpha

mosaic_image.convert("RGB").save(output_filename, 'jpeg') # always output as jpeg to handle incorrect alpha band

print("saved output to %s" % (output_filename,))
print('done.')