Example #1
0
def haishokuColor(img):
    # dominant = Haishoku.getDominant(img)
    # Haishoku.showDominant(img)
    # print(dominant)
    palette = Haishoku.getPalette(img)
    x = []
    y = []
    for per, color in palette:
        x.append(per)
        y.append(color)
    print('调色盘色彩比例:\n', palette)
    Haishoku.showPalette(img)
Example #2
0
def run_bot(reddit):
    print("Obtaining Submissions...")

    for comment in reddit.inbox.mentions(limit=None):
        footer = "\n\n ^I'm a bot. | Creator: [u/JoshuaScript](https://www.reddit.com/u/JoshuaScript). | [Source Code](https://github.com/Joshuascript/Palette_Bot)"
        #checks if the mention is unread, which is only the case if it hasn't been replied to
        if comment.new:
            try:
                #calls the showPalette (which creates and saves the image) method on the image link
                Haishoku.showPalette(comment.submission.url)
                CLIENT_ID = config.imgur_id
                PATH = haishoku.haillow.image_name
                im = pyimgur.Imgur(CLIENT_ID)
                uploaded_image = im.upload_image(
                    PATH,
                    title=
                    f"Color Palette for Reddit Post: {comment.submission.title} ({comment.submission.url})"
                )
                print(uploaded_image.title)
                print(uploaded_image.link)
                print(uploaded_image.size)
                print(uploaded_image.type)
                comment.reply(
                    f"\n\n Here is the color palette of this image visualized: {uploaded_image.link}. The color sizes are proportionate to their dominance in the image. {footer}"
                )
                #Marks the comment as read so it won't be replied to again
                comment.mark_read()
                print(
                    f"Replied to {comment.submission.title} ({comment.submission.url})"
                )
                #deletes generated palette image from local storage
                os.remove(PATH)
            except OSError as e:
                comment.reply(
                    f"This is most likely a non-image post. Try mentioning me in a new comment and I may be able to get this post's palette if it is an image.{footer}"
                )
                print(
                    f"Comment was most likely on a non-image post ({e}) \n\n Title: {comment.submission.title}, Link: {comment.submission.url}"
                )
                comment.mark_read()
            except TypeError as e:
                comment.reply(
                    f"I was unable to get the palette of this image, most likely because it's a gif, which is currently unsupported.{footer}"
                )
                print(
                    f"Image was likely a gif ({e}) \n\n Title: {comment.submission.title}, Link: {comment.submission.url}"
                )
                comment.mark_read()
            except Exception as e:
                print(e.__class__.__name__ + e)
        else:
            continue
Example #3
0
def generate(img_path,
             palette_size,
             freq_min=None,
             debug=False,
             show_palette=False):
    """returns a list of RGB tuples representing a palette of palette_size numbers of color, by maximum use"""

    try:
        full_palette = Haishoku.getPalette(str(img_path))
        map_palette = full_palette[:palette_size]
        if show_palette:
            Haishoku.showPalette(str(img_path))
    except FileNotFoundError:
        print(
            f"File {img_path} not found, be sure this includes the full or relative path - the folders containing the file, not just the file's name."
        )
        exit()

    if debug:
        print(f' ► Full Palette (Freq,RGB) = {full_palette}')
        print(f' ► Map Reduced Palette (Freq,RGB) = {map_palette}')
        print(f' ► Autopalette threshold = {freq_min}')

    if freq_min:
        output = []
        if map_palette[0][0] < freq_min:
            # return dominant color if no colors exceed threshold
            output = [map_palette[0][1]]
            print('  =Sample Warning: ')
            print(
                f'   No color exceeds in {round(freq_min*100,1)}% sample tile. Color {output[0]} represents highest porportion of sample ({map_palette[0][0]*100}%) and will be used as result.'
            )
        else:
            # filter colors below freq_min
            for freq, rgb in map_palette:
                if freq >= freq_min:
                    output.append(rgb)
        if debug:
            print(f' ► Sample tile palette length: {len(output)}')

    else:
        # return only the RGB values
        output = dict(map_palette).values()

    return output
Example #4
0
def main():
    path = "/Users/wujianming/Desktop/WechatIMG18547.jpeg"
    # path = "http://wx2.sinaimg.cn/large/89243dfbly1ffoekfainzj20dw05k0u7.jpg"

    # getPalette api
    palette = Haishoku.getPalette(path)

    # getDominant api
    dominant = Haishoku.getDominant(path)

    # showPalette api
    Haishoku.showPalette(path)

    # showDominant api
    # Haishoku.showDominant(path)

    # Haishoku object
    h = Haishoku.loadHaishoku(path)
    print(h.palette)
    print(h.dominant)
Example #5
0
def main():
    path = "demo_01.png"

    # getPalette api
    palette = Haishoku.getPalette(path)
    print(palette)

    # getDominant api
    dominant = Haishoku.getDominant(path)
    print(dominant)

    # showPalette api
    Haishoku.showPalette(path)

    # showDominant api
    Haishoku.showDominant(path)

    # Haishoku object
    h = Haishoku.loadHaishoku(path)
    print(h.image)
    print(h.palette)
    print(h.dominant)
Example #6
0
def main():
    url = "https://img3.doubanio.com/lpic/s27028282.jpg"
    r = requests.get(url)
    path = BytesIO(r.content)

    # getPalette api
    palette = Haishoku.getPalette(path)
    print(palette)

    # getDominant api
    dominant = Haishoku.getDominant(path)
    print(dominant)

    # showPalette api
    Haishoku.showPalette(path)

    # showDominant api
    Haishoku.showDominant(path)

    # Haishoku object
    h = Haishoku.loadHaishoku(path)
    print(h.palette)
    print(h.dominant)
Example #7
0
def myNewSize(a1):
    width = a1.size[0]
    height = a1.size[1]
    while (width > 500 or height > 500):
        width = 0.9 * width
        height = 0.9 * height
    return width, height


# 窗口居中

palette = Haishoku.getPalette(fp)
palette2 = Haishoku.getPalette2(fp)
#导入

palettePic1 = Haishoku.showPalette(fp)
palettePic2 = Haishoku.showPalette2(fp)
# 配色可视化

colorF = toPalette(palette)
colorF2 = toPalette(palette2)

window = tk.Tk()
window.title('调色盘')
window.configure(background='#323232')

pic = Image.open(fp)
[w, h] = myNewSize(pic)
pic = pic.resize((int(w), int(h)), Image.ANTIALIAS)

photo = ImageTk.PhotoImage(pic)
Example #8
0
from haishoku.haishoku import Haishoku
from PIL import Image
import pprint

img_path = 'images/kershisnik.jpg'
haishoku = Haishoku.loadHaishoku(img_path)

Haishoku.showPalette(img_path)
palette = Haishoku.getPalette(img_path)
print('palette: {}'.format(palette))