鱼C论坛

 找回密码
 立即注册
查看: 1320|回复: 3

Python问题

[复制链接]
发表于 2022-3-22 17:11:23 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

小甲鱼极客python之效率革命绘图篇:
源代码如下:
from PIL import Image

img = Image.open('1.png')
out = img.convert("L")  # 转换为灰度模式
#out.show()
print(out.size)
width,height = out.size
# 调节图片尺寸
# out = out.resize((int(width * 0.5), int(height * 0.5)))
# width, height = out.size

print(out.getpixel((10,10)))
print(out.size)
asciis = "$@#=+-."
texts = ''
for row in range(height):
    for col in range(width):
        gray = out.getpixel((col,row))
        texts += asciis[int(gray/255 *7)]
    texts += '\n'

with open('test1.txt','w') as file:
    file.write(texts)


想请问,为什么会报错:IndexError: string index out of range
图片大小设置为了512*512或者256**256都是一样的报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-23 01:12:51 | 显示全部楼层
报错发全
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-23 16:03:17 | 显示全部楼层

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/Xiao_jiayu/绘图篇/malilian.py", line 20, in <module>
(512, 512)
0
(256, 76)
    texts += asciis[int(gray/255 *7)]
IndexError: string index out of range

Process finished with exit code 1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-26 21:41:20 | 显示全部楼层
int(gray / 255 * 7) = 7
字符串 $@#=+-. 一共7个元素,index从0到6,没有7
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 22:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表