PIL中Image模块的show函数无法正常显示图片

本文介绍了解决Python中PIL库无法正常显示图片的问题。通过在ImageShow.py文件中添加特定代码,使得图片能够在Windows环境下成功展示。

from PIL import Image

im = Image.open('img.jpg')

im.show()

显示如下:

到C:\Python27\Lib\site-packages\PIL找到ImageShow.py文件 添加以下字段(划线的部分:PING 127.0.0.1 -n 5 > NUL


ok可以正常显示了


转载自 http://www.cnblogs.com/Mr-Rice/p/3746064.html


对于 `ToPILImage().show()` 函数显示图片的问题,可从以下方面尝试解决: ### PIL Image模块 show函数问题解决思路借鉴 若使用的是PIL相关功能,当 `show()` 函数正常显示图片时,在win7系统下,可对 `X:\Python26\lib\site-packages\PIL\ImageShow.py` 文件第99行进行替换,替换内容为: ```plaintext return "start /wait %s && PING 127.0.0.1 -n 5 > NUL && del /f %s" % (file, file) ``` 这样或许能解决图片无法正常显示的问题 [^1]。 ### 图片转化过程检查 图片在内存中以 `bytes` 形式存储,转化过程需确保步骤正确。具体步骤为: ```python import torch from PIL import Image # 假设img是你的图片对象 # img.tobytes() 将图片转化成内存中的存储格式 byte_data = img.tobytes() # torch.BytesStorage.frombuffer(img.tobytes() ) 将字节以流的形式输入,转化成一维的张量 tensor_1d = torch.BytesStorage.from_buffer(byte_data) # 对张量进行reshape # 此处需根据实际图片尺寸修改 reshaped_tensor = tensor_1d.reshape(...) # 对张量进行permute(2,0,1) permuted_tensor = reshaped_tensor.permute(2, 0, 1) # 将当前张量的每个元素除以255 normalized_tensor = permuted_tensor / 255 # 输出张量 torchvision.transforms.ToPILImage from torchvision import transforms to_pil = transforms.ToPILImage() pil_img = to_pil(normalized_tensor) pil_img.show() ``` 确保转化过程无误,可能会解决显示图片的问题 [^3]。 ### 参考相关论坛 可参考相关论坛寻求解决方案,如在解决PIL Image模块 `show` 函数无法正常显示图片问题时,可参考的链接为:http://www.velocityreviews.com/forums/t707158-python-pil-and-vista-windows-7-show-not-working.html [^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值