2020-11-02 18:50:29

Hello everyone. I hope you all are having a grand day. I have a question for anyone that knows how to code in python.
So I am just starting out with python in a computer science class at school, so I am working on different labs. Well the lab that I am working on right now requires me to get info from a .jpg file, as well as to manipulate it. However, I cannot figure out for the life of me how to get the python terminal to focus on the file. I have looked for the answer on other sites but nothing I seem to find works. If anyone could please help, if anyone could please just tell me how to get python to focus on this jpg file, then I would really really appreciate it. I have been stuck on this for almost a week and just want to make some kind of progress.

It seems that common sense is a lost art. I am here as a special type of artist to bring that beauty back. If you don't like my work, prove that yours is better. Paint me a beautiful picture of life. Otherwise, I have no interest.

2020-11-02 18:53:21

dont know what you exactly need, do you want to navegate to the file from a command line? just, cd <full path to file>, please be a bit more clear

2020-11-02 19:19:24

This thread belongs to developper section.

Also You should put more details on what are You aiming to do, what kinda info You want to pick etc.

2020-11-02 19:25:53

As for what info I need. I need to get the height and width of the image to start out with.

It seems that common sense is a lost art. I am here as a special type of artist to bring that beauty back. If you don't like my work, prove that yours is better. Paint me a beautiful picture of life. Otherwise, I have no interest.

2020-11-02 20:00:10

You say "focus on".  What does this mean?

A lot of people end up using a library called pil in order to extract image metadata, but there might be something built into the standard library for it.  This isn't a hard problem, though.  You should have been able to find solutions via Google.  What have you tried and how has it broken?  Is there a more specific question or problem that's stopping you?

My Blog
Twitter: @ajhicks1992

2020-11-02 20:10:25 (edited by Rastislav Kish 2020-11-02 20:13:37)

Hi there,
@1: hmm, under the term focus something, we usually understand moving the keyboard focus to a particular place in the environment. This is quite rarely done outside your own app, so I guess you probably meaned something else?

As for getting width and height of a particular image, well, this is not that simple. An image is a bunch of bytes, which you need to decode first to a matrix of pixels i.e. three dimensional array in order to work with it. As you wrote, that you're just starting in Python, make sure you know this concept, as doing this stuff without it is... kind of difficult. smile
Then, you most likely don't want to write your own JpG decoder, as there is plenty of them already available and it would be a waste of time. My favorite is ImageIO. You can install it along numpy like:

pip install numpy imageio

You can then load an image into a numpy array like so:

import numpy as np
from imageio import imread

image=np.asarray(imread("image.jpg"))
print(f"Dymensions of the image are {image.shape}")

Here image.shape returns a tuple containing array dymensions i.e. if the array is 3D, it will contain three items, usually x, y and 3.
Now, the question is, which of the two first elements is width and whichone height.
If I remember right, the firstone was height and secondone width, but I recommend checking it out either in imageio's documentation or by comparing with a viewer, i've worked with few libraries for handling images including ImageIO, PIL and OpenCV, and they use various formats for this.

PIL and OpenCV also have various functions for shifting, rotating, zooming and other functions, which you may be interested in. There are plenty of tutorials especially for OpenCV, btw the loading syntax is very similar to thatone of ImageIO.
Also note that the sample above decompresses and loads the whole image to memory, as finding its shape is usually not the only thing you want to do. There are also cases when however it's the only required information, and you don't need to load the whole thing in that case. OpenCV has a function to get the dimensions only, so you can use it as well when necessary.

Best regards

Rastislav

2020-11-02 20:44:44

@6
That's the least efficient way to do it that possibly exists.  Use pil, or an ffmpeg wrapper, or any of several other things that know how to get this efficiently.  You can almost always get it from encoded headers/metadata or, failing that, by only partly decoding the file.  What you're proposing technically answers the question but is very much in the category of "all I had was a hammer", if you're familiar with the English idiom.  Since the dedicated image libraries aren't actually more code, why not use them?

Numpy/Scipy also only support a very limited set of formats out of the box anyway, unless something has changed.  I'm not actually sure if jpg is on that list or not.

Unfortunately it looks like the built-in Jpeg module was removed in Python 3.

My Blog
Twitter: @ajhicks1992

