Convert JPG to PNG - python

I have used the below code to
convert JPG into PNG file:
But when i am running this code from the command line terminal using: python a.py "C:\Users\nishant.gupta2\PycharmProjects\jpgtopngconverter\photo" new
The system is giving me the error:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\nishant.gupta2\\PycharmProjects\\jpgtopngconverter\\photo'
My code is below:
import sys
import os
from PIL import Image
image_folder=sys.argv[1]
output_folder=sys.argv[2]
if not os.path.exists(output_folder):
os.mkdir(output_folder)
for items in os.listdir(image_folder):
im= Image.open(f'{image_folder}')
im.save(f'{output_folder}.png','png')

It looks like this has to do with file permissions rather than your code. Your code is running under a separate user than the image folder you are specifying, depending on admin status you may get permission issues.

You are trying to open a folder here :
im= Image.open(f'{image_folder}')
rather then image. You should specify the path of image.

Related

Python using os and chdir to read and write to iCloud

I am using the following python code to access a folder in iCloud. I am getting an error of:
FileNotFoundError: [Errno 2] No such file or directory:
import os
os.chdir('/Users/me/Library/Mobile\ Documents/com~apple~CloudDocs/jupyter/')
Is there something I am missing?
There is no backslash in the directory name; you aren't using a shell, so the space does not need to be escaped.
os.chdir('/Users/me/Library/Mobile Documents/com~apple~CloudDocs/jupyter/')

Python permission error when importing a png

VsCode gives me this error:
PS D:\leomu\Documents\Python projects\Bots> &
C:/Python/Python39/python.exe "d:/leomu/Documents/Python
projects/Bots/verify.py"
File "d:\leomu\Documents\Python projects\Bots\verify.py", line 4, in <module>
img = Image.open("D:\leomu\Documents\Python projects\Bots\FototsChat") File
"C:\Python\Python39\lib\site-packages\PIL\Image.py", line 2904, in
open
fp = builtins.open(filename, "rb") PermissionError: [Errno 13] Permission denied: 'D:\\leomu\\Documents\\Python
projects\\Bots\\FototsChat'
when running this:
import pytesseract, time
from PIL import Image
img = Image.open("D:\leomu\Documents\Python projects\Bots\FototsChat")
text = pytesseract.image_to_string("img")
print(text)
The problem is that you are trying to load a file which is protected and cant be openened/changed by non admin users. To fix that, it depends what OS you are on
For windows users:
Open the terminal/your IDE as administrator and that should do the trick.
For linux/mac users:
Open run put sudo before your command to run the program.
Edit: i read the question better and realised the path to the image points to either a file without extension (like .png) or a folder (i suspect its a folder).
So if you are trying to open the file 'test.png' from within that folder, just do something like 'D:\leomu\Documents\Python projects\Bots\FototsChat\test.png'

How to fix a Ghostscript OS Error when using Pillow?

This is the error I am getting:
OS Error: Ghostscript not found on paths
When running the following function:
from PIL import Image
def convert_ps_to_png(ps_file):
img = Image.open(ps_file)
img = Image.save("file.png")# Error occurs on this line
convert_ps_to_png(ps_file_path)
Any solutions would be appreciated. The end goal is to convert a .ps file to a .png file through a script.
As per the answers to your previous questions, you need to have Ghostscript installed, and available in the system environment variable $PATH.

read_pdf FileNotFoundError: [Errno 2] No such file or directory: in Python

I am trying to scrape tables from pdf with read_pdf in python. I am using read_pdf but it doesn't do the job. Also, to mention, I do this in MAC with Jupiter notebook.
This is what I do:
from tabula import read_pdf
file = read_pdf(r'C:\Users\myname\Rprojects\Reports_scraping\data_scraped\icnarc_29052020\icnarc_200529.pdf')
I get this error:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\myname\\Rprojects\\Reports_scraping\\data_scraped\\icnarc_29052020\\icnarc_200529.pdf'
How I can solve this issue?
just to check that the file exist, do you get True when running this:
import os
file_path = r'C:\Users\myname\Rprojects\Reports_scraping\data_scraped\icnarc_29052020\icnarc_200529.pdf'
print( os.path.isfile(file_path))
Edit file_path with wherever is the file(using Python 3).
And did you change "myname" in the path with your actual username... (just in case)
It is preferable to build your paths using os.path.join to make things compatible, on windows it will need to create a root "config.py" file, see
how to get the root folder on windows
#
having discussed with GaB, it seemed that he is using Jupyter notebook on Mac, which explains issues, I saw this link, but can't help more.
Jupyter - import pdf
os.path.join doc
There can be only one possibility, the file is not there, but you have already checked that I assume, if not, Once again check whether the spelling of file is correct. If this doesn't work, then do below trick
Execute the py code in same folder as file, and then use
from tabula import read_pdf
file = read_pdf(r'icnarc_200529.pdf')
Sometimes, this simple method does the trick.

IOError: [Errno 2] No such file or directory: 'bg8.png'

I am new to Python & I'm doing a project in which I have to print so many pictures.But when I run the program I'm getting the following error,
IOError: [Errno 2] No such file or directory: 'bg8.png'
I'm sure that the filename is correct and I have all the pictures including 'bg8.png'in my python folder.
You might have no permission to that file. Please check path and permission of bg8.png
You need PIL library to allow/show png/jpeg based pictures. try this
also try os.chdir(directory) and `pic = open("name.png)

Categories

Resources