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

Alignment #1

Closed
BaldrLector opened this issue Dec 29, 2019 · 3 comments
Closed

Alignment #1

BaldrLector opened this issue Dec 29, 2019 · 3 comments

Comments

@BaldrLector
Copy link

BaldrLector commented Dec 29, 2019

Hi, Great job! Thanks for your sharing.

The method works well in given examples, however, I get a worse result when using the picture generated by waifulabs.com.

The pic is from waifulabs.com, and I convert it to RGBA format with Photoshop. I alignment the face to 128x128 center box and resize the pic to 256x256
image

the results look like this:
image

I believe it should be something wrong at the alignment,
so could you please share the alignment code?

Here my code of alignment:


from PIL import Image
import numpy as np
import cv2

def alinment_image_lowhigh(image, high, low, save_path=None):
    '''
    :param image: image path
    :param high: the bigger hight cordinate
    :param low:  the smaller hight cordinate
    :param save_path:
    :return:
    '''
    image = np.array(Image.open(image))
    size = np.array(image.shape[:2])

    # adjust face scale to 128
    h = high - low
    scale = 128 / h
    high, low = high * scale, low * scale
    hc = (high + low) / 2
    image = np.array(Image.fromarray(image).resize((int(size[0] * scale), int(size[1] * scale))))

    # move face to center
    size = np.array(image.shape[:2])
    delta = size[0] / 2 - hc
    M = np.float32([[1, 0, 0], [0, 1, delta]])
    image = cv2.warpAffine(image, M, (size[1], size[0]), borderValue=(255, 255, 255))
    image = Image.fromarray(image)
    image = image.resize(256, 256)
    if save_path is not None: image.save(save_path)
    image.show()

Thanks, your reply will be appreciated.

@pkhungurn
Copy link
Owner

Please check the RGB channels of the PNG file you exported from Photoshop. One of the condition for the input is that if A = 0, then RGB should be (0,0,0). When you remove the background using Photoshop, it is most likely the case that it just sets the alpha channel to 0, leaving the RGB channel as white. Please clear the RGB channels.

@HelixNGC7293
Copy link

Hi, Great job! Thanks for your sharing.

The method works well in given examples, however, I get a worse result when using the picture generated by waifulabs.com.

The pic is from waifulabs.com, and I convert it to RGBA format with Photoshop. I alignment the face to 128x128 center box and resize the pic to 256x256
image

the results look like this:
image

I believe it should be something wrong at the alignment,
so could you please share the alignment code?

Here my code of alignment:


from PIL import Image
import numpy as np
import cv2

def alinment_image_lowhigh(image, high, low, save_path=None):
    '''
    :param image: image path
    :param high: the bigger hight cordinate
    :param low:  the smaller hight cordinate
    :param save_path:
    :return:
    '''
    image = np.array(Image.open(image))
    size = np.array(image.shape[:2])

    # adjust face scale to 128
    h = high - low
    scale = 128 / h
    high, low = high * scale, low * scale
    hc = (high + low) / 2
    image = np.array(Image.fromarray(image).resize((int(size[0] * scale), int(size[1] * scale))))

    # move face to center
    size = np.array(image.shape[:2])
    delta = size[0] / 2 - hc
    M = np.float32([[1, 0, 0], [0, 1, delta]])
    image = cv2.warpAffine(image, M, (size[1], size[0]), borderValue=(255, 255, 255))
    image = Image.fromarray(image)
    image = image.resize(256, 256)
    if save_path is not None: image.save(save_path)
    image.show()

Thanks, your reply will be appreciated.

Hello BaldrLector~
You can use Gimp to export your PNG file (uncheck 'saving color value from trans pixs' when saving)
It should work!

@BaldrLector
Copy link
Author

Please check the RGB channels of the PNG file you exported from Photoshop. One of the condition for the input is that if A = 0, then RGB should be (0,0,0). When you remove the background using Photoshop, it is most likely the case that it just sets the alpha channel to 0, leaving the RGB channel as white. Please clear the RGB channels.

You are right, after clear the RGB channels, the method work well.
Thanks!

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

No branches or pull requests

3 participants