nuoxoxo / pythonchallenge-in-go

Solving the Python Challenge in Go
http://www.pythonchallenge.com
0 stars 0 forks source link

16.py #2

Closed nuoxoxo closed 3 months ago

nuoxoxo commented 4 months ago
from collections import defaultdict, Counter
from PIL import Image
from random import randint

mozart = Image.open('files/mozart.gif')
print('mozart/', type(mozart))
print('bounds/', mozart.size, type(mozart.size))
C, R = mozart.size

# inspect/ bytes object -----------

def wrapper():
    for n in [0, 1, 10, 42, randint(0, C), randint(0, C)]:
        rowprinter(n)

def wrapperRGB():
    for n in [0, 1, 10, 42, randint(0, C), randint(0, C)]:
        rowprinterRGB(n)

def gifreader():
    pal = mozart.getpalette()
    rgb = [(pal[i], pal[i + 1], pal[i + 2]) for i in range(0, len(pal), 3)]
    hgm = mozart.histogram()
    print('info/', mozart.info)
    print('pale/', pal[:42])
    print('len/', len(pal), "type/", type(pal))
    print('60/', rgb[60])
    print('0/', rgb[0])
    print('96/', rgb[96])
    print('95/', rgb[95])
    print('24/', rgb[24])
    print('54/', rgb[54])
    print('59/', rgb[59])
    print('88/', rgb[88])
    print('hgm/', hgm, type(hgm))
    counter = Counter(hgm)
    head = counter.most_common(6)
    print('head/', head)

gifreader()

def rowprinterRGB(start):
    r = mozart.crop((0, start, R, start + 1))
    r = r.convert('RGB')
    rb = r.tobytes('raw', 'RGB')
    counter = Counter()
    for i in range(0, len(rb), 3): # 3 bytes at a time
        k = tuple(rb[i:i +3 ])
        counter[k] += 1
    head = counter.most_common(21)
    print('\nfromrow/', start)
    print('cropped/', r, type(r))
    print('rawbytes/', rb[:16], type(rb))
    print('lenbytes/', len(rb))
    print('head/')
    for k, v in head: print(k, '-', v)

def rowprinter(start):
    r = mozart.crop((0, start, R, start + 1))
    rb = r.tobytes('raw', 'P')
    head = Counter(rb).most_common(6)
    headx = hex(head[0][0])
    print('\nfromrow/', start)
    print('cropped/', r, type(r))
    print('rawbytes/', rb[:16], type(rb))
    print('lenbytes/', len(rb))
    print('most/', head)
    print('hex/', headx)
    print('rep/', repr(headx))
    if head[0][0] != 60: print("\nexception/byte", head[0][0], '\n')
    assert head[0][0] in [60, 0, 95, 96]

# wrapperRGB()
# wrapper()
### we know the most-occurring byte is 60 (not pink)
### exception (not 60)
### other common occurrences: 96 - 95 - 0

"""
431/95
97/96
232/96
292/96
"""
nuoxoxo commented 4 months ago

intuition / initial attempt in Go

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
    "image"
    "image/gif"
    "os"
    "reflect"
)

func main(){
    mozart, _ := os.Open("files/mozart.gif")
    defer mozart.Close()
    mozartgif, _ := gif.Decode(mozart)
    bounds := mozartgif.Bounds()
    fmt.Println("bounds/", bounds, reflect.TypeOf(bounds))
    fmt.Println("rows", bounds.Dy())
    fmt.Println("cols", bounds.Dx())
    R := bounds.Dy()
    C := bounds.Dx()

    // paletted := image.NewPaletted(bounds, mozartgif.(*image.Paletted).Palette)
    pimg, _ := mozartgif.(*image.Paletted)
    r := 0
    for r < R {
        row := make(map[uint8]int)
        c := 0
        for c < C {
            color := pimg.ColorIndexAt(c, r)
            row[color]++
            c++
        }
        for k, v := range row {
            fmt.Println("row/", k, v)
        }
        fmt.Println("")
        r++
    }
}

func init(){
    URL := "http://www.pythonchallenge.com/pc/return/mozart.html"
    ups, mid := "hugefile", 4
    conn := & http.Client{}
    req, err := http.NewRequest("GET", URL, nil)
    if err != nil {fmt.Println("err/", err)}

    req.SetBasicAuth(ups[:mid], ups[mid:])
    resp, _ := conn.Do(req)
    defer resp.Body.Close()

    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body), "\nbody ends/\n\n")

}