@@ -505,6 +505,50 @@ def test_get(self):
505
505
self .assertRaises (tkinter .TclError , image .get , 16 , 15 )
506
506
self .assertRaises (tkinter .TclError , image .get , 15 , 16 )
507
507
508
+ def test_read (self ):
509
+ # Due to the Tk bug https://core.tcl-lang.org/tk/tktview/1576528
510
+ # the -from option does not work correctly for GIF and PNG files.
511
+ # Use the PPM file for this test.
512
+ testfile = support .findfile ('python.ppm' , subdir = 'tkinterdata' )
513
+ image = tkinter .PhotoImage (master = self .root , file = testfile )
514
+
515
+ image2 = tkinter .PhotoImage (master = self .root )
516
+ image2 .read (testfile )
517
+ self .assertEqual (image2 .type (), 'photo' )
518
+ self .assertEqual (image2 .width (), 16 )
519
+ self .assertEqual (image2 .height (), 16 )
520
+ self .assertEqual (image2 .get (0 , 0 ), image .get (0 , 0 ))
521
+ self .assertEqual (image2 .get (4 , 6 ), image .get (4 , 6 ))
522
+
523
+ self .assertRaises (tkinter .TclError , image2 .read , self .testfile , 'ppm' )
524
+
525
+ image2 = tkinter .PhotoImage (master = self .root )
526
+ image2 .read (testfile , from_coords = (2 , 3 , 14 , 11 ))
527
+ self .assertEqual (image2 .width (), 12 )
528
+ self .assertEqual (image2 .height (), 8 )
529
+ self .assertEqual (image2 .get (0 , 0 ), image .get (2 , 3 ))
530
+ self .assertEqual (image2 .get (11 , 7 ), image .get (13 , 10 ))
531
+ self .assertEqual (image2 .get (2 , 4 ), image .get (2 + 2 , 4 + 3 ))
532
+
533
+ image2 = tkinter .PhotoImage (master = self .root , file = testfile )
534
+ self .assertEqual (image2 .width (), 16 )
535
+ self .assertEqual (image2 .height (), 16 )
536
+ image2 .read (testfile , from_coords = (2 , 3 , 14 , 11 ), shrink = True )
537
+ self .assertEqual (image2 .width (), 12 )
538
+ self .assertEqual (image2 .height (), 8 )
539
+ self .assertEqual (image2 .get (0 , 0 ), image .get (2 , 3 ))
540
+ self .assertEqual (image2 .get (11 , 7 ), image .get (13 , 10 ))
541
+ self .assertEqual (image2 .get (2 , 4 ), image .get (2 + 2 , 4 + 3 ))
542
+
543
+ image2 = tkinter .PhotoImage (master = self .root )
544
+ image2 .read (testfile , from_coords = (2 , 3 , 14 , 11 ), to = (3 , 6 ))
545
+ self .assertEqual (image2 .type (), 'photo' )
546
+ self .assertEqual (image2 .width (), 15 )
547
+ self .assertEqual (image2 .height (), 14 )
548
+ self .assertEqual (image2 .get (0 + 3 , 0 + 6 ), image .get (2 , 3 ))
549
+ self .assertEqual (image2 .get (11 + 3 , 7 + 6 ), image .get (13 , 10 ))
550
+ self .assertEqual (image2 .get (2 + 3 , 4 + 6 ), image .get (2 + 2 , 4 + 3 ))
551
+
508
552
def test_write (self ):
509
553
filename = os_helper .TESTFN
510
554
import locale
@@ -516,26 +560,78 @@ def test_write(self):
516
560
517
561
image .write (filename )
518
562
image2 = tkinter .PhotoImage ('::img::test2' , master = self .root ,
519
- format = 'ppm' ,
520
- file = filename )
563
+ format = 'ppm' , file = filename )
521
564
self .assertEqual (str (image2 ), '::img::test2' )
522
565
self .assertEqual (image2 .type (), 'photo' )
523
566
self .assertEqual (image2 .width (), 16 )
524
567
self .assertEqual (image2 .height (), 16 )
525
568
self .assertEqual (image2 .get (0 , 0 ), image .get (0 , 0 ))
526
- self .assertEqual (image2 .get (15 , 8 ), image .get (15 , 8 ))
569
+ self .assertEqual (image2 .get (4 , 6 ), image .get (4 , 6 ))
527
570
528
571
image .write (filename , format = 'gif' , from_coords = (4 , 6 , 6 , 9 ))
529
572
image3 = tkinter .PhotoImage ('::img::test3' , master = self .root ,
530
- format = 'gif' ,
531
- file = filename )
573
+ format = 'gif' , file = filename )
574
+ self .assertEqual (str (image3 ), '::img::test3' )
575
+ self .assertEqual (image3 .type (), 'photo' )
576
+ self .assertEqual (image3 .width (), 2 )
577
+ self .assertEqual (image3 .height (), 3 )
578
+ self .assertEqual (image3 .get (0 , 0 ), image .get (4 , 6 ))
579
+ self .assertEqual (image3 .get (1 , 2 ), image .get (5 , 8 ))
580
+
581
+ image .write (filename , background = '#ff0000' )
582
+ image4 = tkinter .PhotoImage ('::img::test4' , master = self .root ,
583
+ format = 'ppm' , file = filename )
584
+ self .assertEqual (image4 .get (0 , 0 ), (255 , 0 , 0 ))
585
+ self .assertEqual (image4 .get (4 , 6 ), image .get (4 , 6 ))
586
+
587
+ image .write (filename , grayscale = True )
588
+ image5 = tkinter .PhotoImage ('::img::test5' , master = self .root ,
589
+ format = 'ppm' , file = filename )
590
+ c = image5 .get (4 , 6 )
591
+ self .assertTrue (c [0 ] == c [1 ] == c [2 ], c )
592
+
593
+ def test_data (self ):
594
+ image = self .create ()
595
+
596
+ data = image .data ()
597
+ self .assertIsInstance (data , tuple )
598
+ for row in data :
599
+ self .assertIsInstance (row , str )
600
+ self .assertEqual (data [6 ].split ()[4 ], '#%02x%02x%02x' % image .get (4 , 6 ))
601
+
602
+ data = image .data ('ppm' )
603
+ image2 = tkinter .PhotoImage ('::img::test2' , master = self .root ,
604
+ format = 'ppm' , data = data )
605
+ self .assertEqual (str (image2 ), '::img::test2' )
606
+ self .assertEqual (image2 .type (), 'photo' )
607
+ self .assertEqual (image2 .width (), 16 )
608
+ self .assertEqual (image2 .height (), 16 )
609
+ self .assertEqual (image2 .get (0 , 0 ), image .get (0 , 0 ))
610
+ self .assertEqual (image2 .get (4 , 6 ), image .get (4 , 6 ))
611
+
612
+ data = image .data (format = 'gif' , from_coords = (4 , 6 , 6 , 9 ))
613
+ image3 = tkinter .PhotoImage ('::img::test3' , master = self .root ,
614
+ format = 'gif' , data = data )
532
615
self .assertEqual (str (image3 ), '::img::test3' )
533
616
self .assertEqual (image3 .type (), 'photo' )
534
617
self .assertEqual (image3 .width (), 2 )
535
618
self .assertEqual (image3 .height (), 3 )
536
619
self .assertEqual (image3 .get (0 , 0 ), image .get (4 , 6 ))
537
620
self .assertEqual (image3 .get (1 , 2 ), image .get (5 , 8 ))
538
621
622
+ data = image .data ('ppm' , background = '#ff0000' )
623
+ image4 = tkinter .PhotoImage ('::img::test4' , master = self .root ,
624
+ format = 'ppm' , data = data )
625
+ self .assertEqual (image4 .get (0 , 0 ), (255 , 0 , 0 ))
626
+ self .assertEqual (image4 .get (4 , 6 ), image .get (4 , 6 ))
627
+
628
+ data = image .data ('ppm' , grayscale = True )
629
+ image5 = tkinter .PhotoImage ('::img::test5' , master = self .root ,
630
+ format = 'ppm' , data = data )
631
+ c = image5 .get (4 , 6 )
632
+ self .assertTrue (c [0 ] == c [1 ] == c [2 ], c )
633
+
634
+
539
635
def test_transparency (self ):
540
636
image = self .create ()
541
637
self .assertEqual (image .transparency_get (0 , 0 ), True )
0 commit comments