purl = 'https://www.icoolhunt.com/prey/%d' % pid
	
        #downloads the photo to images/ dir
        try:
            getImageFromURL(purl, str(pid), 'images/')
        except urllib2.HTTPError:
            with open('processError.log', 'a') as err:
                err.write('%s\t%s\n' % (pid, 'HTTP Error'))
            continue
	
	#file to be analyzed
	filename = 'images/%d' % pid
	
        #returns rgb colors, count=n
        try:
            photoRGBColors = colorz(filename, n=3)
        except IOError:
            with open('processError.log', 'a') as err:
                err.write('%s\t%s\n' % (pid, 'image IOError'))
            continue
	
	#write to file [pid - R1 - G1 - B1 - R2 - G2 - B2 - R3 - G3 - B3]
	fRGB.write("%d\t" % pid + '\t'.join([("%s" % color) for sublist in photoRGBColors for color in sublist])+'\n')
	
	#convert to HSV
	photoHSVColors = [convert_color(sRGBColor(*c), HSVColor) for c in photoRGBColors]
	
	#write to file [pid - H1 - S1 - V1 - H2 - S2 - V2 - H3 - S3 - V3]
	fHSV.write('%d\t' % pid + '\t'.join(['\t'.join([str(color.hsv_h), str(color.hsv_s), str(color.hsv_v)]) for color in photoHSVColors])+'\n')
	
fRGB.close()
images = os.listdir('images')

n_clusters = 10

fRGB = open('RGB10colors.txt', 'a')
fHSL = open('HSL10colors.txt', 'a')

for pid in images[:10]:
    #write to file [pid - R1 - G1 - B1 - R2 - G2 - B2 - R3 - G3 - B3]

    #file to be analyzed
    filename = 'images/%s' % pid

    try:
        photoRGBColors = colorz(filename, n=n_clusters)
    except IOError:
        with open('processImageError.log', 'a') as err:
            err.write('%s\t%s\n' % (pid, 'image IOError'))
        continue

    #pid, R1,G1,B1, R2,G2,B2, R3,G3,B3 = map(int, prey.strip().split('\t'))
    #photoRGBColors = [[R1,G1,B1], [R2,G2,B2], [R3,G3,B3]]

    fRGB.write("%s\t" % pid + '\t'.join([("%s" % color) for sublist in photoRGBColors for color in sublist])+'\n')
        

    #convert to HSL
    photoHSLColors = [convert_color(sRGBColor(*c, is_upscaled=True), HSLColor) for c in photoRGBColors]
    
    #write to file [pid - H1 - S1 - L1 - H2 - S2 - L2 - H3 - S3 - L3]