Package: a-pandas-ex-image-tools
$pipinstalla-pandas-ex-image-toolsimportcv2importpandasaspdfroma_cv2_imshow_threadimportadd_imshow_thread_to_cv2add_imshow_thread_to_cv2()froma_pandas_ex_image_toolsimportpd_add_image_tools,debugger,get_list_of_all_colors_in_rangepd_add_image_tools()#you can ignore all exceptionsdebugger.stop_on_exceptions=True#Useful if you want to ignore every exception, but want to see what's going ondebugger.print_all_exceptions=Truepythonlogo='https://github.com/hansalemaos/screenshots/raw/main/colorfind1.png'df=pd.Q_image2df(pythonlogo)dfOut[5]:xyredgreenblue000000110000220000330000440000...............3999519519900039996196199000399971971990003999819819900039999199199000[40000rowsx5columns]df1=df.im_multicolor_lookup([(255,232,115)])df1Out[11]:xyredgreenblue13337137662552321151333813866255232115133391396625523211513340140662552321151334114166255232115...............25269691262552321152527070126255232115254676712725523211525468681272552321152566767128255232115[608rowsx5columns]df1.im_xy_to_convex_hull()# get the convex hullOut[12]:array([[[154,67]],[[153,68]],[[73,124]],[[67,128]],[[67,114]],[[70,108]],[[74,104]],[[137,66]],[[150,66]]],dtype=int32)df.im_show_df_image()# cv2.imshow# converts the pandas DataFrame back to a numpy arraydf.im_df_to_np_image()df.im_print_all_colors()# prints all colors sorted by qtydf1.im_xy_to_shapely()Out[13]:<shapely.geometry.polygon.Polygonat0x21302f70># Gets all x,y coordinates as a numpy arraydf1.im_xy_to_np()Out[14]:array([[137,66],[138,66],[139,66],...,[67,127],[68,127],[67,128]],dtype=uint16)# you can even edit imagesdf.loc[df.im_multicolor_lookup([(0,0,0)]).index,['red','green','blue']]=[255,255,0]# Replace a color range with one colordf.loc[df.im_multicolor_lookup(get_list_of_all_colors_in_range((0,0,0),(10,10,10))).index,['red','green','blue']]=[255,255,0]# cluster colors in areas# More about the parameters: https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.htmlmultilookupdf=df.im_multicolor_lookup(colorlist=[(255,232,115),(255,0,0),(48,105,152)],).im_xy_to_color_clusters(eps=2,min_samples=4,n_jobs=-1,poly_color=(255,255,0),poly_outline_thickness=3,rectanglecolor=(0,255,0),)multilookupdfOut[24]:aa_repr_pointaa_bounds...aa_draw_polyaa_draw_rectangle0(124,82)(106,66,132,96)...()()1(142,71)(137,66,154,79)...()()2(73,119)(67,102,104,128)...()()3(158,123)(144,117,164,131)...()()4(119,155)(93,139,132,167)...()()[5rowsx9columns]# Cluster multiple colorsmultilookupdf=df.im_multicolor_lookup(colorlist=[(255,232,115),(255,0,0),(48,105,152)],).im_xy_to_color_clusters(eps=2,min_samples=4,n_jobs=-1,poly_color=(255,0,120),poly_outline_thickness=3,rectanglecolor=(0,255,0),)# show the clustered resultsnpimage=df.im_df_to_np_image().copy()multilookupdf.aa_draw_rectangle.apply(lambdax:x(npimage))multilookupdf.aa_draw_poly.apply(lambdax:x(npimage))cv2.imshow_thread(npimage)bab=df.im_multicolor_lookup(colorlist=[(255,232,115),(48,105,152)],).im_xy_to_merged_color_clusters(image=df.im_df_to_np_image().copy(),show_results=True,max_merge_distance=2,eps=2,min_samples=4,n_jobs=-1,poly_color=(255,255,0),poly_outline_thickness=3,rectanglecolor=(0,255,0),mergedcolor=(0,0,255),)babOut[6]:aa_repr_point...bb_pic0(124,82)...[[[0,0,0],[0,0,0],[0,0,0],[0,0,0],...1(142,71)...[[[0,0,0],[0,0,0],[0,0,0],[0,0,0],...2(73,119)...[[[0,0,0],[0,0,0],[0,0,0],[0,0,0],...# Do you prefer working with dicts instead of DataFrames?df.im_get_color_lookup_dict(){(0,0,0):array([[0,0],[1,0],[2,0],...,[196,199],[197,199],[198,199]],dtype=uint16),(15,33,48):array([[199,199]],dtype=uint16),(17,35,49):array([[131,88]],dtype=uint16),(17,35,50):array([[63,114]],dtype=uint16),(17,36,53):array([[75,99]],dtype=uint16),(17,37,53):array([[119,97],[129,91]],dtype=uint16),(18,36,50):array([[126,94]],dtype=uint16),(18,37,52):array([[125,35]],dtype=uint16),(19,38,53):array([[68,104]],dtype=uint16),(19,39,55):array([[36,124],[73,100]],dtype=uint16),...df.im_get_image_size()# Out[7]: [(0, 0, 200, 200)]# Count colors in certain regionsdf.im_get_colors_in_regions_and_count(regions=[(10,10,100,100),(150,150,160,170)])Out[11]:redgreenblue...size_of_areapercentage_of_arearegion0000...810061.925926(10,10,100,100)169132182...81001.790123(10,10,100,100)2255255255...81001.160494(10,10,100,100)367129179...81001.148148(10,10,100,100)462123172...81001.135802(10,10,100,100).......................21859110151...81000.012346(10,10,100,100)21959111153...81000.012346(10,10,100,100)22060112154...81000.012346(10,10,100,100)22162111147...81000.012346(10,10,100,100)222000...200115.500000(150,150,160,170)[223rowsx7columns]# You can limit the search regiondf.im_limit_search_areas(areas=[(0,0,50,50),(50,50,150,170)]).im_multicolor_lookup(colorlist=[(255,232,115),(255,0,0),(48,105,152)])Out[12]:xyredgreenblue1333213266481051521333713766255232115133381386625523211513339139662552321151334014066255232115...............33502102167255003350310316725500335041041672550033505105167255003350610616725500[1352rowsx5columns]# define regions for every individual colorcolordict={(255,0,0):[(0,0,50,50),(0,0,1000,1000)],(255,232,115):[(0,0,1000,1000),(500,500,502,502),(900,900,1000,950),],(255,255,255):[(0,0,1000,1000),(400,200,800,400)],(0,176,111):[(0,0,1000,1000)],(57,59,61):[(0,0,1000,1000)],(0,0,0):[(0,0,1000,1000)],}regi=df.im_get_coords_of_colors_in_regions(colordict)xyredgreenblue000000110000220000330000440000...............3999519519900039996196199000399971971990003999819819900039999199199000[28789rowsx5columns]# You can use a multi index to check individual pixelsdfxy=df.im_xy_as_index()redgreenbluexy000001000200030004000.........199195000196000197000198000199000[40000rowsx3columns]# Let's get the RGB values using the DataFrame we have just createddfxy.loc[[(0,100),(100,100),(44,40)]]Out[17]:redgreenbluexy01000001001000004440000# can also be done like that:coordlist=[(10,10),(100,100),(50,80)]colors_of_coords=df.im_get_colors_of_coords(coordlist)colors_of_coordsOut[19]:redgreenbluexy1010000100100000508070134184Compare2images:pythonlogo='https://github.com/hansalemaos/screenshots/raw/main/colorfind1.png'df=pd.Q_image2df(pythonlogo)df.loc[df.im_multicolor_lookup(get_list_of_all_colors_in_range((0,0,0),(10,10,10))).index,['red','green','blue']]=[255,255,0]pythonlogo='https://github.com/hansalemaos/screenshots/raw/main/colorfind1.png'df2=pd.Q_image2df(pythonlogo)df2.im_compare_2_images(df)Out[23]:pic_indexxyr1g1b1r2g2b20000000255255011100002552550222000025525503330000255255044400002552550.......................2751039995195199000255255027511399961961990002552550275123999719719900025525502751339998198199000255255027514399991991990002552550[27515rowsx9columns]# If you want to do additional stuff with OpenCV:df.im_xy_to_cv2_coords()Out[28]:array([[[0,0],[1,0],[2,0],...,[197,199],[198,199],[199,199]]])df.im_singlecolor_lookup((255,255,0))Out[45]:xyredgreenblue00025525501102552550220255255033025525504402552550...............399951951992552550399961961992552550399971971992552550399981981992552550399991991992552550[27515rowsx5columns]df.im_get_closest_colors([(10,3,4)])Out[55]:rgbratingrgb000011.179688(10,3,4)115334853.500000(10,3,4)217354955.656250(10,3,4)317355056.468750(10,3,4)418365057.187500(10,3,4).................712250242218400.750000(10,3,4)713252247230411.250000(10,3,4)714241244246412.250000(10,3,4)715253251243421.500000(10,3,4)716255255255432.000000(10,3,4)[717rowsx5columns]get_list_of_all_colors_in_range((0,0,0),(3,4,3))Out[59]:[(0,0,0),(0,0,1),(0,0,2),(0,0,3),(0,1,0),(0,1,1),(0,1,2),(0,1,3),(0,2,0),(0,2,1),(0,2,2),(0,2,3),(0,3,0),dfr=df.im_rgb_as_index()Out[65]:xyredgreenblue00000010020030040......255255255120155255121155255117156255118156255119156[40000rowsx2columns]#Now you can use dfr.loc to get the coordinatesdfr.loc[(17,37,53)]dfr.loc[(39,83,119)]# finds all colors that only exist in the region and nowhere else in the picturedf.im_get_unique_colors_in_region([(0,0,100,100)])Out[72]:xyredgreenblue1843232927313718818433339273137187184343492721361871843535927213618618436369272135186.............1425555717113518614256567171135185142575771711341851226666615310214018431319259111153[845rowsx5columns]# finds all colors that are in the region and somewhere else in the picturedf.im_get_not_unique_colors_in_region([(0,0,100,100)])Out[73]:xyredgreenblue000000110000220000330000440000...............200969610000020097971000002009898100000200999910000020100100100000[9356rowsx5columns]