2020-11-02 22:34:29 (edited by Rastislav Kish 2020-11-02 22:35:17)

Hi there,
@7: I recommend reading the post to the end, that's a good practice in general. smile

Best regards

Rastislav

2020-11-02 23:13:47

Considering your goal is to get the metadata and alter the image data, you'll likely require an image handling library like some of those mentioned. Other's could include Pyglet, wxPython, or Pygame as well.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-11-02 23:21:42

moderation:
Nothing to see here, just moving the topic to a place that would better befit the subject.

2020-11-02 23:46:08

@8
I did read your entire post.  My point stands.  Posting solutions that are the worst way to do something then saying "so this is kind of bad" when the things that are better than it are at the same level of difficulty is counterproductive.  If this was collision or something and it was "use this really slow 10 line algorithm" vs "learn about several data structures, then use them together", okay.  But this isn't that in the slightest.

My Blog
Twitter: @ajhicks1992

2020-11-03 00:59:41

Pygame uses imageio, doesn't it? I'd've gone with the same as 6, on the grounds that 1 was rather nonspecific about what all will be needed when, so I'd just load the whole thing and be done with it.
That said, I had no idea they'd dropped jpg support.  ... Why did they do that? That's weird. I wasn't aware that jpg was the XP of image formats, or whatever.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2020-11-03 01:04:28 (edited by magurp244 2020-11-03 01:05:20)

@12
I'm not sure its a specific bias towards JPEG, so much as the Python dev's clearing out redundant code. There was an update about it [here], much of their rationalization is that the code is dated, unmaintained, and third parties like PIL provide better support.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

2020-11-03 01:12:42

Hi all. I am sorry for being so unclear up to this point. As I said I am just getting into python and coding in general for that matter, so I don't really know how to word what exactly I need properly. Also, the lab I am doing was supposed to be done through the repl.it website but I am trying to do it through the command line version since repl.it doesn't really seem to be accessible enough to use.

It seems that common sense is a lost art. I am here as a special type of artist to bring that beauty back. If you don't like my work, prove that yours is better. Paint me a beautiful picture of life. Otherwise, I have no interest.

2020-11-03 02:42:12

@14, I suspect what you meant to say was that you couldn't get your code to open the file. You can parse the image manually, but that's not going to be an easy task at all and I wouldn't do that if your just starting out. There are rare times when you do need to manually parse an image, and this isn't one of them. Use pil/imageio to do it. A google search should guide you. (Don't use the method outlined in@6; that'll just give you a headache and confuse you a lot more.)

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2020-11-03 08:25:07

hi,
you can use pillow, OpenCV, imageIO, scikit-image and so on.
for OpenCV (which I use myself), you call imread with the name of your image as the first parameter, and what it gives you back is a numpy.ndarray which you can obtain it's shape
to install OpenCV, execute this command in your cmd:

pip install opencv_python

2020-11-03 16:38:31

@15
As far as I know, pillow is a better version of pil, and the API is still the same.

-----
I have code on GitHub

2020-11-04 18:26:21

Alright everyone hello again. So I have appreciated everything that you guys have suggested. Unfortunately however, I must ask another question about the topic. So you guys have suggested opencv and pillow and pil and all of those, but there is just one problem. From what I have found, you require the command terminal in order to install any of those. The problem with that is that I don't have access to cmd because my school has blocked it on their computers. Is there any other way to install these without cmd?

It seems that common sense is a lost art. I am here as a special type of artist to bring that beauty back. If you don't like my work, prove that yours is better. Paint me a beautiful picture of life. Otherwise, I have no interest.

2020-11-04 19:10:58

you are blocked from opening up  cmd.exe? but I thought you were using the python interpreter on command line since the online repl wasn't accessible. how are you working with the python interpreter then?

also on a unrelated note. have you asked your teacher what they expect you to complete the lab? i.e. are you just supposed to use things in the standard library or to use some other library like PIL?

also unrelated, to the others here that recommended numpy and other data science libraries to to a complete programming beginner, instead of pil:
WAT

2020-11-04 20:01:30

Are you sure it's not the run dialog or something else that's blocked? Try making a file called cmd.bat with the following:

@echo off
%systemroot%\system32\cmd.exe

Otherwise it may be worth asking for an accommodation, as without a command prompt you aren't going to get far unfortunately.