python图片转字符画怎么打开图片
1、这里以jupyter notebook作为演示,新建一个空白的PYTHON文件。
2、with open('test.png', 'r') as photo: print(photo.read()) 首先我们要知道打开图片的方式,这里以只读的方式打开,但是出错了,因为我们要以二进制的方式打开。
3、with open('test.png', 'rb') as photo: print(photo.read()) 这个时候我们加了小写的'b',那么就可以读取图片的信息了,但是这些都是看不懂的信息。
4、with open('test.png&拭貉强跳#39;, 'rb') as photo: with open('test_copy.png&垆杪屑丝#39;, 'wb') as photo_copy: photo_copy.write(photo.read()) 我们可以用嵌套,一边打开一边写另外一个新文件,这样就能以字符的形式打开图片。但是这样只能处理一些比较小的图片。
5、with open('test.png', 'rb') as photo: print(len(photo.read())) 我们查看一下总长度,实际上我们打开某些图片的时候,因为太大一次性电脑会很慢。我们可以设定读取的值。
6、with open('test.png', 'rb') as photo: with open('test_copy1.png', 'wb') as photo_copy1: max = 1000 max_read = photo.read(max) 为了一次读取比较少,我们设定比较合适的值。
7、with open('test.png', 'rb') as photo: with open('test_copy1.png', 'wb') as photo_copy1: max = 1000 max_read = photo.read(max) while len(max_read) > 0:然后我们设置一下循环条件。
8、with open('test.png&拭貉强跳#39;, 'rb') as photo: with open('test_copy1.png媪青怍牙', 'wb') as photo_copy1: max = 1000 max_read = photo.read(max) while len(max_read) > 0: photo_copy1.write(max_read) 循环里规定每次只能读取的数量。
9、with open('test.png&拭貉强跳#39;, 'rb') as photo: with open('test_copy1.png媪青怍牙', 'wb') as photo_copy1: max = 1000 max_read = photo.read(max) while len(max_read) > 0: photo_copy1.write(max_read) max_read = photo.read(max) 最后我们必须让程序连续按照我们的指定的间距读取,因为read重复写就会继续往下读取。