问题:使用with方法在pycharm中写图片和视频文件时,无法正确将图片写入项目中。
# with方法读图片文件
with open(r'img.png', 'rb') as file:
print(file.read())
# with 方法写图片文件
with open(r'img.png1', 'wb') as file:
result = file.write()
报错内容:TypeError: write() takes exactly one argument (0 given)
问题及思路:open方法是可以完成这种操作的,但不知道为什么with方法就不行,报错提示翻译过来就是file.write()只接受一个参数,难道是img.png图片参数多了?
想要达成的结果:在pycharm项目里边重新再出现一张跟原图一模一样的图片。
"""
copy()方法
"""
from PIL import Image
picture = Image.open('yase.jpeg')
copy_pic = picture.copy() # 复制
copy_pic.save('copy_pic.jpg')
# with方法读图片文件
with open(r'img.png', 'rb') as file:
content = file.read()
# with 方法写图片文件
with open(r'img.png1', 'wb') as file:
result = file.write(content)